Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | |
Marek Vasut | 02b95a4 | 2020-07-08 06:31:54 +0200 | [diff] [blame] | 3 | #include <asm/io.h> |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 4 | #include <cpu_func.h> |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 5 | #include <dm.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | #include <malloc.h> |
| 7 | #include <net.h> |
Ben Warren | 8ca0b3f | 2008-08-31 10:45:44 -0700 | [diff] [blame] | 8 | #include <netdev.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | #include <pci.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 11 | #include <linux/delay.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 12 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 13 | #define SROM_DLEVEL 0 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 14 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 15 | /* PCI Registers. */ |
| 16 | #define PCI_CFDA_PSM 0x43 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 17 | |
| 18 | #define CFRV_RN 0x000000f0 /* Revision Number */ |
| 19 | |
| 20 | #define WAKEUP 0x00 /* Power Saving Wakeup */ |
| 21 | #define SLEEP 0x80 /* Power Saving Sleep Mode */ |
| 22 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 23 | #define DC2114x_BRK 0x0020 /* CFRV break between DC21142 & DC21143 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 24 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 25 | /* Ethernet chip registers. */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 26 | #define DE4X5_BMR 0x000 /* Bus Mode Register */ |
| 27 | #define DE4X5_TPD 0x008 /* Transmit Poll Demand Reg */ |
| 28 | #define DE4X5_RRBA 0x018 /* RX Ring Base Address Reg */ |
| 29 | #define DE4X5_TRBA 0x020 /* TX Ring Base Address Reg */ |
| 30 | #define DE4X5_STS 0x028 /* Status Register */ |
| 31 | #define DE4X5_OMR 0x030 /* Operation Mode Register */ |
| 32 | #define DE4X5_SICR 0x068 /* SIA Connectivity Register */ |
| 33 | #define DE4X5_APROM 0x048 /* Ethernet Address PROM */ |
| 34 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 35 | /* Register bits. */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 36 | #define BMR_SWR 0x00000001 /* Software Reset */ |
| 37 | #define STS_TS 0x00700000 /* Transmit Process State */ |
| 38 | #define STS_RS 0x000e0000 /* Receive Process State */ |
| 39 | #define OMR_ST 0x00002000 /* Start/Stop Transmission Command */ |
| 40 | #define OMR_SR 0x00000002 /* Start/Stop Receive */ |
| 41 | #define OMR_PS 0x00040000 /* Port Select */ |
| 42 | #define OMR_SDP 0x02000000 /* SD Polarity - MUST BE ASSERTED */ |
| 43 | #define OMR_PM 0x00000080 /* Pass All Multicast */ |
| 44 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 45 | /* Descriptor bits. */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | #define R_OWN 0x80000000 /* Own Bit */ |
| 47 | #define RD_RER 0x02000000 /* Receive End Of Ring */ |
| 48 | #define RD_LS 0x00000100 /* Last Descriptor */ |
| 49 | #define RD_ES 0x00008000 /* Error Summary */ |
| 50 | #define TD_TER 0x02000000 /* Transmit End Of Ring */ |
| 51 | #define T_OWN 0x80000000 /* Own Bit */ |
| 52 | #define TD_LS 0x40000000 /* Last Segment */ |
| 53 | #define TD_FS 0x20000000 /* First Segment */ |
| 54 | #define TD_ES 0x00008000 /* Error Summary */ |
| 55 | #define TD_SET 0x08000000 /* Setup Packet */ |
| 56 | |
| 57 | /* The EEPROM commands include the alway-set leading bit. */ |
| 58 | #define SROM_WRITE_CMD 5 |
| 59 | #define SROM_READ_CMD 6 |
| 60 | #define SROM_ERASE_CMD 7 |
| 61 | |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 62 | #define SROM_HWADD 0x0014 /* Hardware Address offset in SROM */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | #define SROM_RD 0x00004000 /* Read from Boot ROM */ |
Marek Vasut | eb216f1 | 2020-04-19 03:09:26 +0200 | [diff] [blame] | 64 | #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */ |
| 65 | #define EE_WRITE_0 0x4801 |
| 66 | #define EE_WRITE_1 0x4805 |
| 67 | #define EE_DATA_READ 0x08 /* EEPROM chip data out. */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | #define SROM_SR 0x00000800 /* Select Serial ROM when set */ |
| 69 | |
| 70 | #define DT_IN 0x00000004 /* Serial Data In */ |
| 71 | #define DT_CLK 0x00000002 /* Serial ROM Clock */ |
| 72 | #define DT_CS 0x00000001 /* Serial ROM Chip Select */ |
| 73 | |
| 74 | #define POLL_DEMAND 1 |
| 75 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 76 | #if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
| 77 | #define phys_to_bus(dev, a) virt_to_phys((volatile const void *)(a)) |
| 78 | #else |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 79 | #define phys_to_bus(dev, a) dm_pci_phys_to_mem((dev), (a)) |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 80 | #endif |
Hanyuan Zhao | c303f4a | 2024-08-09 16:57:00 +0800 | [diff] [blame] | 81 | |
| 82 | /* Number of TX descriptors */ |
| 83 | #if CONFIG_IS_ENABLED(TULIP_MULTIPLE_TX_DESC) |
| 84 | #define NUM_TX_DESC 4 |
| 85 | #else |
| 86 | #define NUM_TX_DESC 1 |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 87 | #endif |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 88 | |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 89 | #define NUM_RX_DESC PKTBUFSRX |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 90 | #define RX_BUFF_SZ PKTSIZE_ALIGN |
| 91 | |
| 92 | #define TOUT_LOOP 1000000 |
| 93 | |
| 94 | #define SETUP_FRAME_LEN 192 |
| 95 | |
| 96 | struct de4x5_desc { |
| 97 | volatile s32 status; |
| 98 | u32 des1; |
| 99 | u32 buf; |
| 100 | u32 next; |
| 101 | }; |
| 102 | |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 103 | /* Assigned for network card's ring buffer: |
| 104 | * Some CPU might treat these memories as cached, and changes to these memories |
| 105 | * won't immediately be visible to each other. It is necessary to ensure that |
| 106 | * these memories between the CPU and the network card are marked as uncached. |
| 107 | */ |
| 108 | static struct de4x5_desc rx_ring[NUM_RX_DESC] __aligned(32); |
| 109 | static struct de4x5_desc tx_ring[NUM_TX_DESC] __aligned(32); |
| 110 | |
Marek Vasut | 2301a4b | 2020-07-08 06:42:07 +0200 | [diff] [blame] | 111 | struct dc2114x_priv { |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 112 | struct de4x5_desc *rx_ring; /* Must be uncached to CPU */ |
| 113 | struct de4x5_desc *tx_ring; /* Must be uncached to CPU */ |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 114 | int rx_new; /* RX descriptor ring pointer */ |
| 115 | int tx_new; /* TX descriptor ring pointer */ |
| 116 | char rx_ring_size; |
| 117 | char tx_ring_size; |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 118 | struct udevice *devno; |
Marek Vasut | 2301a4b | 2020-07-08 06:42:07 +0200 | [diff] [blame] | 119 | char *name; |
| 120 | void __iomem *iobase; |
| 121 | u8 *enetaddr; |
| 122 | }; |
| 123 | |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 124 | /* RX and TX descriptor ring */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 125 | static u32 dc2114x_inl(struct dc2114x_priv *priv, u32 addr) |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 126 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 127 | return le32_to_cpu(readl(priv->iobase + addr)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 130 | static void dc2114x_outl(struct dc2114x_priv *priv, u32 command, u32 addr) |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 131 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 132 | writel(cpu_to_le32(command), priv->iobase + addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 135 | static void reset_de4x5(struct dc2114x_priv *priv) |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 136 | { |
Marek Vasut | 3b7b9e2 | 2020-04-19 03:40:03 +0200 | [diff] [blame] | 137 | u32 i; |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 138 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 139 | i = dc2114x_inl(priv, DE4X5_BMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 140 | mdelay(1); |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 141 | dc2114x_outl(priv, i | BMR_SWR, DE4X5_BMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 142 | mdelay(1); |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 143 | dc2114x_outl(priv, i, DE4X5_BMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 144 | mdelay(1); |
| 145 | |
| 146 | for (i = 0; i < 5; i++) { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 147 | dc2114x_inl(priv, DE4X5_BMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 148 | mdelay(10); |
| 149 | } |
| 150 | |
| 151 | mdelay(1); |
| 152 | } |
| 153 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 154 | static void start_de4x5(struct dc2114x_priv *priv) |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 155 | { |
Marek Vasut | 3b7b9e2 | 2020-04-19 03:40:03 +0200 | [diff] [blame] | 156 | u32 omr; |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 157 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 158 | omr = dc2114x_inl(priv, DE4X5_OMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 159 | omr |= OMR_ST | OMR_SR; |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 160 | dc2114x_outl(priv, omr, DE4X5_OMR); /* Enable the TX and/or RX */ |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 163 | static void stop_de4x5(struct dc2114x_priv *priv) |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 164 | { |
Marek Vasut | 3b7b9e2 | 2020-04-19 03:40:03 +0200 | [diff] [blame] | 165 | u32 omr; |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 166 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 167 | omr = dc2114x_inl(priv, DE4X5_OMR); |
Marek Vasut | 04da061 | 2020-04-19 03:36:46 +0200 | [diff] [blame] | 168 | omr &= ~(OMR_ST | OMR_SR); |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 169 | dc2114x_outl(priv, omr, DE4X5_OMR); /* Disable the TX and/or RX */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Marek Vasut | 171f5e5 | 2020-04-18 01:56:51 +0200 | [diff] [blame] | 172 | /* SROM Read and write routines. */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 173 | static void sendto_srom(struct dc2114x_priv *priv, u_int command, u_long addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 174 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 175 | dc2114x_outl(priv, command, addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 176 | udelay(1); |
| 177 | } |
| 178 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 179 | static int getfrom_srom(struct dc2114x_priv *priv, u_long addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 180 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 181 | u32 tmp = dc2114x_inl(priv, addr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 182 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 183 | udelay(1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 184 | return tmp; |
| 185 | } |
| 186 | |
| 187 | /* Note: this routine returns extra data bits for size detection. */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 188 | static int do_read_eeprom(struct dc2114x_priv *priv, u_long ioaddr, int location, |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 189 | int addr_len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 190 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 191 | int read_cmd = location | (SROM_READ_CMD << addr_len); |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 192 | unsigned int retval = 0; |
| 193 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 194 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 195 | sendto_srom(priv, SROM_RD | SROM_SR, ioaddr); |
| 196 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 197 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 198 | debug_cond(SROM_DLEVEL >= 1, " EEPROM read at %d ", location); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | |
| 200 | /* Shift the read command bits out. */ |
| 201 | for (i = 4 + addr_len; i >= 0; i--) { |
| 202 | short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0; |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 203 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 204 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | dataval, |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 205 | ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 206 | udelay(10); |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 207 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK, |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 208 | ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 209 | udelay(10); |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 210 | debug_cond(SROM_DLEVEL >= 2, "%X", |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 211 | getfrom_srom(priv, ioaddr) & 15); |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 212 | retval = (retval << 1) | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 213 | !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 216 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 217 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 218 | debug_cond(SROM_DLEVEL >= 2, " :%X:", getfrom_srom(priv, ioaddr) & 15); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 219 | |
| 220 | for (i = 16; i > 0; i--) { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 221 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 222 | udelay(10); |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 223 | debug_cond(SROM_DLEVEL >= 2, "%X", |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 224 | getfrom_srom(priv, ioaddr) & 15); |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 225 | retval = (retval << 1) | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 226 | !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ); |
| 227 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 228 | udelay(10); |
| 229 | } |
| 230 | |
| 231 | /* Terminate the EEPROM access. */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 232 | sendto_srom(priv, SROM_RD | SROM_SR, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 233 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 234 | debug_cond(SROM_DLEVEL >= 2, " EEPROM value at %d is %5.5x.\n", |
| 235 | location, retval); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | |
| 237 | return retval; |
| 238 | } |
| 239 | |
Marek Vasut | 171f5e5 | 2020-04-18 01:56:51 +0200 | [diff] [blame] | 240 | /* |
| 241 | * This executes a generic EEPROM command, typically a write or write |
wdenk | c935d3b | 2004-01-03 19:43:48 +0000 | [diff] [blame] | 242 | * enable. It returns the data output from the EEPROM, and thus may |
| 243 | * also be used for reads. |
| 244 | */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 245 | static int do_eeprom_cmd(struct dc2114x_priv *priv, u_long ioaddr, int cmd, |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 246 | int cmd_len) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 247 | { |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 248 | unsigned int retval = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 249 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 250 | debug_cond(SROM_DLEVEL >= 1, " EEPROM op 0x%x: ", cmd); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 251 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 252 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 253 | |
| 254 | /* Shift the command bits out. */ |
| 255 | do { |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 256 | short dataval = (cmd & BIT(cmd_len)) ? EE_WRITE_1 : EE_WRITE_0; |
| 257 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 258 | sendto_srom(priv, dataval, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | udelay(10); |
| 260 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 261 | debug_cond(SROM_DLEVEL >= 2, "%X", |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 262 | getfrom_srom(priv, ioaddr) & 15); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 263 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 264 | sendto_srom(priv, dataval | DT_CLK, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 265 | udelay(10); |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 266 | retval = (retval << 1) | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 267 | !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 268 | } while (--cmd_len >= 0); |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 269 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 270 | sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | |
| 272 | /* Terminate the EEPROM access. */ |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 273 | sendto_srom(priv, SROM_RD | SROM_SR, ioaddr); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 274 | |
Marek Vasut | c2abfca | 2020-04-19 04:05:44 +0200 | [diff] [blame] | 275 | debug_cond(SROM_DLEVEL >= 1, " EEPROM result is 0x%5.5x.\n", retval); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 276 | |
| 277 | return retval; |
| 278 | } |
| 279 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 280 | static int read_srom(struct dc2114x_priv *priv, u_long ioaddr, int index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 281 | { |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 282 | int ee_addr_size; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 283 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 284 | ee_addr_size = (do_read_eeprom(priv, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6; |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 285 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 286 | return do_eeprom_cmd(priv, ioaddr, 0xffff | |
Marek Vasut | 2e5c2a1 | 2020-04-19 03:11:06 +0200 | [diff] [blame] | 287 | (((SROM_READ_CMD << ee_addr_size) | index) << 16), |
| 288 | 3 + ee_addr_size + 16); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Marek Vasut | bc4666a | 2020-07-08 07:20:14 +0200 | [diff] [blame] | 291 | static void send_setup_frame(struct dc2114x_priv *priv) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 292 | { |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 293 | /* We are writing setup frame and these changes should be visible to the |
| 294 | * network card immediately. So let's directly read/write through the |
| 295 | * uncached window. |
| 296 | */ |
| 297 | char __setup_frame[SETUP_FRAME_LEN] __aligned(32); |
| 298 | char *setup_frame = (char *)map_physmem((phys_addr_t)virt_to_phys(__setup_frame), 0, MAP_NOCACHE); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 299 | char *pa = &setup_frame[0]; |
| 300 | int i; |
| 301 | |
| 302 | memset(pa, 0xff, SETUP_FRAME_LEN); |
| 303 | |
| 304 | for (i = 0; i < ETH_ALEN; i++) { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 305 | *(pa + (i & 1)) = priv->enetaddr[i]; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 306 | if (i & 0x01) |
| 307 | pa += 4; |
| 308 | } |
| 309 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 310 | for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) { |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 311 | if (i < TOUT_LOOP) |
| 312 | continue; |
| 313 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 314 | printf("%s: tx error buffer not ready\n", priv->name); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 318 | priv->tx_ring[priv->tx_new].buf = cpu_to_le32(phys_to_bus(priv->devno, |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 319 | (phys_addr_t)&setup_frame[0])); |
Hanyuan Zhao | c303f4a | 2024-08-09 16:57:00 +0800 | [diff] [blame] | 320 | #if CONFIG_IS_ENABLED(TULIP_MULTIPLE_TX_DESC) |
| 321 | priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_SET | SETUP_FRAME_LEN); |
| 322 | priv->tx_ring[priv->tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER); |
| 323 | #else |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 324 | priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_TER | TD_SET | SETUP_FRAME_LEN); |
Hanyuan Zhao | c303f4a | 2024-08-09 16:57:00 +0800 | [diff] [blame] | 325 | #endif |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 326 | priv->tx_ring[priv->tx_new].status = cpu_to_le32(T_OWN); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 327 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 328 | dc2114x_outl(priv, POLL_DEMAND, DE4X5_TPD); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 329 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 330 | for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) { |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 331 | if (i < TOUT_LOOP) |
| 332 | continue; |
| 333 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 334 | printf("%s: tx buffer not ready\n", priv->name); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 338 | if (le32_to_cpu(priv->tx_ring[priv->tx_new].status) != 0x7FFFFFFF) { |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 339 | debug("TX error status2 = 0x%08X\n", |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 340 | le32_to_cpu(priv->tx_ring[priv->tx_new].status)); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 343 | priv->tx_new = (priv->tx_new + 1) % NUM_TX_DESC; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Marek Vasut | bc4666a | 2020-07-08 07:20:14 +0200 | [diff] [blame] | 346 | static int dc21x4x_send_common(struct dc2114x_priv *priv, void *packet, int length) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 347 | { |
| 348 | int status = -1; |
| 349 | int i; |
| 350 | |
| 351 | if (length <= 0) { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 352 | printf("%s: bad packet size: %d\n", priv->name, length); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 353 | goto done; |
| 354 | } |
| 355 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 356 | for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) { |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 357 | if (i < TOUT_LOOP) |
| 358 | continue; |
| 359 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 360 | printf("%s: tx error buffer not ready\n", priv->name); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 361 | goto done; |
| 362 | } |
| 363 | |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 364 | /* Packet should be visible to the network card */ |
| 365 | flush_dcache_range((phys_addr_t)packet, (phys_addr_t)(packet + RX_BUFF_SZ)); |
| 366 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 367 | priv->tx_ring[priv->tx_new].buf = cpu_to_le32(phys_to_bus(priv->devno, |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 368 | (phys_addr_t)packet)); |
Hanyuan Zhao | c303f4a | 2024-08-09 16:57:00 +0800 | [diff] [blame] | 369 | #if CONFIG_IS_ENABLED(TULIP_MULTIPLE_TX_DESC) |
| 370 | priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_LS | TD_FS | length); |
| 371 | priv->tx_ring[priv->tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER); |
| 372 | #else |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 373 | priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_TER | TD_LS | TD_FS | length); |
Hanyuan Zhao | c303f4a | 2024-08-09 16:57:00 +0800 | [diff] [blame] | 374 | #endif |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 375 | priv->tx_ring[priv->tx_new].status = cpu_to_le32(T_OWN); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 376 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 377 | dc2114x_outl(priv, POLL_DEMAND, DE4X5_TPD); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 378 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 379 | for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) { |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 380 | if (i < TOUT_LOOP) |
| 381 | continue; |
| 382 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 383 | printf(".%s: tx buffer not ready\n", priv->name); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 384 | goto done; |
| 385 | } |
| 386 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 387 | if (le32_to_cpu(priv->tx_ring[priv->tx_new].status) & TD_ES) { |
| 388 | priv->tx_ring[priv->tx_new].status = 0x0; |
Hanyuan Zhao | 5fa3e10 | 2024-08-09 16:56:59 +0800 | [diff] [blame] | 389 | #if !CONFIG_IS_ENABLED(TULIP_IGNORE_TX_NO_CARRIER) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 390 | goto done; |
Hanyuan Zhao | 5fa3e10 | 2024-08-09 16:56:59 +0800 | [diff] [blame] | 391 | #endif |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | status = length; |
| 395 | |
| 396 | done: |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 397 | priv->tx_new = (priv->tx_new + 1) % NUM_TX_DESC; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 398 | return status; |
| 399 | } |
| 400 | |
Marek Vasut | 05c4917 | 2020-07-08 07:12:58 +0200 | [diff] [blame] | 401 | static int dc21x4x_recv_check(struct dc2114x_priv *priv) |
| 402 | { |
| 403 | int length = 0; |
| 404 | u32 status; |
| 405 | |
| 406 | status = le32_to_cpu(priv->rx_ring[priv->rx_new].status); |
| 407 | |
| 408 | if (status & R_OWN) |
| 409 | return 0; |
| 410 | |
| 411 | if (status & RD_LS) { |
| 412 | /* Valid frame status. */ |
| 413 | if (status & RD_ES) { |
| 414 | /* There was an error. */ |
| 415 | printf("RX error status = 0x%08X\n", status); |
| 416 | return -EINVAL; |
| 417 | } else { |
| 418 | /* A valid frame received. */ |
| 419 | length = (le32_to_cpu(priv->rx_ring[priv->rx_new].status) |
| 420 | >> 16); |
| 421 | |
| 422 | return length; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return -EAGAIN; |
| 427 | } |
| 428 | |
Marek Vasut | bc4666a | 2020-07-08 07:20:14 +0200 | [diff] [blame] | 429 | static int dc21x4x_init_common(struct dc2114x_priv *priv) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 430 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 431 | int i; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 432 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 433 | reset_de4x5(priv); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 434 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 435 | if (dc2114x_inl(priv, DE4X5_STS) & (STS_TS | STS_RS)) { |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 436 | printf("Error: Cannot reset ethernet controller.\n"); |
| 437 | return -1; |
| 438 | } |
| 439 | |
Hanyuan Zhao | ba30f46 | 2024-08-09 16:57:01 +0800 | [diff] [blame] | 440 | /* 2024-07: |
| 441 | * Remove the OMR_PM flag and choose 16 perfect filtering mode since in |
| 442 | * modern networks there're plenty of multicasts and set ORM_PM flag will |
| 443 | * increase the dc2114x's workload and ask the U-Boot to handle packets |
| 444 | * not related to itself. And most of the time, U-Boot does not need this |
| 445 | * feature. |
| 446 | * |
| 447 | * A better way: let user to decide whether to have this flag. |
| 448 | */ |
| 449 | dc2114x_outl(priv, OMR_SDP | OMR_PS, DE4X5_OMR); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 450 | |
| 451 | for (i = 0; i < NUM_RX_DESC; i++) { |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 452 | priv->rx_ring[i].status = cpu_to_le32(R_OWN); |
| 453 | priv->rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ); |
| 454 | priv->rx_ring[i].buf = cpu_to_le32(phys_to_bus(priv->devno, |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 455 | (phys_addr_t)net_rx_packets[i])); |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 456 | priv->rx_ring[i].next = 0; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | for (i = 0; i < NUM_TX_DESC; i++) { |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 460 | priv->tx_ring[i].status = 0; |
| 461 | priv->tx_ring[i].des1 = 0; |
| 462 | priv->tx_ring[i].buf = 0; |
| 463 | priv->tx_ring[i].next = 0; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 464 | } |
| 465 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 466 | priv->rx_ring_size = NUM_RX_DESC; |
| 467 | priv->tx_ring_size = NUM_TX_DESC; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 468 | |
| 469 | /* Write the end of list marker to the descriptor lists. */ |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 470 | priv->rx_ring[priv->rx_ring_size - 1].des1 |= cpu_to_le32(RD_RER); |
| 471 | priv->tx_ring[priv->tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 472 | |
| 473 | /* Tell the adapter where the TX/RX rings are located. */ |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 474 | dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->rx_ring), |
Marek Vasut | 8a5c6f1 | 2020-07-08 06:50:41 +0200 | [diff] [blame] | 475 | DE4X5_RRBA); |
Hanyuan Zhao | 8c18c53 | 2024-08-09 16:56:58 +0800 | [diff] [blame] | 476 | dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->tx_ring), |
Marek Vasut | 8a5c6f1 | 2020-07-08 06:50:41 +0200 | [diff] [blame] | 477 | DE4X5_TRBA); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 478 | |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 479 | start_de4x5(priv); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 480 | |
Marek Vasut | 32d8d11 | 2020-07-08 07:01:32 +0200 | [diff] [blame] | 481 | priv->tx_new = 0; |
| 482 | priv->rx_new = 0; |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 483 | |
Marek Vasut | bc4666a | 2020-07-08 07:20:14 +0200 | [diff] [blame] | 484 | send_setup_frame(priv); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Marek Vasut | bc4666a | 2020-07-08 07:20:14 +0200 | [diff] [blame] | 489 | static void dc21x4x_halt_common(struct dc2114x_priv *priv) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 490 | { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 491 | stop_de4x5(priv); |
| 492 | dc2114x_outl(priv, 0, DE4X5_SICR); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Marek Vasut | 2301a4b | 2020-07-08 06:42:07 +0200 | [diff] [blame] | 495 | static void read_hw_addr(struct dc2114x_priv *priv) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 496 | { |
Marek Vasut | 2301a4b | 2020-07-08 06:42:07 +0200 | [diff] [blame] | 497 | u_short tmp, *p = (u_short *)(&priv->enetaddr[0]); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 498 | int i, j = 0; |
| 499 | |
| 500 | for (i = 0; i < (ETH_ALEN >> 1); i++) { |
Marek Vasut | fcd6217 | 2020-07-08 06:46:09 +0200 | [diff] [blame] | 501 | tmp = read_srom(priv, DE4X5_APROM, (SROM_HWADD >> 1) + i); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 502 | *p = le16_to_cpu(tmp); |
| 503 | j += *p++; |
| 504 | } |
| 505 | |
| 506 | if (!j || j == 0x2fffd) { |
Marek Vasut | 2301a4b | 2020-07-08 06:42:07 +0200 | [diff] [blame] | 507 | memset(priv->enetaddr, 0, ETH_ALEN); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 508 | debug("Warning: can't read HW address from SROM.\n"); |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 512 | #if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 513 | static struct pci_device_id supported[] = { |
Marek Vasut | 75e375b | 2020-06-20 17:36:42 +0200 | [diff] [blame] | 514 | { PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST) }, |
| 515 | { PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142) }, |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 516 | { } |
| 517 | }; |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 518 | #endif |
Marek Vasut | dbe9c0c | 2020-04-19 04:00:49 +0200 | [diff] [blame] | 519 | |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 520 | static int dc2114x_start(struct udevice *dev) |
| 521 | { |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 522 | struct dc2114x_priv *priv = dev_get_priv(dev); |
Hanyuan Zhao | a35aa5a | 2024-08-09 16:56:55 +0800 | [diff] [blame] | 523 | int rval; |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 524 | |
Tom Rini | 568407f | 2024-10-27 10:15:43 -0600 | [diff] [blame] | 525 | if (!priv->enetaddr) { |
Hanyuan Zhao | a35aa5a | 2024-08-09 16:56:55 +0800 | [diff] [blame] | 526 | rval = eth_env_get_enetaddr("ethaddr", priv->enetaddr); |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 527 | |
Hanyuan Zhao | a35aa5a | 2024-08-09 16:56:55 +0800 | [diff] [blame] | 528 | if (!rval) { |
| 529 | printf("dc2114x: Err: please set a valid MAC address\n"); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | } |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 533 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 534 | #if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 535 | /* Ensure we're not sleeping. */ |
| 536 | dm_pci_write_config8(dev, PCI_CFDA_PSM, WAKEUP); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 537 | #endif |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 538 | |
| 539 | return dc21x4x_init_common(priv); |
| 540 | } |
| 541 | |
| 542 | static void dc2114x_stop(struct udevice *dev) |
| 543 | { |
| 544 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 545 | |
| 546 | dc21x4x_halt_common(priv); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 547 | #if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 548 | dm_pci_write_config8(dev, PCI_CFDA_PSM, SLEEP); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 549 | #endif |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static int dc2114x_send(struct udevice *dev, void *packet, int length) |
| 553 | { |
| 554 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 555 | int ret; |
| 556 | |
| 557 | ret = dc21x4x_send_common(priv, packet, length); |
| 558 | |
| 559 | return ret ? 0 : -ETIMEDOUT; |
| 560 | } |
| 561 | |
| 562 | static int dc2114x_recv(struct udevice *dev, int flags, uchar **packetp) |
| 563 | { |
| 564 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 565 | int ret; |
| 566 | |
| 567 | ret = dc21x4x_recv_check(priv); |
| 568 | |
| 569 | if (ret < 0) { |
| 570 | /* Update entry information. */ |
| 571 | priv->rx_new = (priv->rx_new + 1) % priv->rx_ring_size; |
| 572 | ret = 0; |
| 573 | } |
| 574 | |
| 575 | if (!ret) |
| 576 | return 0; |
| 577 | |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 578 | invalidate_dcache_range((phys_addr_t)net_rx_packets[priv->rx_new], (phys_addr_t)(net_rx_packets[priv->rx_new] + RX_BUFF_SZ)); |
| 579 | *packetp = (uchar *)net_rx_packets[priv->rx_new]; |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 580 | |
| 581 | return ret - 4; |
| 582 | } |
| 583 | |
| 584 | static int dc2114x_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 585 | { |
| 586 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 587 | |
| 588 | priv->rx_ring[priv->rx_new].status = cpu_to_le32(R_OWN); |
| 589 | |
| 590 | /* Update entry information. */ |
| 591 | priv->rx_new = (priv->rx_new + 1) % priv->rx_ring_size; |
| 592 | |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static int dc2114x_read_rom_hwaddr(struct udevice *dev) |
| 597 | { |
| 598 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 599 | |
| 600 | read_hw_addr(priv); |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static int dc2114x_bind(struct udevice *dev) |
| 606 | { |
Hanyuan Zhao | 26d88de | 2024-08-09 16:56:56 +0800 | [diff] [blame] | 607 | static int card_number = 0; |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 608 | char name[16]; |
| 609 | |
| 610 | sprintf(name, "dc2114x#%u", card_number++); |
| 611 | |
| 612 | return device_set_name(dev, name); |
| 613 | } |
| 614 | |
| 615 | static int dc2114x_probe(struct udevice *dev) |
| 616 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 617 | struct eth_pdata *plat = dev_get_plat(dev); |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 618 | struct dc2114x_priv *priv = dev_get_priv(dev); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 619 | |
| 620 | #if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 621 | u16 command, status; |
| 622 | u32 iobase; |
| 623 | |
| 624 | dm_pci_read_config32(dev, PCI_BASE_ADDRESS_1, &iobase); |
| 625 | iobase &= ~0xf; |
| 626 | |
| 627 | debug("dc2114x: DEC 2114x PCI Device @0x%x\n", iobase); |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 628 | priv->iobase = (void __iomem *)dm_pci_mem_to_phys(dev, iobase); |
| 629 | |
| 630 | command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER; |
| 631 | dm_pci_write_config16(dev, PCI_COMMAND, command); |
| 632 | dm_pci_read_config16(dev, PCI_COMMAND, &status); |
| 633 | if ((status & command) != command) { |
| 634 | printf("dc2114x: Couldn't enable IO access or Bus Mastering\n"); |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | |
| 638 | dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x60); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 639 | #endif |
Hanyuan Zhao | a35aa5a | 2024-08-09 16:56:55 +0800 | [diff] [blame] | 640 | |
| 641 | priv->devno = dev; |
| 642 | priv->enetaddr = plat->enetaddr; |
Hanyuan Zhao | 23edc8f | 2024-08-09 16:56:57 +0800 | [diff] [blame] | 643 | priv->rx_ring = (struct de4x5_desc *)map_physmem((phys_addr_t)virt_to_phys(rx_ring), 0, MAP_NOCACHE); |
| 644 | priv->tx_ring = (struct de4x5_desc *)map_physmem((phys_addr_t)virt_to_phys(tx_ring), 0, MAP_NOCACHE); |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 649 | #if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
| 650 | static int dc2114x_of_to_plat(struct udevice *dev) |
| 651 | { |
| 652 | struct eth_pdata *plat = dev_get_plat(dev); |
| 653 | struct dc2114x_priv *priv = dev_get_priv(dev); |
| 654 | |
| 655 | plat->iobase = (phys_addr_t)map_physmem((phys_addr_t)devfdt_get_addr(dev), 0, MAP_NOCACHE); |
Tom Rini | 568407f | 2024-10-27 10:15:43 -0600 | [diff] [blame] | 656 | priv->iobase = (void *)plat->iobase; |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 657 | |
| 658 | return 0; |
| 659 | } |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 660 | #endif |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 661 | |
| 662 | static const struct eth_ops dc2114x_ops = { |
| 663 | .start = dc2114x_start, |
| 664 | .send = dc2114x_send, |
| 665 | .recv = dc2114x_recv, |
| 666 | .stop = dc2114x_stop, |
| 667 | .free_pkt = dc2114x_free_pkt, |
| 668 | .read_rom_hwaddr = dc2114x_read_rom_hwaddr, |
| 669 | }; |
| 670 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 671 | #if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
| 672 | static const struct udevice_id dc2114x_eth_ids[] = { |
| 673 | { .compatible = "dec,dmfe" }, |
| 674 | { .compatible = "tulip,dmfe" }, |
| 675 | { .compatible = "dec,dc2114x" }, |
| 676 | { .compatible = "tulip,dc2114x" }, |
| 677 | { } |
| 678 | }; |
| 679 | #endif |
| 680 | |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 681 | U_BOOT_DRIVER(eth_dc2114x) = { |
| 682 | .name = "eth_dc2114x", |
| 683 | .id = UCLASS_ETH, |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 684 | #if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
| 685 | .of_match = dc2114x_eth_ids, |
| 686 | .of_to_plat = dc2114x_of_to_plat, |
| 687 | #endif |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 688 | .bind = dc2114x_bind, |
| 689 | .probe = dc2114x_probe, |
| 690 | .ops = &dc2114x_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 691 | .priv_auto = sizeof(struct dc2114x_priv), |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 692 | .plat_auto = sizeof(struct eth_pdata), |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 693 | }; |
| 694 | |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 695 | #if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI) |
Marek Vasut | f23a785 | 2020-07-08 07:26:14 +0200 | [diff] [blame] | 696 | U_BOOT_PCI_DEVICE(eth_dc2114x, supported); |
Hanyuan Zhao | 76146b9 | 2024-08-09 16:56:54 +0800 | [diff] [blame] | 697 | #endif |