blob: d008696b0ffb1918af21121067cfed53481d598d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002
3#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -06004#include <env.h>
wdenkc6097192002-11-03 00:24:07 +00005#include <malloc.h>
6#include <net.h>
Ben Warren840f8a52008-08-31 10:45:44 -07007#include <netdev.h>
wdenkc6097192002-11-03 00:24:07 +00008#include <pci.h>
9
Marek Vasut091eea82020-04-19 04:05:44 +020010#define SROM_DLEVEL 0
wdenkc6097192002-11-03 00:24:07 +000011
12#undef UPDATE_SROM
13
Marek Vasut81d10f72020-04-19 03:09:26 +020014/* PCI Registers. */
15#define PCI_CFDA_PSM 0x43
wdenkc6097192002-11-03 00:24:07 +000016
17#define CFRV_RN 0x000000f0 /* Revision Number */
18
19#define WAKEUP 0x00 /* Power Saving Wakeup */
20#define SLEEP 0x80 /* Power Saving Sleep Mode */
21
Marek Vasut81d10f72020-04-19 03:09:26 +020022#define DC2114x_BRK 0x0020 /* CFRV break between DC21142 & DC21143 */
wdenkc6097192002-11-03 00:24:07 +000023
Marek Vasut81d10f72020-04-19 03:09:26 +020024/* Ethernet chip registers. */
wdenkc6097192002-11-03 00:24:07 +000025#define DE4X5_BMR 0x000 /* Bus Mode Register */
26#define DE4X5_TPD 0x008 /* Transmit Poll Demand Reg */
27#define DE4X5_RRBA 0x018 /* RX Ring Base Address Reg */
28#define DE4X5_TRBA 0x020 /* TX Ring Base Address Reg */
29#define DE4X5_STS 0x028 /* Status Register */
30#define DE4X5_OMR 0x030 /* Operation Mode Register */
31#define DE4X5_SICR 0x068 /* SIA Connectivity Register */
32#define DE4X5_APROM 0x048 /* Ethernet Address PROM */
33
Marek Vasut81d10f72020-04-19 03:09:26 +020034/* Register bits. */
wdenkc6097192002-11-03 00:24:07 +000035#define BMR_SWR 0x00000001 /* Software Reset */
36#define STS_TS 0x00700000 /* Transmit Process State */
37#define STS_RS 0x000e0000 /* Receive Process State */
38#define OMR_ST 0x00002000 /* Start/Stop Transmission Command */
39#define OMR_SR 0x00000002 /* Start/Stop Receive */
40#define OMR_PS 0x00040000 /* Port Select */
41#define OMR_SDP 0x02000000 /* SD Polarity - MUST BE ASSERTED */
42#define OMR_PM 0x00000080 /* Pass All Multicast */
43
Marek Vasut81d10f72020-04-19 03:09:26 +020044/* Descriptor bits. */
wdenkc6097192002-11-03 00:24:07 +000045#define R_OWN 0x80000000 /* Own Bit */
46#define RD_RER 0x02000000 /* Receive End Of Ring */
47#define RD_LS 0x00000100 /* Last Descriptor */
48#define RD_ES 0x00008000 /* Error Summary */
49#define TD_TER 0x02000000 /* Transmit End Of Ring */
50#define T_OWN 0x80000000 /* Own Bit */
51#define TD_LS 0x40000000 /* Last Segment */
52#define TD_FS 0x20000000 /* First Segment */
53#define TD_ES 0x00008000 /* Error Summary */
54#define TD_SET 0x08000000 /* Setup Packet */
55
56/* The EEPROM commands include the alway-set leading bit. */
57#define SROM_WRITE_CMD 5
58#define SROM_READ_CMD 6
59#define SROM_ERASE_CMD 7
60
Marek Vasut81d10f72020-04-19 03:09:26 +020061#define SROM_HWADD 0x0014 /* Hardware Address offset in SROM */
wdenkc6097192002-11-03 00:24:07 +000062#define SROM_RD 0x00004000 /* Read from Boot ROM */
Marek Vasut81d10f72020-04-19 03:09:26 +020063#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
64#define EE_WRITE_0 0x4801
65#define EE_WRITE_1 0x4805
66#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
wdenkc6097192002-11-03 00:24:07 +000067#define SROM_SR 0x00000800 /* Select Serial ROM when set */
68
69#define DT_IN 0x00000004 /* Serial Data In */
70#define DT_CLK 0x00000002 /* Serial ROM Clock */
71#define DT_CS 0x00000001 /* Serial ROM Chip Select */
72
73#define POLL_DEMAND 1
74
Marek Vasut75244fb2020-04-19 03:36:46 +020075#if defined(CONFIG_E500)
76#define phys_to_bus(a) (a)
77#else
78#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
79#endif
80
Marek Vasut5e2ad052020-04-19 04:00:49 +020081#define NUM_RX_DESC PKTBUFSRX
82#define NUM_TX_DESC 1 /* Number of TX descriptors */
83#define RX_BUFF_SZ PKTSIZE_ALIGN
84
85#define TOUT_LOOP 1000000
86
87#define SETUP_FRAME_LEN 192
88
89struct de4x5_desc {
90 volatile s32 status;
91 u32 des1;
92 u32 buf;
93 u32 next;
94};
95
96/* RX and TX descriptor ring */
97static struct de4x5_desc rx_ring[NUM_RX_DESC] __aligned(32);
98static struct de4x5_desc tx_ring[NUM_TX_DESC] __aligned(32);
99static int rx_new; /* RX descriptor ring pointer */
100static int tx_new; /* TX descriptor ring pointer */
101
102static char rx_ring_size;
103static char tx_ring_size;
104
Marek Vasutf02b7012020-04-19 03:40:03 +0200105static u32 dc2114x_inl(struct eth_device *dev, u32 addr)
Marek Vasut75244fb2020-04-19 03:36:46 +0200106{
Marek Vasutf02b7012020-04-19 03:40:03 +0200107 return le32_to_cpu(*(volatile u32 *)(addr + dev->iobase));
Marek Vasut75244fb2020-04-19 03:36:46 +0200108}
109
Marek Vasutf02b7012020-04-19 03:40:03 +0200110static void dc2114x_outl(struct eth_device *dev, u32 command, u32 addr)
Marek Vasut75244fb2020-04-19 03:36:46 +0200111{
Marek Vasutf02b7012020-04-19 03:40:03 +0200112 *(volatile u32 *)(addr + dev->iobase) = cpu_to_le32(command);
Marek Vasut75244fb2020-04-19 03:36:46 +0200113}
114
115static void reset_de4x5(struct eth_device *dev)
116{
Marek Vasutf02b7012020-04-19 03:40:03 +0200117 u32 i;
Marek Vasut75244fb2020-04-19 03:36:46 +0200118
Marek Vasutf02b7012020-04-19 03:40:03 +0200119 i = dc2114x_inl(dev, DE4X5_BMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200120 mdelay(1);
Marek Vasutf02b7012020-04-19 03:40:03 +0200121 dc2114x_outl(dev, i | BMR_SWR, DE4X5_BMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200122 mdelay(1);
Marek Vasutf02b7012020-04-19 03:40:03 +0200123 dc2114x_outl(dev, i, DE4X5_BMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200124 mdelay(1);
125
126 for (i = 0; i < 5; i++) {
Marek Vasutf02b7012020-04-19 03:40:03 +0200127 dc2114x_inl(dev, DE4X5_BMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200128 mdelay(10);
129 }
130
131 mdelay(1);
wdenkc6097192002-11-03 00:24:07 +0000132}
133
Marek Vasut75244fb2020-04-19 03:36:46 +0200134static void start_de4x5(struct eth_device *dev)
135{
Marek Vasutf02b7012020-04-19 03:40:03 +0200136 u32 omr;
Marek Vasut75244fb2020-04-19 03:36:46 +0200137
Marek Vasutf02b7012020-04-19 03:40:03 +0200138 omr = dc2114x_inl(dev, DE4X5_OMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200139 omr |= OMR_ST | OMR_SR;
Marek Vasutf02b7012020-04-19 03:40:03 +0200140 dc2114x_outl(dev, omr, DE4X5_OMR); /* Enable the TX and/or RX */
wdenkc6097192002-11-03 00:24:07 +0000141}
142
Marek Vasut75244fb2020-04-19 03:36:46 +0200143static void stop_de4x5(struct eth_device *dev)
144{
Marek Vasutf02b7012020-04-19 03:40:03 +0200145 u32 omr;
Marek Vasut75244fb2020-04-19 03:36:46 +0200146
Marek Vasutf02b7012020-04-19 03:40:03 +0200147 omr = dc2114x_inl(dev, DE4X5_OMR);
Marek Vasut75244fb2020-04-19 03:36:46 +0200148 omr &= ~(OMR_ST | OMR_SR);
Marek Vasutf02b7012020-04-19 03:40:03 +0200149 dc2114x_outl(dev, omr, DE4X5_OMR); /* Disable the TX and/or RX */
wdenkc6097192002-11-03 00:24:07 +0000150}
151
Marek Vasut5e2ad052020-04-19 04:00:49 +0200152/* SROM Read and write routines. */
153static void sendto_srom(struct eth_device *dev, u_int command, u_long addr)
154{
155 dc2114x_outl(dev, command, addr);
156 udelay(1);
157}
wdenkc6097192002-11-03 00:24:07 +0000158
Marek Vasut5e2ad052020-04-19 04:00:49 +0200159static int getfrom_srom(struct eth_device *dev, u_long addr)
160{
161 u32 tmp = dc2114x_inl(dev, addr);
wdenkc6097192002-11-03 00:24:07 +0000162
Marek Vasut5e2ad052020-04-19 04:00:49 +0200163 udelay(1);
164 return tmp;
165}
wdenkc6097192002-11-03 00:24:07 +0000166
Marek Vasut5e2ad052020-04-19 04:00:49 +0200167/* Note: this routine returns extra data bits for size detection. */
168static int do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location,
169 int addr_len)
170{
171 int read_cmd = location | (SROM_READ_CMD << addr_len);
172 unsigned int retval = 0;
173 int i;
wdenkc6097192002-11-03 00:24:07 +0000174
Marek Vasut5e2ad052020-04-19 04:00:49 +0200175 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
176 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000177
Marek Vasut091eea82020-04-19 04:05:44 +0200178 debug_cond(SROM_DLEVEL >= 1, " EEPROM read at %d ", location);
wdenkc6097192002-11-03 00:24:07 +0000179
Marek Vasut5e2ad052020-04-19 04:00:49 +0200180 /* Shift the read command bits out. */
181 for (i = 4 + addr_len; i >= 0; i--) {
182 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
wdenkc6097192002-11-03 00:24:07 +0000183
Marek Vasut5e2ad052020-04-19 04:00:49 +0200184 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval,
185 ioaddr);
186 udelay(10);
187 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK,
188 ioaddr);
189 udelay(10);
Marek Vasut091eea82020-04-19 04:05:44 +0200190 debug_cond(SROM_DLEVEL >= 2, "%X",
191 getfrom_srom(dev, ioaddr) & 15);
Marek Vasut5e2ad052020-04-19 04:00:49 +0200192 retval = (retval << 1) |
193 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
194 }
wdenkc6097192002-11-03 00:24:07 +0000195
Marek Vasut5e2ad052020-04-19 04:00:49 +0200196 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000197
Marek Vasut091eea82020-04-19 04:05:44 +0200198 debug_cond(SROM_DLEVEL >= 2, " :%X:", getfrom_srom(dev, ioaddr) & 15);
wdenkc6097192002-11-03 00:24:07 +0000199
Marek Vasut5e2ad052020-04-19 04:00:49 +0200200 for (i = 16; i > 0; i--) {
201 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
202 udelay(10);
Marek Vasut091eea82020-04-19 04:05:44 +0200203 debug_cond(SROM_DLEVEL >= 2, "%X",
204 getfrom_srom(dev, ioaddr) & 15);
Marek Vasut5e2ad052020-04-19 04:00:49 +0200205 retval = (retval << 1) |
206 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
207 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
208 udelay(10);
209 }
wdenkc6097192002-11-03 00:24:07 +0000210
Marek Vasut5e2ad052020-04-19 04:00:49 +0200211 /* Terminate the EEPROM access. */
212 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000213
Marek Vasut091eea82020-04-19 04:05:44 +0200214 debug_cond(SROM_DLEVEL >= 2, " EEPROM value at %d is %5.5x.\n",
215 location, retval);
wdenkc6097192002-11-03 00:24:07 +0000216
Marek Vasut5e2ad052020-04-19 04:00:49 +0200217 return retval;
218}
wdenkc6097192002-11-03 00:24:07 +0000219
Marek Vasut5e2ad052020-04-19 04:00:49 +0200220/*
221 * This executes a generic EEPROM command, typically a write or write
222 * enable. It returns the data output from the EEPROM, and thus may
223 * also be used for reads.
224 */
225static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd,
226 int cmd_len)
227{
228 unsigned int retval = 0;
wdenkc6097192002-11-03 00:24:07 +0000229
Marek Vasut091eea82020-04-19 04:05:44 +0200230 debug_cond(SROM_DLEVEL >= 1, " EEPROM op 0x%x: ", cmd);
wdenkc6097192002-11-03 00:24:07 +0000231
Marek Vasut5e2ad052020-04-19 04:00:49 +0200232 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000233
Marek Vasut5e2ad052020-04-19 04:00:49 +0200234 /* Shift the command bits out. */
235 do {
236 short dataval = (cmd & BIT(cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
wdenkc6097192002-11-03 00:24:07 +0000237
Marek Vasut5e2ad052020-04-19 04:00:49 +0200238 sendto_srom(dev, dataval, ioaddr);
239 udelay(10);
Marek Vasut268cc5b2020-04-19 03:09:47 +0200240
Marek Vasut091eea82020-04-19 04:05:44 +0200241 debug_cond(SROM_DLEVEL >= 2, "%X",
242 getfrom_srom(dev, ioaddr) & 15);
Nobuhiro Iwamatsud45fa742010-10-19 14:03:40 +0900243
Marek Vasut5e2ad052020-04-19 04:00:49 +0200244 sendto_srom(dev, dataval | DT_CLK, ioaddr);
245 udelay(10);
246 retval = (retval << 1) |
247 !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
248 } while (--cmd_len >= 0);
wdenk0260cd62004-01-02 15:01:32 +0000249
Marek Vasut5e2ad052020-04-19 04:00:49 +0200250 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000251
Marek Vasut5e2ad052020-04-19 04:00:49 +0200252 /* Terminate the EEPROM access. */
253 sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
wdenkc6097192002-11-03 00:24:07 +0000254
Marek Vasut091eea82020-04-19 04:05:44 +0200255 debug_cond(SROM_DLEVEL >= 1, " EEPROM result is 0x%5.5x.\n", retval);
wdenkc6097192002-11-03 00:24:07 +0000256
Marek Vasut5e2ad052020-04-19 04:00:49 +0200257 return retval;
258}
Marek Vasut331e4ec2020-04-18 01:56:51 +0200259
Marek Vasut5e2ad052020-04-19 04:00:49 +0200260static int read_srom(struct eth_device *dev, u_long ioaddr, int index)
261{
262 int ee_addr_size;
wdenkc6097192002-11-03 00:24:07 +0000263
Marek Vasut5e2ad052020-04-19 04:00:49 +0200264 ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
wdenkc6097192002-11-03 00:24:07 +0000265
Marek Vasut5e2ad052020-04-19 04:00:49 +0200266 return do_eeprom_cmd(dev, ioaddr, 0xffff |
267 (((SROM_READ_CMD << ee_addr_size) | index) << 16),
268 3 + ee_addr_size + 16);
wdenkc6097192002-11-03 00:24:07 +0000269}
270
Marek Vasut5e2ad052020-04-19 04:00:49 +0200271#ifdef UPDATE_SROM
272static int write_srom(struct eth_device *dev, u_long ioaddr, int index,
273 int new_value)
wdenkc6097192002-11-03 00:24:07 +0000274{
Marek Vasut5e2ad052020-04-19 04:00:49 +0200275 unsigned short newval;
276 int ee_addr_size;
Marek Vasut268cc5b2020-04-19 03:09:47 +0200277 int i;
wdenkc6097192002-11-03 00:24:07 +0000278
Marek Vasut5e2ad052020-04-19 04:00:49 +0200279 ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
wdenkc6097192002-11-03 00:24:07 +0000280
Marek Vasut5e2ad052020-04-19 04:00:49 +0200281 udelay(10 * 1000); /* test-only */
wdenkc6097192002-11-03 00:24:07 +0000282
Marek Vasut091eea82020-04-19 04:05:44 +0200283 debug_cond(SROM_DLEVEL >= 1, "ee_addr_size=%d.\n", ee_addr_size);
284 debug_cond(SROM_DLEVEL >= 1,
285 "Writing new entry 0x%4.4x to offset %d.\n",
286 new_value, index);
wdenkc6097192002-11-03 00:24:07 +0000287
Marek Vasut5e2ad052020-04-19 04:00:49 +0200288 /* Enable programming modes. */
289 do_eeprom_cmd(dev, ioaddr, 0x4f << (ee_addr_size - 4),
290 3 + ee_addr_size);
wdenkc6097192002-11-03 00:24:07 +0000291
Marek Vasut5e2ad052020-04-19 04:00:49 +0200292 /* Do the actual write. */
293 do_eeprom_cmd(dev, ioaddr, new_value |
294 (((SROM_WRITE_CMD << ee_addr_size) | index) << 16),
295 3 + ee_addr_size + 16);
296
297 /* Poll for write finished. */
298 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
299 for (i = 0; i < 10000; i++) { /* Typical 2000 ticks */
300 if (getfrom_srom(dev, ioaddr) & EE_DATA_READ)
301 break;
wdenkc6097192002-11-03 00:24:07 +0000302 }
303
Marek Vasut091eea82020-04-19 04:05:44 +0200304 debug_cond(SROM_DLEVEL >= 1, " Write finished after %d ticks.\n", i);
Marek Vasut5e2ad052020-04-19 04:00:49 +0200305
306 /* Disable programming. */
307 do_eeprom_cmd(dev, ioaddr, (0x40 << (ee_addr_size - 4)),
308 3 + ee_addr_size);
309
310 /* And read the result. */
311 newval = do_eeprom_cmd(dev, ioaddr,
312 (((SROM_READ_CMD << ee_addr_size) | index) << 16)
313 | 0xffff, 3 + ee_addr_size + 16);
Marek Vasut091eea82020-04-19 04:05:44 +0200314
315 debug_cond(SROM_DLEVEL >= 1, " New value at offset %d is %4.4x.\n",
316 index, newval);
Marek Vasut5e2ad052020-04-19 04:00:49 +0200317
318 return 1;
319}
320
321static void update_srom(struct eth_device *dev, bd_t *bis)
322{
323 static unsigned short eeprom[0x40] = {
324 0x140b, 0x6610, 0x0000, 0x0000, /* 00 */
325 0x0000, 0x0000, 0x0000, 0x0000, /* 04 */
326 0x00a3, 0x0103, 0x0000, 0x0000, /* 08 */
327 0x0000, 0x1f00, 0x0000, 0x0000, /* 0c */
328 0x0108, 0x038d, 0x0000, 0x0000, /* 10 */
329 0xe078, 0x0001, 0x0040, 0x0018, /* 14 */
330 0x0000, 0x0000, 0x0000, 0x0000, /* 18 */
331 0x0000, 0x0000, 0x0000, 0x0000, /* 1c */
332 0x0000, 0x0000, 0x0000, 0x0000, /* 20 */
333 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
334 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
335 0x0000, 0x0000, 0x0000, 0x0000, /* 2c */
336 0x0000, 0x0000, 0x0000, 0x0000, /* 30 */
337 0x0000, 0x0000, 0x0000, 0x0000, /* 34 */
338 0x0000, 0x0000, 0x0000, 0x0000, /* 38 */
339 0x0000, 0x0000, 0x0000, 0x4e07, /* 3c */
340 };
341 uchar enetaddr[6];
342 int i;
343
344 /* Ethernet Addr... */
345 if (!eth_env_get_enetaddr("ethaddr", enetaddr))
346 return;
347
348 eeprom[0x0a] = (enetaddr[1] << 8) | enetaddr[0];
349 eeprom[0x0b] = (enetaddr[3] << 8) | enetaddr[2];
350 eeprom[0x0c] = (enetaddr[5] << 8) | enetaddr[4];
351
352 for (i = 0; i < 0x40; i++)
353 write_srom(dev, DE4X5_APROM, i, eeprom[i]);
354}
355#endif /* UPDATE_SROM */
356
357static void send_setup_frame(struct eth_device *dev, bd_t *bis)
358{
359 char setup_frame[SETUP_FRAME_LEN];
360 char *pa = &setup_frame[0];
361 int i;
362
363 memset(pa, 0xff, SETUP_FRAME_LEN);
364
365 for (i = 0; i < ETH_ALEN; i++) {
366 *(pa + (i & 1)) = dev->enetaddr[i];
367 if (i & 0x01)
368 pa += 4;
wdenkc6097192002-11-03 00:24:07 +0000369 }
370
Marek Vasut5e2ad052020-04-19 04:00:49 +0200371 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
372 if (i < TOUT_LOOP)
373 continue;
wdenkc6097192002-11-03 00:24:07 +0000374
Marek Vasut5e2ad052020-04-19 04:00:49 +0200375 printf("%s: tx error buffer not ready\n", dev->name);
376 return;
377 }
wdenkc6097192002-11-03 00:24:07 +0000378
Marek Vasut5e2ad052020-04-19 04:00:49 +0200379 tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32)&setup_frame[0]));
380 tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_SET | SETUP_FRAME_LEN);
381 tx_ring[tx_new].status = cpu_to_le32(T_OWN);
wdenkc6097192002-11-03 00:24:07 +0000382
Marek Vasut5e2ad052020-04-19 04:00:49 +0200383 dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
wdenkc6097192002-11-03 00:24:07 +0000384
Marek Vasut5e2ad052020-04-19 04:00:49 +0200385 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
386 if (i < TOUT_LOOP)
387 continue;
wdenkc6097192002-11-03 00:24:07 +0000388
Marek Vasut5e2ad052020-04-19 04:00:49 +0200389 printf("%s: tx buffer not ready\n", dev->name);
390 return;
391 }
wdenkc6097192002-11-03 00:24:07 +0000392
Marek Vasut5e2ad052020-04-19 04:00:49 +0200393 if (le32_to_cpu(tx_ring[tx_new].status) != 0x7FFFFFFF) {
394 printf("TX error status2 = 0x%08X\n",
395 le32_to_cpu(tx_ring[tx_new].status));
396 }
397
398 tx_new = (tx_new + 1) % NUM_TX_DESC;
wdenkc6097192002-11-03 00:24:07 +0000399}
400
Joe Hershbergercfb0cee2012-05-21 14:45:22 +0000401static int dc21x4x_send(struct eth_device *dev, void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000402{
Marek Vasute3ffef32020-04-19 03:10:14 +0200403 int status = -1;
404 int i;
wdenkc6097192002-11-03 00:24:07 +0000405
406 if (length <= 0) {
407 printf("%s: bad packet size: %d\n", dev->name, length);
Marek Vasute3ffef32020-04-19 03:10:14 +0200408 goto done;
wdenkc6097192002-11-03 00:24:07 +0000409 }
410
Marek Vasute3ffef32020-04-19 03:10:14 +0200411 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
412 if (i < TOUT_LOOP)
413 continue;
414
415 printf("%s: tx error buffer not ready\n", dev->name);
416 goto done;
wdenkc6097192002-11-03 00:24:07 +0000417 }
418
Marek Vasute3ffef32020-04-19 03:10:14 +0200419 tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32)packet));
420 tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_LS | TD_FS | length);
wdenkc6097192002-11-03 00:24:07 +0000421 tx_ring[tx_new].status = cpu_to_le32(T_OWN);
422
Marek Vasutf02b7012020-04-19 03:40:03 +0200423 dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
wdenkc6097192002-11-03 00:24:07 +0000424
Marek Vasute3ffef32020-04-19 03:10:14 +0200425 for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
426 if (i < TOUT_LOOP)
427 continue;
428
429 printf(".%s: tx buffer not ready\n", dev->name);
430 goto done;
wdenkc6097192002-11-03 00:24:07 +0000431 }
432
433 if (le32_to_cpu(tx_ring[tx_new].status) & TD_ES) {
wdenk0260cd62004-01-02 15:01:32 +0000434 tx_ring[tx_new].status = 0x0;
Marek Vasute3ffef32020-04-19 03:10:14 +0200435 goto done;
wdenkc6097192002-11-03 00:24:07 +0000436 }
437
438 status = length;
439
Marek Vasute3ffef32020-04-19 03:10:14 +0200440done:
441 tx_new = (tx_new + 1) % NUM_TX_DESC;
wdenkc6097192002-11-03 00:24:07 +0000442 return status;
443}
444
Marek Vasutf30abb72020-04-19 03:10:25 +0200445static int dc21x4x_recv(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000446{
Marek Vasutf30abb72020-04-19 03:10:25 +0200447 int length = 0;
448 u32 status;
wdenkc6097192002-11-03 00:24:07 +0000449
Marek Vasutf30abb72020-04-19 03:10:25 +0200450 while (true) {
451 status = le32_to_cpu(rx_ring[rx_new].status);
wdenkc6097192002-11-03 00:24:07 +0000452
Marek Vasutf30abb72020-04-19 03:10:25 +0200453 if (status & R_OWN)
wdenkc6097192002-11-03 00:24:07 +0000454 break;
wdenkc6097192002-11-03 00:24:07 +0000455
456 if (status & RD_LS) {
Marek Vasutf30abb72020-04-19 03:10:25 +0200457 /* Valid frame status. */
wdenkc6097192002-11-03 00:24:07 +0000458 if (status & RD_ES) {
Marek Vasutf30abb72020-04-19 03:10:25 +0200459 /* There was an error. */
wdenkc6097192002-11-03 00:24:07 +0000460 printf("RX error status = 0x%08X\n", status);
461 } else {
Marek Vasutf30abb72020-04-19 03:10:25 +0200462 /* A valid frame received. */
463 length = (le32_to_cpu(rx_ring[rx_new].status)
464 >> 16);
wdenkc6097192002-11-03 00:24:07 +0000465
Marek Vasutf30abb72020-04-19 03:10:25 +0200466 /* Pass the packet up to the protocol layers */
467 net_process_received_packet
468 (net_rx_packets[rx_new], length - 4);
wdenkc6097192002-11-03 00:24:07 +0000469 }
470
Marek Vasutf30abb72020-04-19 03:10:25 +0200471 /*
472 * Change buffer ownership for this frame,
473 * back to the adapter.
wdenkc6097192002-11-03 00:24:07 +0000474 */
475 rx_ring[rx_new].status = cpu_to_le32(R_OWN);
476 }
477
Marek Vasutf30abb72020-04-19 03:10:25 +0200478 /* Update entry information. */
Marek Vasut81d10f72020-04-19 03:09:26 +0200479 rx_new = (rx_new + 1) % rx_ring_size;
wdenkc6097192002-11-03 00:24:07 +0000480 }
481
482 return length;
483}
484
Marek Vasut5e2ad052020-04-19 04:00:49 +0200485static int dc21x4x_init(struct eth_device *dev, bd_t *bis)
wdenkc6097192002-11-03 00:24:07 +0000486{
Marek Vasut5e2ad052020-04-19 04:00:49 +0200487 int i;
Marek Vasut1f346782020-04-19 03:10:30 +0200488 int devbusfn = (int)dev->priv;
wdenkc6097192002-11-03 00:24:07 +0000489
Marek Vasut5e2ad052020-04-19 04:00:49 +0200490 /* Ensure we're not sleeping. */
491 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
wdenkc6097192002-11-03 00:24:07 +0000492
Marek Vasut5e2ad052020-04-19 04:00:49 +0200493 reset_de4x5(dev);
wdenkc6097192002-11-03 00:24:07 +0000494
Marek Vasut5e2ad052020-04-19 04:00:49 +0200495 if (dc2114x_inl(dev, DE4X5_STS) & (STS_TS | STS_RS)) {
496 printf("Error: Cannot reset ethernet controller.\n");
497 return -1;
498 }
wdenkc6097192002-11-03 00:24:07 +0000499
Marek Vasut5e2ad052020-04-19 04:00:49 +0200500 dc2114x_outl(dev, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
wdenkc6097192002-11-03 00:24:07 +0000501
Marek Vasut5e2ad052020-04-19 04:00:49 +0200502 for (i = 0; i < NUM_RX_DESC; i++) {
503 rx_ring[i].status = cpu_to_le32(R_OWN);
504 rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
505 rx_ring[i].buf =
506 cpu_to_le32(phys_to_bus((u32)net_rx_packets[i]));
507 rx_ring[i].next = 0;
wdenkc6097192002-11-03 00:24:07 +0000508 }
509
Marek Vasut5e2ad052020-04-19 04:00:49 +0200510 for (i = 0; i < NUM_TX_DESC; i++) {
511 tx_ring[i].status = 0;
512 tx_ring[i].des1 = 0;
513 tx_ring[i].buf = 0;
514 tx_ring[i].next = 0;
wdenkc6097192002-11-03 00:24:07 +0000515 }
516
Marek Vasut5e2ad052020-04-19 04:00:49 +0200517 rx_ring_size = NUM_RX_DESC;
518 tx_ring_size = NUM_TX_DESC;
wdenkc6097192002-11-03 00:24:07 +0000519
Marek Vasut5e2ad052020-04-19 04:00:49 +0200520 /* Write the end of list marker to the descriptor lists. */
521 rx_ring[rx_ring_size - 1].des1 |= cpu_to_le32(RD_RER);
522 tx_ring[tx_ring_size - 1].des1 |= cpu_to_le32(TD_TER);
wdenkc6097192002-11-03 00:24:07 +0000523
Marek Vasut5e2ad052020-04-19 04:00:49 +0200524 /* Tell the adapter where the TX/RX rings are located. */
525 dc2114x_outl(dev, phys_to_bus((u32)&rx_ring), DE4X5_RRBA);
526 dc2114x_outl(dev, phys_to_bus((u32)&tx_ring), DE4X5_TRBA);
Marek Vasute13635a2020-04-19 03:10:50 +0200527
Marek Vasut5e2ad052020-04-19 04:00:49 +0200528 start_de4x5(dev);
wdenkc6097192002-11-03 00:24:07 +0000529
Marek Vasut5e2ad052020-04-19 04:00:49 +0200530 tx_new = 0;
531 rx_new = 0;
wdenk0260cd62004-01-02 15:01:32 +0000532
Marek Vasut5e2ad052020-04-19 04:00:49 +0200533 send_setup_frame(dev, bis);
wdenkc6097192002-11-03 00:24:07 +0000534
Marek Vasut5e2ad052020-04-19 04:00:49 +0200535 return 0;
wdenkc6097192002-11-03 00:24:07 +0000536}
537
Marek Vasut5e2ad052020-04-19 04:00:49 +0200538static void dc21x4x_halt(struct eth_device *dev)
wdenkc6097192002-11-03 00:24:07 +0000539{
Marek Vasut5e2ad052020-04-19 04:00:49 +0200540 int devbusfn = (int)dev->priv;
wdenkc6097192002-11-03 00:24:07 +0000541
Marek Vasut5e2ad052020-04-19 04:00:49 +0200542 stop_de4x5(dev);
543 dc2114x_outl(dev, 0, DE4X5_SICR);
544
545 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, SLEEP);
wdenkc6097192002-11-03 00:24:07 +0000546}
547
Marek Vasut5e2ad052020-04-19 04:00:49 +0200548static void read_hw_addr(struct eth_device *dev, bd_t *bis)
wdenkc6097192002-11-03 00:24:07 +0000549{
Marek Vasut5e2ad052020-04-19 04:00:49 +0200550 u_short tmp, *p = (u_short *)(&dev->enetaddr[0]);
551 int i, j = 0;
Marek Vasutb46c7a02020-04-19 03:11:06 +0200552
Marek Vasut5e2ad052020-04-19 04:00:49 +0200553 for (i = 0; i < (ETH_ALEN >> 1); i++) {
554 tmp = read_srom(dev, DE4X5_APROM, (SROM_HWADD >> 1) + i);
555 *p = le16_to_cpu(tmp);
556 j += *p++;
wdenkc6097192002-11-03 00:24:07 +0000557 }
558
Marek Vasut5e2ad052020-04-19 04:00:49 +0200559 if (!j || j == 0x2fffd) {
560 memset(dev->enetaddr, 0, ETH_ALEN);
561 debug("Warning: can't read HW address from SROM.\n");
562#ifdef UPDATE_SROM
563 update_srom(dev, bis);
wdenkc6097192002-11-03 00:24:07 +0000564#endif
wdenkc6097192002-11-03 00:24:07 +0000565 }
wdenkc6097192002-11-03 00:24:07 +0000566}
567
Marek Vasut5e2ad052020-04-19 04:00:49 +0200568static struct pci_device_id supported[] = {
569 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST },
570 { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142 },
571 { }
572};
wdenkc6097192002-11-03 00:24:07 +0000573
Marek Vasut5e2ad052020-04-19 04:00:49 +0200574int dc21x4x_initialize(bd_t *bis)
wdenkc6097192002-11-03 00:24:07 +0000575{
Marek Vasut5e2ad052020-04-19 04:00:49 +0200576 struct eth_device *dev;
577 unsigned short status;
578 unsigned char timer;
579 unsigned int iobase;
580 int card_number = 0;
581 pci_dev_t devbusfn;
582 unsigned int cfrv;
583 int idx = 0;
Marek Vasutb46c7a02020-04-19 03:11:06 +0200584
Marek Vasut5e2ad052020-04-19 04:00:49 +0200585 while (1) {
586 devbusfn = pci_find_devices(supported, idx++);
587 if (devbusfn == -1)
588 break;
wdenkc6097192002-11-03 00:24:07 +0000589
Marek Vasut5e2ad052020-04-19 04:00:49 +0200590 /* Get the chip configuration revision register. */
591 pci_read_config_dword(devbusfn, PCI_REVISION_ID, &cfrv);
wdenkc6097192002-11-03 00:24:07 +0000592
Marek Vasut5e2ad052020-04-19 04:00:49 +0200593 if ((cfrv & CFRV_RN) < DC2114x_BRK) {
594 printf("Error: The chip is not DC21143.\n");
595 continue;
596 }
Marek Vasutb46c7a02020-04-19 03:11:06 +0200597
Marek Vasut5e2ad052020-04-19 04:00:49 +0200598 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
599 status |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
600 pci_write_config_word(devbusfn, PCI_COMMAND, status);
wdenkc6097192002-11-03 00:24:07 +0000601
Marek Vasut5e2ad052020-04-19 04:00:49 +0200602 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
603 if (!(status & PCI_COMMAND_MEMORY)) {
604 printf("Error: Can not enable MEMORY access.\n");
605 continue;
606 }
wdenkc6097192002-11-03 00:24:07 +0000607
Marek Vasut5e2ad052020-04-19 04:00:49 +0200608 if (!(status & PCI_COMMAND_MASTER)) {
609 printf("Error: Can not enable Bus Mastering.\n");
610 continue;
611 }
wdenkc6097192002-11-03 00:24:07 +0000612
Marek Vasut5e2ad052020-04-19 04:00:49 +0200613 /* Check the latency timer for values >= 0x60. */
614 pci_read_config_byte(devbusfn, PCI_LATENCY_TIMER, &timer);
wdenkc6097192002-11-03 00:24:07 +0000615
Marek Vasut5e2ad052020-04-19 04:00:49 +0200616 if (timer < 0x60) {
617 pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER,
618 0x60);
619 }
wdenkc6097192002-11-03 00:24:07 +0000620
Marek Vasut5e2ad052020-04-19 04:00:49 +0200621 /* read BAR for memory space access */
622 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &iobase);
623 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
624 debug("dc21x4x: DEC 21142 PCI Device @0x%x\n", iobase);
wdenkc6097192002-11-03 00:24:07 +0000625
Marek Vasut5e2ad052020-04-19 04:00:49 +0200626 dev = (struct eth_device *)malloc(sizeof(*dev));
627 if (!dev) {
628 printf("Can not allocalte memory of dc21x4x\n");
629 break;
630 }
wdenkc6097192002-11-03 00:24:07 +0000631
Marek Vasut5e2ad052020-04-19 04:00:49 +0200632 memset(dev, 0, sizeof(*dev));
Marek Vasutb46c7a02020-04-19 03:11:06 +0200633
Marek Vasut5e2ad052020-04-19 04:00:49 +0200634 sprintf(dev->name, "dc21x4x#%d", card_number);
wdenkc6097192002-11-03 00:24:07 +0000635
Marek Vasut5e2ad052020-04-19 04:00:49 +0200636 dev->iobase = pci_mem_to_phys(devbusfn, iobase);
637 dev->priv = (void *)devbusfn;
638 dev->init = dc21x4x_init;
639 dev->halt = dc21x4x_halt;
640 dev->send = dc21x4x_send;
641 dev->recv = dc21x4x_recv;
wdenkc6097192002-11-03 00:24:07 +0000642
Marek Vasut5e2ad052020-04-19 04:00:49 +0200643 /* Ensure we're not sleeping. */
644 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
wdenkc6097192002-11-03 00:24:07 +0000645
Marek Vasut5e2ad052020-04-19 04:00:49 +0200646 udelay(10 * 1000);
wdenkc6097192002-11-03 00:24:07 +0000647
Marek Vasut5e2ad052020-04-19 04:00:49 +0200648 read_hw_addr(dev, bis);
wdenkc6097192002-11-03 00:24:07 +0000649
Marek Vasut5e2ad052020-04-19 04:00:49 +0200650 eth_register(dev);
Marek Vasutb46c7a02020-04-19 03:11:06 +0200651
Marek Vasut5e2ad052020-04-19 04:00:49 +0200652 card_number++;
653 }
wdenkc6097192002-11-03 00:24:07 +0000654
Marek Vasut5e2ad052020-04-19 04:00:49 +0200655 return card_number;
wdenkc6097192002-11-03 00:24:07 +0000656}