blob: 7c0665faa8e12572acd10f2a3063f6cd9c8ab28a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002
Marek Vasut02b95a42020-07-08 06:31:54 +02003#include <asm/io.h>
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +08004#include <cpu_func.h>
Marek Vasutf23a7852020-07-08 07:26:14 +02005#include <dm.h>
wdenkc6097192002-11-03 00:24:07 +00006#include <malloc.h>
7#include <net.h>
Ben Warren8ca0b3f2008-08-31 10:45:44 -07008#include <netdev.h>
wdenkc6097192002-11-03 00:24:07 +00009#include <pci.h>
Simon Glasscd93d622020-05-10 11:40:13 -060010#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060011#include <linux/delay.h>
wdenkc6097192002-11-03 00:24:07 +000012
Marek Vasutc2abfca2020-04-19 04:05:44 +020013#define SROM_DLEVEL 0
wdenkc6097192002-11-03 00:24:07 +000014
Marek Vasuteb216f12020-04-19 03:09:26 +020015/* PCI Registers. */
16#define PCI_CFDA_PSM 0x43
wdenkc6097192002-11-03 00:24:07 +000017
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 Vasuteb216f12020-04-19 03:09:26 +020023#define DC2114x_BRK 0x0020 /* CFRV break between DC21142 & DC21143 */
wdenkc6097192002-11-03 00:24:07 +000024
Marek Vasuteb216f12020-04-19 03:09:26 +020025/* Ethernet chip registers. */
wdenkc6097192002-11-03 00:24:07 +000026#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 Vasuteb216f12020-04-19 03:09:26 +020035/* Register bits. */
wdenkc6097192002-11-03 00:24:07 +000036#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 Vasuteb216f12020-04-19 03:09:26 +020045/* Descriptor bits. */
wdenkc6097192002-11-03 00:24:07 +000046#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 Vasuteb216f12020-04-19 03:09:26 +020062#define SROM_HWADD 0x0014 /* Hardware Address offset in SROM */
wdenkc6097192002-11-03 00:24:07 +000063#define SROM_RD 0x00004000 /* Read from Boot ROM */
Marek Vasuteb216f12020-04-19 03:09:26 +020064#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. */
wdenkc6097192002-11-03 00:24:07 +000068#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 Zhao76146b92024-08-09 16:56:54 +080076#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 Vasutf23a7852020-07-08 07:26:14 +020079#define phys_to_bus(dev, a) dm_pci_phys_to_mem((dev), (a))
Hanyuan Zhao76146b92024-08-09 16:56:54 +080080#endif
Hanyuan Zhaoc303f4a2024-08-09 16:57:00 +080081
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 Zhao76146b92024-08-09 16:56:54 +080087#endif
Marek Vasut04da0612020-04-19 03:36:46 +020088
Marek Vasutdbe9c0c2020-04-19 04:00:49 +020089#define NUM_RX_DESC PKTBUFSRX
Marek Vasutdbe9c0c2020-04-19 04:00:49 +020090#define RX_BUFF_SZ PKTSIZE_ALIGN
91
92#define TOUT_LOOP 1000000
93
94#define SETUP_FRAME_LEN 192
95
96struct de4x5_desc {
97 volatile s32 status;
98 u32 des1;
99 u32 buf;
100 u32 next;
101};
102
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800103/* 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 */
108static struct de4x5_desc rx_ring[NUM_RX_DESC] __aligned(32);
109static struct de4x5_desc tx_ring[NUM_TX_DESC] __aligned(32);
110
Marek Vasut2301a4b2020-07-08 06:42:07 +0200111struct dc2114x_priv {
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800112 struct de4x5_desc *rx_ring; /* Must be uncached to CPU */
113 struct de4x5_desc *tx_ring; /* Must be uncached to CPU */
Marek Vasut32d8d112020-07-08 07:01:32 +0200114 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 Vasutf23a7852020-07-08 07:26:14 +0200118 struct udevice *devno;
Marek Vasut2301a4b2020-07-08 06:42:07 +0200119 char *name;
120 void __iomem *iobase;
121 u8 *enetaddr;
122};
123
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200124/* RX and TX descriptor ring */
Marek Vasutfcd62172020-07-08 06:46:09 +0200125static u32 dc2114x_inl(struct dc2114x_priv *priv, u32 addr)
Marek Vasut04da0612020-04-19 03:36:46 +0200126{
Marek Vasutfcd62172020-07-08 06:46:09 +0200127 return le32_to_cpu(readl(priv->iobase + addr));
wdenkc6097192002-11-03 00:24:07 +0000128}
129
Marek Vasutfcd62172020-07-08 06:46:09 +0200130static void dc2114x_outl(struct dc2114x_priv *priv, u32 command, u32 addr)
Marek Vasut04da0612020-04-19 03:36:46 +0200131{
Marek Vasutfcd62172020-07-08 06:46:09 +0200132 writel(cpu_to_le32(command), priv->iobase + addr);
wdenkc6097192002-11-03 00:24:07 +0000133}
134
Marek Vasutfcd62172020-07-08 06:46:09 +0200135static void reset_de4x5(struct dc2114x_priv *priv)
Marek Vasut04da0612020-04-19 03:36:46 +0200136{
Marek Vasut3b7b9e22020-04-19 03:40:03 +0200137 u32 i;
Marek Vasut04da0612020-04-19 03:36:46 +0200138
Marek Vasutfcd62172020-07-08 06:46:09 +0200139 i = dc2114x_inl(priv, DE4X5_BMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200140 mdelay(1);
Marek Vasutfcd62172020-07-08 06:46:09 +0200141 dc2114x_outl(priv, i | BMR_SWR, DE4X5_BMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200142 mdelay(1);
Marek Vasutfcd62172020-07-08 06:46:09 +0200143 dc2114x_outl(priv, i, DE4X5_BMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200144 mdelay(1);
145
146 for (i = 0; i < 5; i++) {
Marek Vasutfcd62172020-07-08 06:46:09 +0200147 dc2114x_inl(priv, DE4X5_BMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200148 mdelay(10);
149 }
150
151 mdelay(1);
152}
153
Marek Vasutfcd62172020-07-08 06:46:09 +0200154static void start_de4x5(struct dc2114x_priv *priv)
Marek Vasut04da0612020-04-19 03:36:46 +0200155{
Marek Vasut3b7b9e22020-04-19 03:40:03 +0200156 u32 omr;
Marek Vasut04da0612020-04-19 03:36:46 +0200157
Marek Vasutfcd62172020-07-08 06:46:09 +0200158 omr = dc2114x_inl(priv, DE4X5_OMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200159 omr |= OMR_ST | OMR_SR;
Marek Vasutfcd62172020-07-08 06:46:09 +0200160 dc2114x_outl(priv, omr, DE4X5_OMR); /* Enable the TX and/or RX */
Marek Vasut04da0612020-04-19 03:36:46 +0200161}
162
Marek Vasutfcd62172020-07-08 06:46:09 +0200163static void stop_de4x5(struct dc2114x_priv *priv)
Marek Vasut04da0612020-04-19 03:36:46 +0200164{
Marek Vasut3b7b9e22020-04-19 03:40:03 +0200165 u32 omr;
Marek Vasut04da0612020-04-19 03:36:46 +0200166
Marek Vasutfcd62172020-07-08 06:46:09 +0200167 omr = dc2114x_inl(priv, DE4X5_OMR);
Marek Vasut04da0612020-04-19 03:36:46 +0200168 omr &= ~(OMR_ST | OMR_SR);
Marek Vasutfcd62172020-07-08 06:46:09 +0200169 dc2114x_outl(priv, omr, DE4X5_OMR); /* Disable the TX and/or RX */
wdenkc6097192002-11-03 00:24:07 +0000170}
171
Marek Vasut171f5e52020-04-18 01:56:51 +0200172/* SROM Read and write routines. */
Marek Vasutfcd62172020-07-08 06:46:09 +0200173static void sendto_srom(struct dc2114x_priv *priv, u_int command, u_long addr)
wdenkc6097192002-11-03 00:24:07 +0000174{
Marek Vasutfcd62172020-07-08 06:46:09 +0200175 dc2114x_outl(priv, command, addr);
wdenkc6097192002-11-03 00:24:07 +0000176 udelay(1);
177}
178
Marek Vasutfcd62172020-07-08 06:46:09 +0200179static int getfrom_srom(struct dc2114x_priv *priv, u_long addr)
wdenkc6097192002-11-03 00:24:07 +0000180{
Marek Vasutfcd62172020-07-08 06:46:09 +0200181 u32 tmp = dc2114x_inl(priv, addr);
wdenkc6097192002-11-03 00:24:07 +0000182
wdenkc6097192002-11-03 00:24:07 +0000183 udelay(1);
wdenkc6097192002-11-03 00:24:07 +0000184 return tmp;
185}
186
187/* Note: this routine returns extra data bits for size detection. */
Marek Vasutfcd62172020-07-08 06:46:09 +0200188static int do_read_eeprom(struct dc2114x_priv *priv, u_long ioaddr, int location,
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200189 int addr_len)
wdenkc6097192002-11-03 00:24:07 +0000190{
wdenkc6097192002-11-03 00:24:07 +0000191 int read_cmd = location | (SROM_READ_CMD << addr_len);
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200192 unsigned int retval = 0;
193 int i;
wdenkc6097192002-11-03 00:24:07 +0000194
Marek Vasutfcd62172020-07-08 06:46:09 +0200195 sendto_srom(priv, SROM_RD | SROM_SR, ioaddr);
196 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000197
Marek Vasutc2abfca2020-04-19 04:05:44 +0200198 debug_cond(SROM_DLEVEL >= 1, " EEPROM read at %d ", location);
wdenkc6097192002-11-03 00:24:07 +0000199
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 Vasut2e5c2a12020-04-19 03:11:06 +0200203
Marek Vasutfcd62172020-07-08 06:46:09 +0200204 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | dataval,
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200205 ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000206 udelay(10);
Marek Vasutfcd62172020-07-08 06:46:09 +0200207 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK,
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200208 ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000209 udelay(10);
Marek Vasutc2abfca2020-04-19 04:05:44 +0200210 debug_cond(SROM_DLEVEL >= 2, "%X",
Marek Vasutfcd62172020-07-08 06:46:09 +0200211 getfrom_srom(priv, ioaddr) & 15);
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200212 retval = (retval << 1) |
Marek Vasutfcd62172020-07-08 06:46:09 +0200213 !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ);
wdenkc6097192002-11-03 00:24:07 +0000214 }
215
Marek Vasutfcd62172020-07-08 06:46:09 +0200216 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000217
Marek Vasutfcd62172020-07-08 06:46:09 +0200218 debug_cond(SROM_DLEVEL >= 2, " :%X:", getfrom_srom(priv, ioaddr) & 15);
wdenkc6097192002-11-03 00:24:07 +0000219
220 for (i = 16; i > 0; i--) {
Marek Vasutfcd62172020-07-08 06:46:09 +0200221 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000222 udelay(10);
Marek Vasutc2abfca2020-04-19 04:05:44 +0200223 debug_cond(SROM_DLEVEL >= 2, "%X",
Marek Vasutfcd62172020-07-08 06:46:09 +0200224 getfrom_srom(priv, ioaddr) & 15);
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200225 retval = (retval << 1) |
Marek Vasutfcd62172020-07-08 06:46:09 +0200226 !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ);
227 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000228 udelay(10);
229 }
230
231 /* Terminate the EEPROM access. */
Marek Vasutfcd62172020-07-08 06:46:09 +0200232 sendto_srom(priv, SROM_RD | SROM_SR, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000233
Marek Vasutc2abfca2020-04-19 04:05:44 +0200234 debug_cond(SROM_DLEVEL >= 2, " EEPROM value at %d is %5.5x.\n",
235 location, retval);
wdenkc6097192002-11-03 00:24:07 +0000236
237 return retval;
238}
239
Marek Vasut171f5e52020-04-18 01:56:51 +0200240/*
241 * This executes a generic EEPROM command, typically a write or write
wdenkc935d3b2004-01-03 19:43:48 +0000242 * enable. It returns the data output from the EEPROM, and thus may
243 * also be used for reads.
244 */
Marek Vasutfcd62172020-07-08 06:46:09 +0200245static int do_eeprom_cmd(struct dc2114x_priv *priv, u_long ioaddr, int cmd,
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200246 int cmd_len)
wdenkc6097192002-11-03 00:24:07 +0000247{
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200248 unsigned int retval = 0;
wdenkc6097192002-11-03 00:24:07 +0000249
Marek Vasutc2abfca2020-04-19 04:05:44 +0200250 debug_cond(SROM_DLEVEL >= 1, " EEPROM op 0x%x: ", cmd);
wdenkc6097192002-11-03 00:24:07 +0000251
Marek Vasutfcd62172020-07-08 06:46:09 +0200252 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000253
254 /* Shift the command bits out. */
255 do {
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200256 short dataval = (cmd & BIT(cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
257
Marek Vasutfcd62172020-07-08 06:46:09 +0200258 sendto_srom(priv, dataval, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000259 udelay(10);
260
Marek Vasutc2abfca2020-04-19 04:05:44 +0200261 debug_cond(SROM_DLEVEL >= 2, "%X",
Marek Vasutfcd62172020-07-08 06:46:09 +0200262 getfrom_srom(priv, ioaddr) & 15);
wdenkc6097192002-11-03 00:24:07 +0000263
Marek Vasutfcd62172020-07-08 06:46:09 +0200264 sendto_srom(priv, dataval | DT_CLK, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000265 udelay(10);
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200266 retval = (retval << 1) |
Marek Vasutfcd62172020-07-08 06:46:09 +0200267 !!(getfrom_srom(priv, ioaddr) & EE_DATA_READ);
wdenkc6097192002-11-03 00:24:07 +0000268 } while (--cmd_len >= 0);
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200269
Marek Vasutfcd62172020-07-08 06:46:09 +0200270 sendto_srom(priv, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000271
272 /* Terminate the EEPROM access. */
Marek Vasutfcd62172020-07-08 06:46:09 +0200273 sendto_srom(priv, SROM_RD | SROM_SR, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000274
Marek Vasutc2abfca2020-04-19 04:05:44 +0200275 debug_cond(SROM_DLEVEL >= 1, " EEPROM result is 0x%5.5x.\n", retval);
wdenkc6097192002-11-03 00:24:07 +0000276
277 return retval;
278}
279
Marek Vasutfcd62172020-07-08 06:46:09 +0200280static int read_srom(struct dc2114x_priv *priv, u_long ioaddr, int index)
wdenkc6097192002-11-03 00:24:07 +0000281{
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200282 int ee_addr_size;
wdenkc6097192002-11-03 00:24:07 +0000283
Marek Vasutfcd62172020-07-08 06:46:09 +0200284 ee_addr_size = (do_read_eeprom(priv, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200285
Marek Vasutfcd62172020-07-08 06:46:09 +0200286 return do_eeprom_cmd(priv, ioaddr, 0xffff |
Marek Vasut2e5c2a12020-04-19 03:11:06 +0200287 (((SROM_READ_CMD << ee_addr_size) | index) << 16),
288 3 + ee_addr_size + 16);
wdenkc6097192002-11-03 00:24:07 +0000289}
290
Marek Vasutbc4666a2020-07-08 07:20:14 +0200291static void send_setup_frame(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200292{
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800293 /* 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 Vasutdbe9c0c2020-04-19 04:00:49 +0200299 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 Vasutfcd62172020-07-08 06:46:09 +0200305 *(pa + (i & 1)) = priv->enetaddr[i];
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200306 if (i & 0x01)
307 pa += 4;
308 }
309
Marek Vasut32d8d112020-07-08 07:01:32 +0200310 for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) {
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200311 if (i < TOUT_LOOP)
312 continue;
313
Marek Vasutfcd62172020-07-08 06:46:09 +0200314 printf("%s: tx error buffer not ready\n", priv->name);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200315 return;
316 }
317
Marek Vasut32d8d112020-07-08 07:01:32 +0200318 priv->tx_ring[priv->tx_new].buf = cpu_to_le32(phys_to_bus(priv->devno,
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800319 (phys_addr_t)&setup_frame[0]));
Hanyuan Zhaoc303f4a2024-08-09 16:57:00 +0800320#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 Vasut32d8d112020-07-08 07:01:32 +0200324 priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_TER | TD_SET | SETUP_FRAME_LEN);
Hanyuan Zhaoc303f4a2024-08-09 16:57:00 +0800325#endif
Marek Vasut32d8d112020-07-08 07:01:32 +0200326 priv->tx_ring[priv->tx_new].status = cpu_to_le32(T_OWN);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200327
Marek Vasutfcd62172020-07-08 06:46:09 +0200328 dc2114x_outl(priv, POLL_DEMAND, DE4X5_TPD);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200329
Marek Vasut32d8d112020-07-08 07:01:32 +0200330 for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) {
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200331 if (i < TOUT_LOOP)
332 continue;
333
Marek Vasutfcd62172020-07-08 06:46:09 +0200334 printf("%s: tx buffer not ready\n", priv->name);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200335 return;
336 }
337
Marek Vasut32d8d112020-07-08 07:01:32 +0200338 if (le32_to_cpu(priv->tx_ring[priv->tx_new].status) != 0x7FFFFFFF) {
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800339 debug("TX error status2 = 0x%08X\n",
Marek Vasut32d8d112020-07-08 07:01:32 +0200340 le32_to_cpu(priv->tx_ring[priv->tx_new].status));
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200341 }
342
Marek Vasut32d8d112020-07-08 07:01:32 +0200343 priv->tx_new = (priv->tx_new + 1) % NUM_TX_DESC;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200344}
345
Marek Vasutbc4666a2020-07-08 07:20:14 +0200346static int dc21x4x_send_common(struct dc2114x_priv *priv, void *packet, int length)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200347{
348 int status = -1;
349 int i;
350
351 if (length <= 0) {
Marek Vasutfcd62172020-07-08 06:46:09 +0200352 printf("%s: bad packet size: %d\n", priv->name, length);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200353 goto done;
354 }
355
Marek Vasut32d8d112020-07-08 07:01:32 +0200356 for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) {
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200357 if (i < TOUT_LOOP)
358 continue;
359
Marek Vasutfcd62172020-07-08 06:46:09 +0200360 printf("%s: tx error buffer not ready\n", priv->name);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200361 goto done;
362 }
363
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800364 /* 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 Vasut32d8d112020-07-08 07:01:32 +0200367 priv->tx_ring[priv->tx_new].buf = cpu_to_le32(phys_to_bus(priv->devno,
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800368 (phys_addr_t)packet));
Hanyuan Zhaoc303f4a2024-08-09 16:57:00 +0800369#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 Vasut32d8d112020-07-08 07:01:32 +0200373 priv->tx_ring[priv->tx_new].des1 = cpu_to_le32(TD_TER | TD_LS | TD_FS | length);
Hanyuan Zhaoc303f4a2024-08-09 16:57:00 +0800374#endif
Marek Vasut32d8d112020-07-08 07:01:32 +0200375 priv->tx_ring[priv->tx_new].status = cpu_to_le32(T_OWN);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200376
Marek Vasutfcd62172020-07-08 06:46:09 +0200377 dc2114x_outl(priv, POLL_DEMAND, DE4X5_TPD);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200378
Marek Vasut32d8d112020-07-08 07:01:32 +0200379 for (i = 0; priv->tx_ring[priv->tx_new].status & cpu_to_le32(T_OWN); i++) {
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200380 if (i < TOUT_LOOP)
381 continue;
382
Marek Vasutfcd62172020-07-08 06:46:09 +0200383 printf(".%s: tx buffer not ready\n", priv->name);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200384 goto done;
385 }
386
Marek Vasut32d8d112020-07-08 07:01:32 +0200387 if (le32_to_cpu(priv->tx_ring[priv->tx_new].status) & TD_ES) {
388 priv->tx_ring[priv->tx_new].status = 0x0;
Hanyuan Zhao5fa3e102024-08-09 16:56:59 +0800389#if !CONFIG_IS_ENABLED(TULIP_IGNORE_TX_NO_CARRIER)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200390 goto done;
Hanyuan Zhao5fa3e102024-08-09 16:56:59 +0800391#endif
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200392 }
393
394 status = length;
395
396done:
Marek Vasut32d8d112020-07-08 07:01:32 +0200397 priv->tx_new = (priv->tx_new + 1) % NUM_TX_DESC;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200398 return status;
399}
400
Marek Vasut05c49172020-07-08 07:12:58 +0200401static 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 Vasutbc4666a2020-07-08 07:20:14 +0200429static int dc21x4x_init_common(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200430{
Marek Vasutfcd62172020-07-08 06:46:09 +0200431 int i;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200432
Marek Vasutfcd62172020-07-08 06:46:09 +0200433 reset_de4x5(priv);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200434
Marek Vasutfcd62172020-07-08 06:46:09 +0200435 if (dc2114x_inl(priv, DE4X5_STS) & (STS_TS | STS_RS)) {
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200436 printf("Error: Cannot reset ethernet controller.\n");
437 return -1;
438 }
439
Hanyuan Zhaoba30f462024-08-09 16:57:01 +0800440 /* 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 Vasutdbe9c0c2020-04-19 04:00:49 +0200450
451 for (i = 0; i < NUM_RX_DESC; i++) {
Marek Vasut32d8d112020-07-08 07:01:32 +0200452 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 Zhao8c18c532024-08-09 16:56:58 +0800455 (phys_addr_t)net_rx_packets[i]));
Marek Vasut32d8d112020-07-08 07:01:32 +0200456 priv->rx_ring[i].next = 0;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200457 }
458
459 for (i = 0; i < NUM_TX_DESC; i++) {
Marek Vasut32d8d112020-07-08 07:01:32 +0200460 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 Vasutdbe9c0c2020-04-19 04:00:49 +0200464 }
465
Marek Vasut32d8d112020-07-08 07:01:32 +0200466 priv->rx_ring_size = NUM_RX_DESC;
467 priv->tx_ring_size = NUM_TX_DESC;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200468
469 /* Write the end of list marker to the descriptor lists. */
Marek Vasut32d8d112020-07-08 07:01:32 +0200470 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 Vasutdbe9c0c2020-04-19 04:00:49 +0200472
473 /* Tell the adapter where the TX/RX rings are located. */
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800474 dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->rx_ring),
Marek Vasut8a5c6f12020-07-08 06:50:41 +0200475 DE4X5_RRBA);
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800476 dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->tx_ring),
Marek Vasut8a5c6f12020-07-08 06:50:41 +0200477 DE4X5_TRBA);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200478
Marek Vasutfcd62172020-07-08 06:46:09 +0200479 start_de4x5(priv);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200480
Marek Vasut32d8d112020-07-08 07:01:32 +0200481 priv->tx_new = 0;
482 priv->rx_new = 0;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200483
Marek Vasutbc4666a2020-07-08 07:20:14 +0200484 send_setup_frame(priv);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200485
486 return 0;
487}
488
Marek Vasutbc4666a2020-07-08 07:20:14 +0200489static void dc21x4x_halt_common(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200490{
Marek Vasutfcd62172020-07-08 06:46:09 +0200491 stop_de4x5(priv);
492 dc2114x_outl(priv, 0, DE4X5_SICR);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200493}
494
Marek Vasut2301a4b2020-07-08 06:42:07 +0200495static void read_hw_addr(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200496{
Marek Vasut2301a4b2020-07-08 06:42:07 +0200497 u_short tmp, *p = (u_short *)(&priv->enetaddr[0]);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200498 int i, j = 0;
499
500 for (i = 0; i < (ETH_ALEN >> 1); i++) {
Marek Vasutfcd62172020-07-08 06:46:09 +0200501 tmp = read_srom(priv, DE4X5_APROM, (SROM_HWADD >> 1) + i);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200502 *p = le16_to_cpu(tmp);
503 j += *p++;
504 }
505
506 if (!j || j == 0x2fffd) {
Marek Vasut2301a4b2020-07-08 06:42:07 +0200507 memset(priv->enetaddr, 0, ETH_ALEN);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200508 debug("Warning: can't read HW address from SROM.\n");
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200509 }
510}
511
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800512#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200513static struct pci_device_id supported[] = {
Marek Vasut75e375b2020-06-20 17:36:42 +0200514 { 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 Vasutdbe9c0c2020-04-19 04:00:49 +0200516 { }
517};
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800518#endif
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200519
Marek Vasutf23a7852020-07-08 07:26:14 +0200520static int dc2114x_start(struct udevice *dev)
521{
Marek Vasutf23a7852020-07-08 07:26:14 +0200522 struct dc2114x_priv *priv = dev_get_priv(dev);
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800523 int rval;
Marek Vasutf23a7852020-07-08 07:26:14 +0200524
Tom Rini568407f2024-10-27 10:15:43 -0600525 if (!priv->enetaddr) {
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800526 rval = eth_env_get_enetaddr("ethaddr", priv->enetaddr);
Marek Vasutf23a7852020-07-08 07:26:14 +0200527
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800528 if (!rval) {
529 printf("dc2114x: Err: please set a valid MAC address\n");
530 return -EINVAL;
531 }
532 }
Marek Vasutf23a7852020-07-08 07:26:14 +0200533
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800534#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200535 /* Ensure we're not sleeping. */
536 dm_pci_write_config8(dev, PCI_CFDA_PSM, WAKEUP);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800537#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200538
539 return dc21x4x_init_common(priv);
540}
541
542static void dc2114x_stop(struct udevice *dev)
543{
544 struct dc2114x_priv *priv = dev_get_priv(dev);
545
546 dc21x4x_halt_common(priv);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800547#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200548 dm_pci_write_config8(dev, PCI_CFDA_PSM, SLEEP);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800549#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200550}
551
552static 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
562static 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 Zhao23edc8f2024-08-09 16:56:57 +0800578 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 Vasutf23a7852020-07-08 07:26:14 +0200580
581 return ret - 4;
582}
583
584static 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
596static 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
605static int dc2114x_bind(struct udevice *dev)
606{
Hanyuan Zhao26d88de2024-08-09 16:56:56 +0800607 static int card_number = 0;
Marek Vasutf23a7852020-07-08 07:26:14 +0200608 char name[16];
609
610 sprintf(name, "dc2114x#%u", card_number++);
611
612 return device_set_name(dev, name);
613}
614
615static int dc2114x_probe(struct udevice *dev)
616{
Simon Glassc69cda22020-12-03 16:55:20 -0700617 struct eth_pdata *plat = dev_get_plat(dev);
Marek Vasutf23a7852020-07-08 07:26:14 +0200618 struct dc2114x_priv *priv = dev_get_priv(dev);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800619
620#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200621 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 Vasutf23a7852020-07-08 07:26:14 +0200628 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 Zhao76146b92024-08-09 16:56:54 +0800639#endif
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800640
641 priv->devno = dev;
642 priv->enetaddr = plat->enetaddr;
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800643 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 Vasutf23a7852020-07-08 07:26:14 +0200645
646 return 0;
647}
648
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800649#if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
650static 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 Rini568407f2024-10-27 10:15:43 -0600656 priv->iobase = (void *)plat->iobase;
Marek Vasutf23a7852020-07-08 07:26:14 +0200657
658 return 0;
659}
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800660#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200661
662static 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 Zhao76146b92024-08-09 16:56:54 +0800671#if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
672static 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 Vasutf23a7852020-07-08 07:26:14 +0200681U_BOOT_DRIVER(eth_dc2114x) = {
682 .name = "eth_dc2114x",
683 .id = UCLASS_ETH,
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800684#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 Vasutf23a7852020-07-08 07:26:14 +0200688 .bind = dc2114x_bind,
689 .probe = dc2114x_probe,
690 .ops = &dc2114x_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700691 .priv_auto = sizeof(struct dc2114x_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700692 .plat_auto = sizeof(struct eth_pdata),
Marek Vasutf23a7852020-07-08 07:26:14 +0200693};
694
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800695#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200696U_BOOT_PCI_DEVICE(eth_dc2114x, supported);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800697#endif