Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <malloc.h> |
| 9 | #include <net.h> |
Ben Warren | 10efa02 | 2008-08-31 20:37:00 -0700 | [diff] [blame] | 10 | #include <netdev.h> |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <pci.h> |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 13 | #include <miiphy.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 15 | |
| 16 | #undef DEBUG |
| 17 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 18 | /* Ethernet chip registers. */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 19 | #define SCBStatus 0 /* Rx/Command Unit Status *Word* */ |
| 20 | #define SCBIntAckByte 1 /* Rx/Command Unit STAT/ACK byte */ |
| 21 | #define SCBCmd 2 /* Rx/Command Unit Command *Word* */ |
| 22 | #define SCBIntrCtlByte 3 /* Rx/Command Unit Intr.Control Byte */ |
| 23 | #define SCBPointer 4 /* General purpose pointer. */ |
| 24 | #define SCBPort 8 /* Misc. commands and operands. */ |
| 25 | #define SCBflash 12 /* Flash memory control. */ |
| 26 | #define SCBeeprom 14 /* EEPROM memory control. */ |
| 27 | #define SCBCtrlMDI 16 /* MDI interface control. */ |
| 28 | #define SCBEarlyRx 20 /* Early receive byte count. */ |
| 29 | #define SCBGenControl 28 /* 82559 General Control Register */ |
| 30 | #define SCBGenStatus 29 /* 82559 General Status register */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 31 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 32 | /* 82559 SCB status word defnitions */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 33 | #define SCB_STATUS_CX 0x8000 /* CU finished command (transmit) */ |
| 34 | #define SCB_STATUS_FR 0x4000 /* frame received */ |
| 35 | #define SCB_STATUS_CNA 0x2000 /* CU left active state */ |
| 36 | #define SCB_STATUS_RNR 0x1000 /* receiver left ready state */ |
| 37 | #define SCB_STATUS_MDI 0x0800 /* MDI read/write cycle done */ |
| 38 | #define SCB_STATUS_SWI 0x0400 /* software generated interrupt */ |
| 39 | #define SCB_STATUS_FCP 0x0100 /* flow control pause interrupt */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 40 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 41 | #define SCB_INTACK_MASK 0xFD00 /* all the above */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 42 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 43 | #define SCB_INTACK_TX (SCB_STATUS_CX | SCB_STATUS_CNA) |
| 44 | #define SCB_INTACK_RX (SCB_STATUS_FR | SCB_STATUS_RNR) |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 45 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 46 | /* System control block commands */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 47 | /* CU Commands */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 48 | #define CU_NOP 0x0000 |
| 49 | #define CU_START 0x0010 |
| 50 | #define CU_RESUME 0x0020 |
| 51 | #define CU_STATSADDR 0x0040 /* Load Dump Statistics ctrs addr */ |
| 52 | #define CU_SHOWSTATS 0x0050 /* Dump statistics counters. */ |
| 53 | #define CU_ADDR_LOAD 0x0060 /* Base address to add to CU commands */ |
| 54 | #define CU_DUMPSTATS 0x0070 /* Dump then reset stats counters. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 55 | |
| 56 | /* RUC Commands */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 57 | #define RUC_NOP 0x0000 |
| 58 | #define RUC_START 0x0001 |
| 59 | #define RUC_RESUME 0x0002 |
| 60 | #define RUC_ABORT 0x0004 |
| 61 | #define RUC_ADDR_LOAD 0x0006 /* (seems not to clear on acceptance) */ |
| 62 | #define RUC_RESUMENR 0x0007 |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 63 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 64 | #define CU_CMD_MASK 0x00f0 |
| 65 | #define RU_CMD_MASK 0x0007 |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 66 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 67 | #define SCB_M 0x0100 /* 0 = enable interrupt, 1 = disable */ |
| 68 | #define SCB_SWI 0x0200 /* 1 - cause device to interrupt */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 69 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 70 | #define CU_STATUS_MASK 0x00C0 |
| 71 | #define RU_STATUS_MASK 0x003C |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 72 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 73 | #define RU_STATUS_IDLE (0<<2) |
| 74 | #define RU_STATUS_SUS (1<<2) |
| 75 | #define RU_STATUS_NORES (2<<2) |
| 76 | #define RU_STATUS_READY (4<<2) |
| 77 | #define RU_STATUS_NO_RBDS_SUS ((1<<2)|(8<<2)) |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 78 | #define RU_STATUS_NO_RBDS_NORES ((2<<2)|(8<<2)) |
| 79 | #define RU_STATUS_NO_RBDS_READY ((4<<2)|(8<<2)) |
| 80 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 81 | /* 82559 Port interface commands. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 82 | #define I82559_RESET 0x00000000 /* Software reset */ |
| 83 | #define I82559_SELFTEST 0x00000001 /* 82559 Selftest command */ |
| 84 | #define I82559_SELECTIVE_RESET 0x00000002 |
| 85 | #define I82559_DUMP 0x00000003 |
| 86 | #define I82559_DUMP_WAKEUP 0x00000007 |
| 87 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 88 | /* 82559 Eeprom interface. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 89 | #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */ |
| 90 | #define EE_CS 0x02 /* EEPROM chip select. */ |
| 91 | #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */ |
| 92 | #define EE_WRITE_0 0x01 |
| 93 | #define EE_WRITE_1 0x05 |
| 94 | #define EE_DATA_READ 0x08 /* EEPROM chip data out. */ |
| 95 | #define EE_ENB (0x4800 | EE_CS) |
| 96 | #define EE_CMD_BITS 3 |
| 97 | #define EE_DATA_BITS 16 |
| 98 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 99 | /* The EEPROM commands include the alway-set leading bit. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 100 | #define EE_EWENB_CMD (4 << addr_len) |
| 101 | #define EE_WRITE_CMD (5 << addr_len) |
| 102 | #define EE_READ_CMD (6 << addr_len) |
| 103 | #define EE_ERASE_CMD (7 << addr_len) |
| 104 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 105 | /* Receive frame descriptors. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 106 | struct RxFD { |
| 107 | volatile u16 status; |
| 108 | volatile u16 control; |
| 109 | volatile u32 link; /* struct RxFD * */ |
| 110 | volatile u32 rx_buf_addr; /* void * */ |
| 111 | volatile u32 count; |
| 112 | |
| 113 | volatile u8 data[PKTSIZE_ALIGN]; |
| 114 | }; |
| 115 | |
| 116 | #define RFD_STATUS_C 0x8000 /* completion of received frame */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 117 | #define RFD_STATUS_OK 0x2000 /* frame received with no errors */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 118 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 119 | #define RFD_CONTROL_EL 0x8000 /* 1=last RFD in RFA */ |
| 120 | #define RFD_CONTROL_S 0x4000 /* 1=suspend RU after receiving frame */ |
| 121 | #define RFD_CONTROL_H 0x0010 /* 1=RFD is a header RFD */ |
| 122 | #define RFD_CONTROL_SF 0x0008 /* 0=simplified, 1=flexible mode */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 123 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 124 | #define RFD_COUNT_MASK 0x3fff |
| 125 | #define RFD_COUNT_F 0x4000 |
| 126 | #define RFD_COUNT_EOF 0x8000 |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 127 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 128 | #define RFD_RX_CRC 0x0800 /* crc error */ |
| 129 | #define RFD_RX_ALIGNMENT 0x0400 /* alignment error */ |
| 130 | #define RFD_RX_RESOURCE 0x0200 /* out of space, no resources */ |
| 131 | #define RFD_RX_DMA_OVER 0x0100 /* DMA overrun */ |
| 132 | #define RFD_RX_SHORT 0x0080 /* short frame error */ |
| 133 | #define RFD_RX_LENGTH 0x0020 |
| 134 | #define RFD_RX_ERROR 0x0010 /* receive error */ |
| 135 | #define RFD_RX_NO_ADR_MATCH 0x0004 /* no address match */ |
| 136 | #define RFD_RX_IA_MATCH 0x0002 /* individual address does not match */ |
| 137 | #define RFD_RX_TCO 0x0001 /* TCO indication */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 138 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 139 | /* Transmit frame descriptors */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 140 | struct TxFD { /* Transmit frame descriptor set. */ |
| 141 | volatile u16 status; |
| 142 | volatile u16 command; |
| 143 | volatile u32 link; /* void * */ |
| 144 | volatile u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */ |
| 145 | volatile s32 count; |
| 146 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 147 | volatile u32 tx_buf_addr0; /* void *, frame to be transmitted. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 148 | volatile s32 tx_buf_size0; /* Length of Tx frame. */ |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 149 | volatile u32 tx_buf_addr1; /* void *, frame to be transmitted. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 150 | volatile s32 tx_buf_size1; /* Length of Tx frame. */ |
| 151 | }; |
| 152 | |
| 153 | #define TxCB_CMD_TRANSMIT 0x0004 /* transmit command */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 154 | #define TxCB_CMD_SF 0x0008 /* 0=simplified, 1=flexible mode */ |
| 155 | #define TxCB_CMD_NC 0x0010 /* 0=CRC insert by controller */ |
| 156 | #define TxCB_CMD_I 0x2000 /* generate interrupt on completion */ |
| 157 | #define TxCB_CMD_S 0x4000 /* suspend on completion */ |
| 158 | #define TxCB_CMD_EL 0x8000 /* last command block in CBL */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 159 | |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 160 | #define TxCB_COUNT_MASK 0x3fff |
| 161 | #define TxCB_COUNT_EOF 0x8000 |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 162 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 163 | /* The Speedo3 Rx and Tx frame/buffer descriptors. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 164 | struct descriptor { /* A generic descriptor. */ |
| 165 | volatile u16 status; |
| 166 | volatile u16 command; |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 167 | volatile u32 link; /* struct descriptor * */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 168 | |
| 169 | unsigned char params[0]; |
| 170 | }; |
| 171 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 172 | #define CONFIG_SYS_CMD_EL 0x8000 |
| 173 | #define CONFIG_SYS_CMD_SUSPEND 0x4000 |
| 174 | #define CONFIG_SYS_CMD_INT 0x2000 |
| 175 | #define CONFIG_SYS_CMD_IAS 0x0001 /* individual address setup */ |
| 176 | #define CONFIG_SYS_CMD_CONFIGURE 0x0002 /* configure */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 177 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | #define CONFIG_SYS_STATUS_C 0x8000 |
| 179 | #define CONFIG_SYS_STATUS_OK 0x2000 |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 180 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 181 | /* Misc. */ |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 182 | #define NUM_RX_DESC PKTBUFSRX |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 183 | #define NUM_TX_DESC 1 /* Number of TX descriptors */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 184 | |
| 185 | #define TOUT_LOOP 1000000 |
| 186 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 187 | static struct RxFD rx_ring[NUM_RX_DESC]; /* RX descriptor ring */ |
| 188 | static struct TxFD tx_ring[NUM_TX_DESC]; /* TX descriptor ring */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 189 | static int rx_next; /* RX descriptor ring pointer */ |
| 190 | static int tx_next; /* TX descriptor ring pointer */ |
| 191 | static int tx_threshold; |
| 192 | |
| 193 | /* |
| 194 | * The parameters for a CmdConfigure operation. |
| 195 | * There are so many options that it would be difficult to document |
| 196 | * each bit. We mostly use the default or recommended settings. |
| 197 | */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 198 | static const char i82558_config_cmd[] = { |
| 199 | 22, 0x08, 0, 1, 0, 0, 0x22, 0x03, 1, /* 1=Use MII 0=Use AUI */ |
| 200 | 0, 0x2E, 0, 0x60, 0x08, 0x88, |
| 201 | 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */ |
| 202 | 0x31, 0x05, |
| 203 | }; |
| 204 | |
| 205 | static void init_rx_ring (struct eth_device *dev); |
| 206 | static void purge_tx_ring (struct eth_device *dev); |
| 207 | |
| 208 | static void read_hw_addr (struct eth_device *dev, bd_t * bis); |
| 209 | |
| 210 | static int eepro100_init (struct eth_device *dev, bd_t * bis); |
Joe Hershberger | bccbe61 | 2012-05-21 14:45:25 +0000 | [diff] [blame] | 211 | static int eepro100_send(struct eth_device *dev, void *packet, int length); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 212 | static int eepro100_recv (struct eth_device *dev); |
| 213 | static void eepro100_halt (struct eth_device *dev); |
| 214 | |
Wolfgang Denk | 03b0040 | 2014-10-21 15:23:32 +0200 | [diff] [blame] | 215 | #if defined(CONFIG_E500) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 216 | #define bus_to_phys(a) (a) |
| 217 | #define phys_to_bus(a) (a) |
| 218 | #else |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 219 | #define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a) |
| 220 | #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a) |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 221 | #endif |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 222 | |
| 223 | static inline int INW (struct eth_device *dev, u_long addr) |
| 224 | { |
Bin Meng | e6655d7 | 2016-01-25 01:26:26 -0800 | [diff] [blame] | 225 | return le16_to_cpu(*(volatile u16 *)(addr + (u_long)dev->iobase)); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static inline void OUTW (struct eth_device *dev, int command, u_long addr) |
| 229 | { |
Bin Meng | e6655d7 | 2016-01-25 01:26:26 -0800 | [diff] [blame] | 230 | *(volatile u16 *)((addr + (u_long)dev->iobase)) = cpu_to_le16(command); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static inline void OUTL (struct eth_device *dev, int command, u_long addr) |
| 234 | { |
Bin Meng | e6655d7 | 2016-01-25 01:26:26 -0800 | [diff] [blame] | 235 | *(volatile u32 *)((addr + (u_long)dev->iobase)) = cpu_to_le32(command); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Jon Loeliger | 07d38a1 | 2007-07-09 17:30:01 -0500 | [diff] [blame] | 238 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 239 | static inline int INL (struct eth_device *dev, u_long addr) |
| 240 | { |
Bin Meng | e6655d7 | 2016-01-25 01:26:26 -0800 | [diff] [blame] | 241 | return le32_to_cpu(*(volatile u32 *)(addr + (u_long)dev->iobase)); |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 244 | static int get_phyreg (struct eth_device *dev, unsigned char addr, |
| 245 | unsigned char reg, unsigned short *value) |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 246 | { |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 247 | int cmd; |
| 248 | int timeout = 50; |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 249 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 250 | /* read requested data */ |
| 251 | cmd = (2 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16); |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 252 | OUTL (dev, cmd, SCBCtrlMDI); |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 253 | |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 254 | do { |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 255 | udelay(1000); |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 256 | cmd = INL (dev, SCBCtrlMDI); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 257 | } while (!(cmd & (1 << 28)) && (--timeout)); |
| 258 | |
| 259 | if (timeout == 0) |
| 260 | return -1; |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 261 | |
| 262 | *value = (unsigned short) (cmd & 0xffff); |
Wolfgang Denk | c570b2f | 2005-09-26 01:06:33 +0200 | [diff] [blame] | 263 | |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 267 | static int set_phyreg (struct eth_device *dev, unsigned char addr, |
| 268 | unsigned char reg, unsigned short value) |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 269 | { |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 270 | int cmd; |
| 271 | int timeout = 50; |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 272 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 273 | /* write requested data */ |
| 274 | cmd = (1 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16); |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 275 | OUTL (dev, cmd | value, SCBCtrlMDI); |
| 276 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 277 | while (!(INL (dev, SCBCtrlMDI) & (1 << 28)) && (--timeout)) |
| 278 | udelay(1000); |
| 279 | |
| 280 | if (timeout == 0) |
| 281 | return -1; |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |
Wolfgang Denk | a912733 | 2005-09-26 00:39:59 +0200 | [diff] [blame] | 285 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 286 | /* |
| 287 | * Check if given phyaddr is valid, i.e. there is a PHY connected. |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 288 | * Do this by checking model value field from ID2 register. |
| 289 | */ |
Ben Warren | d7fb9bc | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 290 | static struct eth_device* verify_phyaddr (const char *devname, |
| 291 | unsigned char addr) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 292 | { |
| 293 | struct eth_device *dev; |
| 294 | unsigned short value; |
| 295 | unsigned char model; |
| 296 | |
| 297 | dev = eth_get_dev_by_name(devname); |
| 298 | if (dev == NULL) { |
| 299 | printf("%s: no such device\n", devname); |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | /* read id2 register */ |
Mike Frysinger | 8ef583a | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 304 | if (get_phyreg(dev, addr, MII_PHYSID2, &value) != 0) { |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 305 | printf("%s: mii read timeout!\n", devname); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | /* get model */ |
| 310 | model = (unsigned char)((value >> 4) & 0x003f); |
| 311 | |
| 312 | if (model == 0) { |
| 313 | printf("%s: no PHY at address %d\n", devname, addr); |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | return dev; |
| 318 | } |
| 319 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 320 | static int eepro100_miiphy_read(struct mii_dev *bus, int addr, int devad, |
| 321 | int reg) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 322 | { |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 323 | unsigned short value = 0; |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 324 | struct eth_device *dev; |
| 325 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 326 | dev = verify_phyaddr(bus->name, addr); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 327 | if (dev == NULL) |
| 328 | return -1; |
| 329 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 330 | if (get_phyreg(dev, addr, reg, &value) != 0) { |
| 331 | printf("%s: mii read timeout!\n", bus->name); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 335 | return value; |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 338 | static int eepro100_miiphy_write(struct mii_dev *bus, int addr, int devad, |
| 339 | int reg, u16 value) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 340 | { |
| 341 | struct eth_device *dev; |
| 342 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 343 | dev = verify_phyaddr(bus->name, addr); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 344 | if (dev == NULL) |
| 345 | return -1; |
| 346 | |
| 347 | if (set_phyreg(dev, addr, reg, value) != 0) { |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 348 | printf("%s: mii write timeout!\n", bus->name); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
Jon Loeliger | 07d38a1 | 2007-07-09 17:30:01 -0500 | [diff] [blame] | 355 | #endif |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 356 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 357 | /* Wait for the chip get the command. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 358 | static int wait_for_eepro100 (struct eth_device *dev) |
| 359 | { |
| 360 | int i; |
| 361 | |
| 362 | for (i = 0; INW (dev, SCBCmd) & (CU_CMD_MASK | RU_CMD_MASK); i++) { |
| 363 | if (i >= TOUT_LOOP) { |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | static struct pci_device_id supported[] = { |
| 372 | {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557}, |
| 373 | {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559}, |
| 374 | {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER}, |
| 375 | {} |
| 376 | }; |
| 377 | |
| 378 | int eepro100_initialize (bd_t * bis) |
| 379 | { |
| 380 | pci_dev_t devno; |
| 381 | int card_number = 0; |
| 382 | struct eth_device *dev; |
| 383 | u32 iobase, status; |
| 384 | int idx = 0; |
| 385 | |
| 386 | while (1) { |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 387 | /* Find PCI device */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 388 | if ((devno = pci_find_devices (supported, idx++)) < 0) { |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | pci_read_config_dword (devno, PCI_BASE_ADDRESS_0, &iobase); |
| 393 | iobase &= ~0xf; |
| 394 | |
| 395 | #ifdef DEBUG |
| 396 | printf ("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n", |
| 397 | iobase); |
| 398 | #endif |
| 399 | |
| 400 | pci_write_config_dword (devno, |
| 401 | PCI_COMMAND, |
| 402 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
| 403 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 404 | /* Check if I/O accesses and Bus Mastering are enabled. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 405 | pci_read_config_dword (devno, PCI_COMMAND, &status); |
| 406 | if (!(status & PCI_COMMAND_MEMORY)) { |
| 407 | printf ("Error: Can not enable MEM access.\n"); |
| 408 | continue; |
| 409 | } |
| 410 | |
| 411 | if (!(status & PCI_COMMAND_MASTER)) { |
| 412 | printf ("Error: Can not enable Bus Mastering.\n"); |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | dev = (struct eth_device *) malloc (sizeof *dev); |
Nobuhiro Iwamatsu | 72c4c33 | 2010-10-19 14:03:41 +0900 | [diff] [blame] | 417 | if (!dev) { |
| 418 | printf("eepro100: Can not allocate memory\n"); |
| 419 | break; |
| 420 | } |
| 421 | memset(dev, 0, sizeof(*dev)); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 422 | |
| 423 | sprintf (dev->name, "i82559#%d", card_number); |
wdenk | 7a8e9bed | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 424 | dev->priv = (void *) devno; /* this have to come before bus_to_phys() */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 425 | dev->iobase = bus_to_phys (iobase); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 426 | dev->init = eepro100_init; |
| 427 | dev->halt = eepro100_halt; |
| 428 | dev->send = eepro100_send; |
| 429 | dev->recv = eepro100_recv; |
| 430 | |
| 431 | eth_register (dev); |
| 432 | |
Jon Loeliger | 07d38a1 | 2007-07-09 17:30:01 -0500 | [diff] [blame] | 433 | #if defined (CONFIG_MII) || defined(CONFIG_CMD_MII) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 434 | /* register mii command access routines */ |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 435 | int retval; |
| 436 | struct mii_dev *mdiodev = mdio_alloc(); |
| 437 | if (!mdiodev) |
| 438 | return -ENOMEM; |
| 439 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
| 440 | mdiodev->read = eepro100_miiphy_read; |
| 441 | mdiodev->write = eepro100_miiphy_write; |
| 442 | |
| 443 | retval = mdio_register(mdiodev); |
| 444 | if (retval < 0) |
| 445 | return retval; |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 446 | #endif |
| 447 | |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 448 | card_number++; |
| 449 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 450 | /* Set the latency timer for value. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 451 | pci_write_config_byte (devno, PCI_LATENCY_TIMER, 0x20); |
| 452 | |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 453 | udelay(10 * 1000); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 454 | |
| 455 | read_hw_addr (dev, bis); |
| 456 | } |
| 457 | |
| 458 | return card_number; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static int eepro100_init (struct eth_device *dev, bd_t * bis) |
| 463 | { |
Ben Warren | 422b1a0 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 464 | int i, status = -1; |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 465 | int tx_cur; |
| 466 | struct descriptor *ias_cmd, *cfg_cmd; |
| 467 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 468 | /* Reset the ethernet controller */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 469 | OUTL (dev, I82559_SELECTIVE_RESET, SCBPort); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 470 | udelay(20); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 471 | |
| 472 | OUTL (dev, I82559_RESET, SCBPort); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 473 | udelay(20); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 474 | |
| 475 | if (!wait_for_eepro100 (dev)) { |
| 476 | printf ("Error: Can not reset ethernet controller.\n"); |
| 477 | goto Done; |
| 478 | } |
| 479 | OUTL (dev, 0, SCBPointer); |
| 480 | OUTW (dev, SCB_M | RUC_ADDR_LOAD, SCBCmd); |
| 481 | |
| 482 | if (!wait_for_eepro100 (dev)) { |
| 483 | printf ("Error: Can not reset ethernet controller.\n"); |
| 484 | goto Done; |
| 485 | } |
| 486 | OUTL (dev, 0, SCBPointer); |
| 487 | OUTW (dev, SCB_M | CU_ADDR_LOAD, SCBCmd); |
| 488 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 489 | /* Initialize Rx and Tx rings. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 490 | init_rx_ring (dev); |
| 491 | purge_tx_ring (dev); |
| 492 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 493 | /* Tell the adapter where the RX ring is located. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 494 | if (!wait_for_eepro100 (dev)) { |
| 495 | printf ("Error: Can not reset ethernet controller.\n"); |
| 496 | goto Done; |
| 497 | } |
| 498 | |
| 499 | OUTL (dev, phys_to_bus ((u32) & rx_ring[rx_next]), SCBPointer); |
| 500 | OUTW (dev, SCB_M | RUC_START, SCBCmd); |
| 501 | |
| 502 | /* Send the Configure frame */ |
| 503 | tx_cur = tx_next; |
| 504 | tx_next = ((tx_next + 1) % NUM_TX_DESC); |
| 505 | |
| 506 | cfg_cmd = (struct descriptor *) &tx_ring[tx_cur]; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 507 | cfg_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_CONFIGURE)); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 508 | cfg_cmd->status = 0; |
| 509 | cfg_cmd->link = cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next])); |
| 510 | |
| 511 | memcpy (cfg_cmd->params, i82558_config_cmd, |
| 512 | sizeof (i82558_config_cmd)); |
| 513 | |
| 514 | if (!wait_for_eepro100 (dev)) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 515 | printf ("Error---CONFIG_SYS_CMD_CONFIGURE: Can not reset ethernet controller.\n"); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 516 | goto Done; |
| 517 | } |
| 518 | |
| 519 | OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer); |
| 520 | OUTW (dev, SCB_M | CU_START, SCBCmd); |
| 521 | |
| 522 | for (i = 0; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 523 | !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 524 | i++) { |
| 525 | if (i >= TOUT_LOOP) { |
| 526 | printf ("%s: Tx error buffer not ready\n", dev->name); |
| 527 | goto Done; |
| 528 | } |
| 529 | } |
| 530 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 531 | if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) { |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 532 | printf ("TX error status = 0x%08X\n", |
| 533 | le16_to_cpu (tx_ring[tx_cur].status)); |
| 534 | goto Done; |
| 535 | } |
| 536 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 537 | /* Send the Individual Address Setup frame */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 538 | tx_cur = tx_next; |
| 539 | tx_next = ((tx_next + 1) % NUM_TX_DESC); |
| 540 | |
| 541 | ias_cmd = (struct descriptor *) &tx_ring[tx_cur]; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 542 | ias_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_IAS)); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 543 | ias_cmd->status = 0; |
| 544 | ias_cmd->link = cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next])); |
| 545 | |
| 546 | memcpy (ias_cmd->params, dev->enetaddr, 6); |
| 547 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 548 | /* Tell the adapter where the TX ring is located. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 549 | if (!wait_for_eepro100 (dev)) { |
| 550 | printf ("Error: Can not reset ethernet controller.\n"); |
| 551 | goto Done; |
| 552 | } |
| 553 | |
| 554 | OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer); |
| 555 | OUTW (dev, SCB_M | CU_START, SCBCmd); |
| 556 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 557 | for (i = 0; !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 558 | i++) { |
| 559 | if (i >= TOUT_LOOP) { |
| 560 | printf ("%s: Tx error buffer not ready\n", |
| 561 | dev->name); |
| 562 | goto Done; |
| 563 | } |
| 564 | } |
| 565 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 566 | if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) { |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 567 | printf ("TX error status = 0x%08X\n", |
| 568 | le16_to_cpu (tx_ring[tx_cur].status)); |
| 569 | goto Done; |
| 570 | } |
| 571 | |
Ben Warren | 422b1a0 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 572 | status = 0; |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 573 | |
| 574 | Done: |
| 575 | return status; |
| 576 | } |
| 577 | |
Joe Hershberger | bccbe61 | 2012-05-21 14:45:25 +0000 | [diff] [blame] | 578 | static int eepro100_send(struct eth_device *dev, void *packet, int length) |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 579 | { |
| 580 | int i, status = -1; |
| 581 | int tx_cur; |
| 582 | |
| 583 | if (length <= 0) { |
| 584 | printf ("%s: bad packet size: %d\n", dev->name, length); |
| 585 | goto Done; |
| 586 | } |
| 587 | |
| 588 | tx_cur = tx_next; |
| 589 | tx_next = (tx_next + 1) % NUM_TX_DESC; |
| 590 | |
| 591 | tx_ring[tx_cur].command = cpu_to_le16 ( TxCB_CMD_TRANSMIT | |
| 592 | TxCB_CMD_SF | |
| 593 | TxCB_CMD_S | |
| 594 | TxCB_CMD_EL ); |
| 595 | tx_ring[tx_cur].status = 0; |
| 596 | tx_ring[tx_cur].count = cpu_to_le32 (tx_threshold); |
| 597 | tx_ring[tx_cur].link = |
| 598 | cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next])); |
| 599 | tx_ring[tx_cur].tx_desc_addr = |
| 600 | cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_cur].tx_buf_addr0)); |
| 601 | tx_ring[tx_cur].tx_buf_addr0 = |
| 602 | cpu_to_le32 (phys_to_bus ((u_long) packet)); |
| 603 | tx_ring[tx_cur].tx_buf_size0 = cpu_to_le32 (length); |
| 604 | |
| 605 | if (!wait_for_eepro100 (dev)) { |
| 606 | printf ("%s: Tx error ethernet controller not ready.\n", |
| 607 | dev->name); |
| 608 | goto Done; |
| 609 | } |
| 610 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 611 | /* Send the packet. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 612 | OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer); |
| 613 | OUTW (dev, SCB_M | CU_START, SCBCmd); |
| 614 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 615 | for (i = 0; !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 616 | i++) { |
| 617 | if (i >= TOUT_LOOP) { |
| 618 | printf ("%s: Tx error buffer not ready\n", dev->name); |
| 619 | goto Done; |
| 620 | } |
| 621 | } |
| 622 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 623 | if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) { |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 624 | printf ("TX error status = 0x%08X\n", |
| 625 | le16_to_cpu (tx_ring[tx_cur].status)); |
| 626 | goto Done; |
| 627 | } |
| 628 | |
| 629 | status = length; |
| 630 | |
| 631 | Done: |
| 632 | return status; |
| 633 | } |
| 634 | |
| 635 | static int eepro100_recv (struct eth_device *dev) |
| 636 | { |
| 637 | u16 status, stat; |
| 638 | int rx_prev, length = 0; |
| 639 | |
| 640 | stat = INW (dev, SCBStatus); |
| 641 | OUTW (dev, stat & SCB_STATUS_RNR, SCBStatus); |
| 642 | |
| 643 | for (;;) { |
| 644 | status = le16_to_cpu (rx_ring[rx_next].status); |
| 645 | |
| 646 | if (!(status & RFD_STATUS_C)) { |
| 647 | break; |
| 648 | } |
| 649 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 650 | /* Valid frame status. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 651 | if ((status & RFD_STATUS_OK)) { |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 652 | /* A valid frame received. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 653 | length = le32_to_cpu (rx_ring[rx_next].count) & 0x3fff; |
| 654 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 655 | /* Pass the packet up to the protocol layers. */ |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 656 | net_process_received_packet((u8 *)rx_ring[rx_next].data, |
| 657 | length); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 658 | } else { |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 659 | /* There was an error. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 660 | printf ("RX error status = 0x%08X\n", status); |
| 661 | } |
| 662 | |
| 663 | rx_ring[rx_next].control = cpu_to_le16 (RFD_CONTROL_S); |
| 664 | rx_ring[rx_next].status = 0; |
| 665 | rx_ring[rx_next].count = cpu_to_le32 (PKTSIZE_ALIGN << 16); |
| 666 | |
| 667 | rx_prev = (rx_next + NUM_RX_DESC - 1) % NUM_RX_DESC; |
| 668 | rx_ring[rx_prev].control = 0; |
| 669 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 670 | /* Update entry information. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 671 | rx_next = (rx_next + 1) % NUM_RX_DESC; |
| 672 | } |
| 673 | |
| 674 | if (stat & SCB_STATUS_RNR) { |
| 675 | |
| 676 | printf ("%s: Receiver is not ready, restart it !\n", dev->name); |
| 677 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 678 | /* Reinitialize Rx ring. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 679 | init_rx_ring (dev); |
| 680 | |
| 681 | if (!wait_for_eepro100 (dev)) { |
| 682 | printf ("Error: Can not restart ethernet controller.\n"); |
| 683 | goto Done; |
| 684 | } |
| 685 | |
| 686 | OUTL (dev, phys_to_bus ((u32) & rx_ring[rx_next]), SCBPointer); |
| 687 | OUTW (dev, SCB_M | RUC_START, SCBCmd); |
| 688 | } |
| 689 | |
| 690 | Done: |
| 691 | return length; |
| 692 | } |
| 693 | |
| 694 | static void eepro100_halt (struct eth_device *dev) |
| 695 | { |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 696 | /* Reset the ethernet controller */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 697 | OUTL (dev, I82559_SELECTIVE_RESET, SCBPort); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 698 | udelay(20); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 699 | |
| 700 | OUTL (dev, I82559_RESET, SCBPort); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 701 | udelay(20); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 702 | |
| 703 | if (!wait_for_eepro100 (dev)) { |
| 704 | printf ("Error: Can not reset ethernet controller.\n"); |
| 705 | goto Done; |
| 706 | } |
| 707 | OUTL (dev, 0, SCBPointer); |
| 708 | OUTW (dev, SCB_M | RUC_ADDR_LOAD, SCBCmd); |
| 709 | |
| 710 | if (!wait_for_eepro100 (dev)) { |
| 711 | printf ("Error: Can not reset ethernet controller.\n"); |
| 712 | goto Done; |
| 713 | } |
| 714 | OUTL (dev, 0, SCBPointer); |
| 715 | OUTW (dev, SCB_M | CU_ADDR_LOAD, SCBCmd); |
| 716 | |
| 717 | Done: |
| 718 | return; |
| 719 | } |
| 720 | |
Marek Vasut | aba283d | 2020-05-23 12:49:16 +0200 | [diff] [blame^] | 721 | /* SROM Read. */ |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 722 | static int read_eeprom (struct eth_device *dev, int location, int addr_len) |
| 723 | { |
| 724 | unsigned short retval = 0; |
| 725 | int read_cmd = location | EE_READ_CMD; |
| 726 | int i; |
| 727 | |
| 728 | OUTW (dev, EE_ENB & ~EE_CS, SCBeeprom); |
| 729 | OUTW (dev, EE_ENB, SCBeeprom); |
| 730 | |
| 731 | /* Shift the read command bits out. */ |
| 732 | for (i = 12; i >= 0; i--) { |
| 733 | short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0; |
| 734 | |
| 735 | OUTW (dev, EE_ENB | dataval, SCBeeprom); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 736 | udelay(1); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 737 | OUTW (dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 738 | udelay(1); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 739 | } |
| 740 | OUTW (dev, EE_ENB, SCBeeprom); |
| 741 | |
| 742 | for (i = 15; i >= 0; i--) { |
| 743 | OUTW (dev, EE_ENB | EE_SHIFT_CLK, SCBeeprom); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 744 | udelay(1); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 745 | retval = (retval << 1) | |
| 746 | ((INW (dev, SCBeeprom) & EE_DATA_READ) ? 1 : 0); |
| 747 | OUTW (dev, EE_ENB, SCBeeprom); |
Simon Glass | 07e1114 | 2020-05-10 11:40:10 -0600 | [diff] [blame] | 748 | udelay(1); |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /* Terminate the EEPROM access. */ |
| 752 | OUTW (dev, EE_ENB & ~EE_CS, SCBeeprom); |
| 753 | return retval; |
| 754 | } |
| 755 | |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 756 | static void init_rx_ring (struct eth_device *dev) |
| 757 | { |
| 758 | int i; |
| 759 | |
| 760 | for (i = 0; i < NUM_RX_DESC; i++) { |
| 761 | rx_ring[i].status = 0; |
| 762 | rx_ring[i].control = |
| 763 | (i == NUM_RX_DESC - 1) ? cpu_to_le16 (RFD_CONTROL_S) : 0; |
| 764 | rx_ring[i].link = |
| 765 | cpu_to_le32 (phys_to_bus |
| 766 | ((u32) & rx_ring[(i + 1) % NUM_RX_DESC])); |
| 767 | rx_ring[i].rx_buf_addr = 0xffffffff; |
| 768 | rx_ring[i].count = cpu_to_le32 (PKTSIZE_ALIGN << 16); |
| 769 | } |
| 770 | |
| 771 | rx_next = 0; |
| 772 | } |
| 773 | |
| 774 | static void purge_tx_ring (struct eth_device *dev) |
| 775 | { |
| 776 | int i; |
| 777 | |
| 778 | tx_next = 0; |
| 779 | tx_threshold = 0x01208000; |
| 780 | |
| 781 | for (i = 0; i < NUM_TX_DESC; i++) { |
| 782 | tx_ring[i].status = 0; |
| 783 | tx_ring[i].command = 0; |
| 784 | tx_ring[i].link = 0; |
| 785 | tx_ring[i].tx_desc_addr = 0; |
| 786 | tx_ring[i].count = 0; |
| 787 | |
| 788 | tx_ring[i].tx_buf_addr0 = 0; |
| 789 | tx_ring[i].tx_buf_size0 = 0; |
| 790 | tx_ring[i].tx_buf_addr1 = 0; |
| 791 | tx_ring[i].tx_buf_size1 = 0; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static void read_hw_addr (struct eth_device *dev, bd_t * bis) |
| 796 | { |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 797 | u16 sum = 0; |
| 798 | int i, j; |
| 799 | int addr_len = read_eeprom (dev, 0, 6) == 0xffff ? 8 : 6; |
| 800 | |
| 801 | for (j = 0, i = 0; i < 0x40; i++) { |
| 802 | u16 value = read_eeprom (dev, i, addr_len); |
| 803 | |
wdenk | 1df49e2 | 2002-09-17 21:37:55 +0000 | [diff] [blame] | 804 | sum += value; |
| 805 | if (i < 3) { |
| 806 | dev->enetaddr[j++] = value; |
| 807 | dev->enetaddr[j++] = value >> 8; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (sum != 0xBABA) { |
| 812 | memset (dev->enetaddr, 0, ETH_ALEN); |
| 813 | #ifdef DEBUG |
| 814 | printf ("%s: Invalid EEPROM checksum %#4.4x, " |
| 815 | "check settings before activating this device!\n", |
| 816 | dev->name, sum); |
| 817 | #endif |
| 818 | } |
| 819 | } |