blob: 11dea9b4d7931b933a89b8cd249689fb29b0fafa [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
Marek Vasutfcd62172020-07-08 06:46:09 +0200440 dc2114x_outl(priv, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200441
442 for (i = 0; i < NUM_RX_DESC; i++) {
Marek Vasut32d8d112020-07-08 07:01:32 +0200443 priv->rx_ring[i].status = cpu_to_le32(R_OWN);
444 priv->rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
445 priv->rx_ring[i].buf = cpu_to_le32(phys_to_bus(priv->devno,
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800446 (phys_addr_t)net_rx_packets[i]));
Marek Vasut32d8d112020-07-08 07:01:32 +0200447 priv->rx_ring[i].next = 0;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200448 }
449
450 for (i = 0; i < NUM_TX_DESC; i++) {
Marek Vasut32d8d112020-07-08 07:01:32 +0200451 priv->tx_ring[i].status = 0;
452 priv->tx_ring[i].des1 = 0;
453 priv->tx_ring[i].buf = 0;
454 priv->tx_ring[i].next = 0;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200455 }
456
Marek Vasut32d8d112020-07-08 07:01:32 +0200457 priv->rx_ring_size = NUM_RX_DESC;
458 priv->tx_ring_size = NUM_TX_DESC;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200459
460 /* Write the end of list marker to the descriptor lists. */
Marek Vasut32d8d112020-07-08 07:01:32 +0200461 priv->rx_ring[priv->rx_ring_size - 1].des1 |= cpu_to_le32(RD_RER);
462 priv->tx_ring[priv->tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200463
464 /* Tell the adapter where the TX/RX rings are located. */
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800465 dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->rx_ring),
Marek Vasut8a5c6f12020-07-08 06:50:41 +0200466 DE4X5_RRBA);
Hanyuan Zhao8c18c532024-08-09 16:56:58 +0800467 dc2114x_outl(priv, phys_to_bus(priv->devno, (phys_addr_t)priv->tx_ring),
Marek Vasut8a5c6f12020-07-08 06:50:41 +0200468 DE4X5_TRBA);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200469
Marek Vasutfcd62172020-07-08 06:46:09 +0200470 start_de4x5(priv);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200471
Marek Vasut32d8d112020-07-08 07:01:32 +0200472 priv->tx_new = 0;
473 priv->rx_new = 0;
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200474
Marek Vasutbc4666a2020-07-08 07:20:14 +0200475 send_setup_frame(priv);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200476
477 return 0;
478}
479
Marek Vasutbc4666a2020-07-08 07:20:14 +0200480static void dc21x4x_halt_common(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200481{
Marek Vasutfcd62172020-07-08 06:46:09 +0200482 stop_de4x5(priv);
483 dc2114x_outl(priv, 0, DE4X5_SICR);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200484}
485
Marek Vasut2301a4b2020-07-08 06:42:07 +0200486static void read_hw_addr(struct dc2114x_priv *priv)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200487{
Marek Vasut2301a4b2020-07-08 06:42:07 +0200488 u_short tmp, *p = (u_short *)(&priv->enetaddr[0]);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200489 int i, j = 0;
490
491 for (i = 0; i < (ETH_ALEN >> 1); i++) {
Marek Vasutfcd62172020-07-08 06:46:09 +0200492 tmp = read_srom(priv, DE4X5_APROM, (SROM_HWADD >> 1) + i);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200493 *p = le16_to_cpu(tmp);
494 j += *p++;
495 }
496
497 if (!j || j == 0x2fffd) {
Marek Vasut2301a4b2020-07-08 06:42:07 +0200498 memset(priv->enetaddr, 0, ETH_ALEN);
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200499 debug("Warning: can't read HW address from SROM.\n");
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200500 }
501}
502
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800503#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200504static struct pci_device_id supported[] = {
Marek Vasut75e375b2020-06-20 17:36:42 +0200505 { PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST) },
506 { PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142) },
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200507 { }
508};
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800509#endif
Marek Vasutdbe9c0c2020-04-19 04:00:49 +0200510
Marek Vasutf23a7852020-07-08 07:26:14 +0200511static int dc2114x_start(struct udevice *dev)
512{
Marek Vasutf23a7852020-07-08 07:26:14 +0200513 struct dc2114x_priv *priv = dev_get_priv(dev);
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800514 int rval;
Marek Vasutf23a7852020-07-08 07:26:14 +0200515
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800516 if(!priv->enetaddr) {
517 rval = eth_env_get_enetaddr("ethaddr", priv->enetaddr);
518
519 if (!rval) {
520 printf("dc2114x: Err: please set a valid MAC address\n");
521 return -EINVAL;
522 }
523 }
Marek Vasutf23a7852020-07-08 07:26:14 +0200524
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800525#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200526 /* Ensure we're not sleeping. */
527 dm_pci_write_config8(dev, PCI_CFDA_PSM, WAKEUP);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800528#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200529
530 return dc21x4x_init_common(priv);
531}
532
533static void dc2114x_stop(struct udevice *dev)
534{
535 struct dc2114x_priv *priv = dev_get_priv(dev);
536
537 dc21x4x_halt_common(priv);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800538#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200539 dm_pci_write_config8(dev, PCI_CFDA_PSM, SLEEP);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800540#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200541}
542
543static int dc2114x_send(struct udevice *dev, void *packet, int length)
544{
545 struct dc2114x_priv *priv = dev_get_priv(dev);
546 int ret;
547
548 ret = dc21x4x_send_common(priv, packet, length);
549
550 return ret ? 0 : -ETIMEDOUT;
551}
552
553static int dc2114x_recv(struct udevice *dev, int flags, uchar **packetp)
554{
555 struct dc2114x_priv *priv = dev_get_priv(dev);
556 int ret;
557
558 ret = dc21x4x_recv_check(priv);
559
560 if (ret < 0) {
561 /* Update entry information. */
562 priv->rx_new = (priv->rx_new + 1) % priv->rx_ring_size;
563 ret = 0;
564 }
565
566 if (!ret)
567 return 0;
568
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800569 invalidate_dcache_range((phys_addr_t)net_rx_packets[priv->rx_new], (phys_addr_t)(net_rx_packets[priv->rx_new] + RX_BUFF_SZ));
570 *packetp = (uchar *)net_rx_packets[priv->rx_new];
Marek Vasutf23a7852020-07-08 07:26:14 +0200571
572 return ret - 4;
573}
574
575static int dc2114x_free_pkt(struct udevice *dev, uchar *packet, int length)
576{
577 struct dc2114x_priv *priv = dev_get_priv(dev);
578
579 priv->rx_ring[priv->rx_new].status = cpu_to_le32(R_OWN);
580
581 /* Update entry information. */
582 priv->rx_new = (priv->rx_new + 1) % priv->rx_ring_size;
583
584 return 0;
585}
586
587static int dc2114x_read_rom_hwaddr(struct udevice *dev)
588{
589 struct dc2114x_priv *priv = dev_get_priv(dev);
590
591 read_hw_addr(priv);
592
593 return 0;
594}
595
596static int dc2114x_bind(struct udevice *dev)
597{
Hanyuan Zhao26d88de2024-08-09 16:56:56 +0800598 static int card_number = 0;
Marek Vasutf23a7852020-07-08 07:26:14 +0200599 char name[16];
600
601 sprintf(name, "dc2114x#%u", card_number++);
602
603 return device_set_name(dev, name);
604}
605
606static int dc2114x_probe(struct udevice *dev)
607{
Simon Glassc69cda22020-12-03 16:55:20 -0700608 struct eth_pdata *plat = dev_get_plat(dev);
Marek Vasutf23a7852020-07-08 07:26:14 +0200609 struct dc2114x_priv *priv = dev_get_priv(dev);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800610
611#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200612 u16 command, status;
613 u32 iobase;
614
615 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_1, &iobase);
616 iobase &= ~0xf;
617
618 debug("dc2114x: DEC 2114x PCI Device @0x%x\n", iobase);
Marek Vasutf23a7852020-07-08 07:26:14 +0200619 priv->iobase = (void __iomem *)dm_pci_mem_to_phys(dev, iobase);
620
621 command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
622 dm_pci_write_config16(dev, PCI_COMMAND, command);
623 dm_pci_read_config16(dev, PCI_COMMAND, &status);
624 if ((status & command) != command) {
625 printf("dc2114x: Couldn't enable IO access or Bus Mastering\n");
626 return -EINVAL;
627 }
628
629 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x60);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800630#endif
Hanyuan Zhaoa35aa5a2024-08-09 16:56:55 +0800631
632 priv->devno = dev;
633 priv->enetaddr = plat->enetaddr;
Hanyuan Zhao23edc8f2024-08-09 16:56:57 +0800634 priv->rx_ring = (struct de4x5_desc *)map_physmem((phys_addr_t)virt_to_phys(rx_ring), 0, MAP_NOCACHE);
635 priv->tx_ring = (struct de4x5_desc *)map_physmem((phys_addr_t)virt_to_phys(tx_ring), 0, MAP_NOCACHE);
636
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800637 return 0;
638}
639
640#if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
641static int dc2114x_of_to_plat(struct udevice *dev)
642{
643 struct eth_pdata *plat = dev_get_plat(dev);
644 struct dc2114x_priv *priv = dev_get_priv(dev);
645
646 plat->iobase = (phys_addr_t)map_physmem((phys_addr_t)devfdt_get_addr(dev), 0, MAP_NOCACHE);
647 priv->iobase = (void*)plat->iobase;
Marek Vasutf23a7852020-07-08 07:26:14 +0200648
649 return 0;
650}
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800651#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200652
653static const struct eth_ops dc2114x_ops = {
654 .start = dc2114x_start,
655 .send = dc2114x_send,
656 .recv = dc2114x_recv,
657 .stop = dc2114x_stop,
658 .free_pkt = dc2114x_free_pkt,
659 .read_rom_hwaddr = dc2114x_read_rom_hwaddr,
660};
661
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800662#if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
663static const struct udevice_id dc2114x_eth_ids[] = {
664 { .compatible = "dec,dmfe" },
665 { .compatible = "tulip,dmfe" },
666 { .compatible = "dec,dc2114x" },
667 { .compatible = "tulip,dc2114x" },
668 { }
669};
670#endif
671
Marek Vasutf23a7852020-07-08 07:26:14 +0200672U_BOOT_DRIVER(eth_dc2114x) = {
673 .name = "eth_dc2114x",
674 .id = UCLASS_ETH,
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800675#if CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
676 .of_match = dc2114x_eth_ids,
677 .of_to_plat = dc2114x_of_to_plat,
678#endif
Marek Vasutf23a7852020-07-08 07:26:14 +0200679 .bind = dc2114x_bind,
680 .probe = dc2114x_probe,
681 .ops = &dc2114x_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700682 .priv_auto = sizeof(struct dc2114x_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700683 .plat_auto = sizeof(struct eth_pdata),
Marek Vasutf23a7852020-07-08 07:26:14 +0200684};
685
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800686#if !CONFIG_IS_ENABLED(TULIP_SUPPORT_NON_PCI)
Marek Vasutf23a7852020-07-08 07:26:14 +0200687U_BOOT_PCI_DEVICE(eth_dc2114x, supported);
Hanyuan Zhao76146b92024-08-09 16:56:54 +0800688#endif