blob: d9ab37b336618d37adaa1c20b0764187ad473f33 [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 * (C) Copyright 2002 Wolfgang Grandegger, wg@denx.de.
4 *
5 * This driver for AMD PCnet network controllers is derived from the
6 * Linux driver pcnet32.c written 1996-1999 by Thomas Bogendoerfer.
wdenkc6097192002-11-03 00:24:07 +00007 */
8
9#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Marek Vasutcf8ec982020-05-17 17:43:22 +020012#include <dm.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <malloc.h>
Marek Vasut60675d42020-05-17 16:16:45 +020014#include <memalign.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <net.h>
Ben Warrenb794a932008-08-31 10:08:43 -070016#include <netdev.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
wdenkc6097192002-11-03 00:24:07 +000018#include <asm/io.h>
19#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
wdenkc6097192002-11-03 00:24:07 +000021
Wolfgang Denk39158312008-04-24 23:44:26 +020022#define PCNET_DEBUG_LEVEL 0 /* 0=off, 1=init, 2=rx/tx */
wdenkc6097192002-11-03 00:24:07 +000023
Wolfgang Denk99726cc2011-11-05 05:12:58 +000024#define PCNET_DEBUG1(fmt,args...) \
25 debug_cond(PCNET_DEBUG_LEVEL > 0, fmt ,##args)
26#define PCNET_DEBUG2(fmt,args...) \
27 debug_cond(PCNET_DEBUG_LEVEL > 1, fmt ,##args)
wdenkc6097192002-11-03 00:24:07 +000028
wdenkc6097192002-11-03 00:24:07 +000029/*
30 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
31 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
32 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
33 */
34#define PCNET_LOG_TX_BUFFERS 0
35#define PCNET_LOG_RX_BUFFERS 2
36
37#define TX_RING_SIZE (1 << (PCNET_LOG_TX_BUFFERS))
38#define TX_RING_LEN_BITS ((PCNET_LOG_TX_BUFFERS) << 12)
39
40#define RX_RING_SIZE (1 << (PCNET_LOG_RX_BUFFERS))
41#define RX_RING_LEN_BITS ((PCNET_LOG_RX_BUFFERS) << 4)
42
43#define PKT_BUF_SZ 1544
44
45/* The PCNET Rx and Tx ring descriptors. */
46struct pcnet_rx_head {
Wolfgang Denk39158312008-04-24 23:44:26 +020047 u32 base;
48 s16 buf_length;
49 s16 status;
50 u32 msg_length;
51 u32 reserved;
wdenkc6097192002-11-03 00:24:07 +000052};
53
54struct pcnet_tx_head {
Wolfgang Denk39158312008-04-24 23:44:26 +020055 u32 base;
56 s16 length;
57 s16 status;
58 u32 misc;
59 u32 reserved;
wdenkc6097192002-11-03 00:24:07 +000060};
61
62/* The PCNET 32-Bit initialization block, described in databook. */
63struct pcnet_init_block {
Wolfgang Denk39158312008-04-24 23:44:26 +020064 u16 mode;
65 u16 tlen_rlen;
66 u8 phys_addr[6];
67 u16 reserved;
68 u32 filter[2];
69 /* Receive and transmit ring base, along with extra bits. */
70 u32 rx_ring;
71 u32 tx_ring;
72 u32 reserved2;
wdenkc6097192002-11-03 00:24:07 +000073};
74
Paul Burton52505922014-04-07 16:41:46 +010075struct pcnet_uncached_priv {
Wolfgang Denk39158312008-04-24 23:44:26 +020076 struct pcnet_rx_head rx_ring[RX_RING_SIZE];
77 struct pcnet_tx_head tx_ring[TX_RING_SIZE];
78 struct pcnet_init_block init_block;
Marek Vasut60675d42020-05-17 16:16:45 +020079} __aligned(ARCH_DMA_MINALIGN);
Paul Burton52505922014-04-07 16:41:46 +010080
Marek Vasutb346e1b2020-05-17 15:10:41 +020081struct pcnet_priv {
Marek Vasut60675d42020-05-17 16:16:45 +020082 struct pcnet_uncached_priv ucp;
Wolfgang Denk39158312008-04-24 23:44:26 +020083 /* Receive Buffer space */
Marek Vasut60675d42020-05-17 16:16:45 +020084 unsigned char rx_buf[RX_RING_SIZE][PKT_BUF_SZ + 4];
85 struct pcnet_uncached_priv *uc;
Marek Vasutcf8ec982020-05-17 17:43:22 +020086#ifdef CONFIG_DM_ETH
87 struct udevice *dev;
88 const char *name;
89#else
Marek Vasut2b1c26a2020-05-17 16:31:04 +020090 pci_dev_t dev;
Marek Vasut5cfe7be2020-05-17 17:04:19 +020091 char *name;
Marek Vasutcf8ec982020-05-17 17:43:22 +020092#endif
93 void __iomem *iobase;
Marek Vasut5cfe7be2020-05-17 17:04:19 +020094 u8 *enetaddr;
Marek Vasut03304cc2020-05-17 17:28:31 +020095 u16 status;
Wolfgang Denk39158312008-04-24 23:44:26 +020096 int cur_rx;
97 int cur_tx;
Marek Vasutb346e1b2020-05-17 15:10:41 +020098};
wdenkc6097192002-11-03 00:24:07 +000099
wdenkc6097192002-11-03 00:24:07 +0000100/* Offsets from base I/O address for WIO mode */
101#define PCNET_RDP 0x10
102#define PCNET_RAP 0x12
103#define PCNET_RESET 0x14
104#define PCNET_BDP 0x16
105
Marek Vasutf7377ad2020-05-17 17:00:42 +0200106static u16 pcnet_read_csr(struct pcnet_priv *lp, int index)
wdenkc6097192002-11-03 00:24:07 +0000107{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200108 writew(index, lp->iobase + PCNET_RAP);
109 return readw(lp->iobase + PCNET_RDP);
wdenkc6097192002-11-03 00:24:07 +0000110}
111
Marek Vasutf7377ad2020-05-17 17:00:42 +0200112static void pcnet_write_csr(struct pcnet_priv *lp, int index, u16 val)
wdenkc6097192002-11-03 00:24:07 +0000113{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200114 writew(index, lp->iobase + PCNET_RAP);
115 writew(val, lp->iobase + PCNET_RDP);
wdenkc6097192002-11-03 00:24:07 +0000116}
117
Marek Vasutf7377ad2020-05-17 17:00:42 +0200118static u16 pcnet_read_bcr(struct pcnet_priv *lp, int index)
wdenkc6097192002-11-03 00:24:07 +0000119{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200120 writew(index, lp->iobase + PCNET_RAP);
121 return readw(lp->iobase + PCNET_BDP);
wdenkc6097192002-11-03 00:24:07 +0000122}
123
Marek Vasutf7377ad2020-05-17 17:00:42 +0200124static void pcnet_write_bcr(struct pcnet_priv *lp, int index, u16 val)
wdenkc6097192002-11-03 00:24:07 +0000125{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200126 writew(index, lp->iobase + PCNET_RAP);
127 writew(val, lp->iobase + PCNET_BDP);
wdenkc6097192002-11-03 00:24:07 +0000128}
129
Marek Vasutf7377ad2020-05-17 17:00:42 +0200130static void pcnet_reset(struct pcnet_priv *lp)
wdenkc6097192002-11-03 00:24:07 +0000131{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200132 readw(lp->iobase + PCNET_RESET);
wdenkc6097192002-11-03 00:24:07 +0000133}
134
Marek Vasutf7377ad2020-05-17 17:00:42 +0200135static int pcnet_check(struct pcnet_priv *lp)
wdenkc6097192002-11-03 00:24:07 +0000136{
Marek Vasutf7377ad2020-05-17 17:00:42 +0200137 writew(88, lp->iobase + PCNET_RAP);
138 return readw(lp->iobase + PCNET_RAP) == 88;
wdenkc6097192002-11-03 00:24:07 +0000139}
140
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200141static inline pci_addr_t pcnet_virt_to_mem(struct pcnet_priv *lp, void *addr)
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100142{
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100143 void *virt_addr = addr;
144
Marek Vasutcf8ec982020-05-17 17:43:22 +0200145#ifdef CONFIG_DM_ETH
146 return dm_pci_virt_to_mem(lp->dev, virt_addr);
147#else
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200148 return pci_virt_to_mem(lp->dev, virt_addr);
Marek Vasutcf8ec982020-05-17 17:43:22 +0200149#endif
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100150}
wdenkc6097192002-11-03 00:24:07 +0000151
152static struct pci_device_id supported[] = {
Marek Vasute2ea3612020-05-17 17:33:17 +0200153 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE) },
Wolfgang Denk39158312008-04-24 23:44:26 +0200154 {}
wdenkc6097192002-11-03 00:24:07 +0000155};
156
Marek Vasut03304cc2020-05-17 17:28:31 +0200157static int pcnet_probe_common(struct pcnet_priv *lp)
wdenkc6097192002-11-03 00:24:07 +0000158{
Wolfgang Denk39158312008-04-24 23:44:26 +0200159 int chip_version;
160 char *chipname;
Wolfgang Denk39158312008-04-24 23:44:26 +0200161 int i;
wdenkc6097192002-11-03 00:24:07 +0000162
Wolfgang Denk39158312008-04-24 23:44:26 +0200163 /* Reset the PCnet controller */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200164 pcnet_reset(lp);
wdenkc6097192002-11-03 00:24:07 +0000165
Wolfgang Denk39158312008-04-24 23:44:26 +0200166 /* Check if register access is working */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200167 if (pcnet_read_csr(lp, 0) != 4 || !pcnet_check(lp)) {
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200168 printf("%s: CSR register access check failed\n", lp->name);
Wolfgang Denk39158312008-04-24 23:44:26 +0200169 return -1;
170 }
wdenkc6097192002-11-03 00:24:07 +0000171
Wolfgang Denk39158312008-04-24 23:44:26 +0200172 /* Identify the chip */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200173 chip_version = pcnet_read_csr(lp, 88) | (pcnet_read_csr(lp, 89) << 16);
Wolfgang Denk39158312008-04-24 23:44:26 +0200174 if ((chip_version & 0xfff) != 0x003)
175 return -1;
176 chip_version = (chip_version >> 12) & 0xffff;
177 switch (chip_version) {
178 case 0x2621:
179 chipname = "PCnet/PCI II 79C970A"; /* PCI */
180 break;
Wolfgang Denk39158312008-04-24 23:44:26 +0200181 case 0x2625:
182 chipname = "PCnet/FAST III 79C973"; /* PCI */
183 break;
Wolfgang Denk39158312008-04-24 23:44:26 +0200184 case 0x2627:
185 chipname = "PCnet/FAST III 79C975"; /* PCI */
186 break;
Wolfgang Denk39158312008-04-24 23:44:26 +0200187 default:
Paul Burton70ab8c02013-11-08 11:18:43 +0000188 printf("%s: PCnet version %#x not supported\n",
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200189 lp->name, chip_version);
Wolfgang Denk39158312008-04-24 23:44:26 +0200190 return -1;
191 }
wdenkc6097192002-11-03 00:24:07 +0000192
Paul Burton70ab8c02013-11-08 11:18:43 +0000193 PCNET_DEBUG1("AMD %s\n", chipname);
wdenkc6097192002-11-03 00:24:07 +0000194
Wolfgang Denk39158312008-04-24 23:44:26 +0200195 /*
196 * In most chips, after a chip reset, the ethernet address is read from
197 * the station address PROM at the base address and programmed into the
198 * "Physical Address Registers" CSR12-14.
199 */
200 for (i = 0; i < 3; i++) {
201 unsigned int val;
202
Marek Vasutf7377ad2020-05-17 17:00:42 +0200203 val = pcnet_read_csr(lp, i + 12) & 0x0ffff;
Wolfgang Denk39158312008-04-24 23:44:26 +0200204 /* There may be endianness issues here. */
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200205 lp->enetaddr[2 * i] = val & 0x0ff;
206 lp->enetaddr[2 * i + 1] = (val >> 8) & 0x0ff;
Wolfgang Denk39158312008-04-24 23:44:26 +0200207 }
wdenkc6097192002-11-03 00:24:07 +0000208
Wolfgang Denk39158312008-04-24 23:44:26 +0200209 return 0;
wdenkc6097192002-11-03 00:24:07 +0000210}
211
Marek Vasut03304cc2020-05-17 17:28:31 +0200212static int pcnet_init_common(struct pcnet_priv *lp)
wdenkc6097192002-11-03 00:24:07 +0000213{
Paul Burton52505922014-04-07 16:41:46 +0100214 struct pcnet_uncached_priv *uc;
Wolfgang Denk39158312008-04-24 23:44:26 +0200215 int i, val;
Paul Burtoned228752016-05-26 14:49:35 +0100216 unsigned long addr;
wdenkc6097192002-11-03 00:24:07 +0000217
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200218 PCNET_DEBUG1("%s: %s...\n", lp->name, __func__);
wdenkc6097192002-11-03 00:24:07 +0000219
Wolfgang Denk39158312008-04-24 23:44:26 +0200220 /* Switch pcnet to 32bit mode */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200221 pcnet_write_bcr(lp, 20, 2);
wdenkc6097192002-11-03 00:24:07 +0000222
Wolfgang Denk39158312008-04-24 23:44:26 +0200223 /* Set/reset autoselect bit */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200224 val = pcnet_read_bcr(lp, 2) & ~2;
Wolfgang Denk39158312008-04-24 23:44:26 +0200225 val |= 2;
Marek Vasutf7377ad2020-05-17 17:00:42 +0200226 pcnet_write_bcr(lp, 2, val);
wdenkc6097192002-11-03 00:24:07 +0000227
Wolfgang Denk39158312008-04-24 23:44:26 +0200228 /* Enable auto negotiate, setup, disable fd */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200229 val = pcnet_read_bcr(lp, 32) & ~0x98;
Wolfgang Denk39158312008-04-24 23:44:26 +0200230 val |= 0x20;
Marek Vasutf7377ad2020-05-17 17:00:42 +0200231 pcnet_write_bcr(lp, 32, val);
wdenkc6097192002-11-03 00:24:07 +0000232
Wolfgang Denk39158312008-04-24 23:44:26 +0200233 /*
Paul Burton03261c02013-11-08 11:18:46 +0000234 * Enable NOUFLO on supported controllers, with the transmit
235 * start point set to the full packet. This will cause entire
236 * packets to be buffered by the ethernet controller before
237 * transmission, eliminating underflows which are common on
238 * slower devices. Controllers which do not support NOUFLO will
239 * simply be left with a larger transmit FIFO threshold.
240 */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200241 val = pcnet_read_bcr(lp, 18);
Paul Burton03261c02013-11-08 11:18:46 +0000242 val |= 1 << 11;
Marek Vasutf7377ad2020-05-17 17:00:42 +0200243 pcnet_write_bcr(lp, 18, val);
244 val = pcnet_read_csr(lp, 80);
Paul Burton03261c02013-11-08 11:18:46 +0000245 val |= 0x3 << 10;
Marek Vasutf7377ad2020-05-17 17:00:42 +0200246 pcnet_write_csr(lp, 80, val);
Paul Burton03261c02013-11-08 11:18:46 +0000247
Paul Burton52505922014-04-07 16:41:46 +0100248 uc = lp->uc;
249
250 uc->init_block.mode = cpu_to_le16(0x0000);
251 uc->init_block.filter[0] = 0x00000000;
252 uc->init_block.filter[1] = 0x00000000;
wdenkc6097192002-11-03 00:24:07 +0000253
Wolfgang Denk39158312008-04-24 23:44:26 +0200254 /*
255 * Initialize the Rx ring.
256 */
257 lp->cur_rx = 0;
258 for (i = 0; i < RX_RING_SIZE; i++) {
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200259 addr = pcnet_virt_to_mem(lp, lp->rx_buf[i]);
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100260 uc->rx_ring[i].base = cpu_to_le32(addr);
Paul Burton52505922014-04-07 16:41:46 +0100261 uc->rx_ring[i].buf_length = cpu_to_le16(-PKT_BUF_SZ);
262 uc->rx_ring[i].status = cpu_to_le16(0x8000);
Wolfgang Denk39158312008-04-24 23:44:26 +0200263 PCNET_DEBUG1
264 ("Rx%d: base=0x%x buf_length=0x%hx status=0x%hx\n", i,
Paul Burton52505922014-04-07 16:41:46 +0100265 uc->rx_ring[i].base, uc->rx_ring[i].buf_length,
266 uc->rx_ring[i].status);
Wolfgang Denk39158312008-04-24 23:44:26 +0200267 }
wdenkc6097192002-11-03 00:24:07 +0000268
Wolfgang Denk39158312008-04-24 23:44:26 +0200269 /*
270 * Initialize the Tx ring. The Tx buffer address is filled in as
271 * needed, but we do need to clear the upper ownership bit.
272 */
273 lp->cur_tx = 0;
274 for (i = 0; i < TX_RING_SIZE; i++) {
Paul Burton52505922014-04-07 16:41:46 +0100275 uc->tx_ring[i].base = 0;
276 uc->tx_ring[i].status = 0;
Wolfgang Denk39158312008-04-24 23:44:26 +0200277 }
wdenkc6097192002-11-03 00:24:07 +0000278
Wolfgang Denk39158312008-04-24 23:44:26 +0200279 /*
280 * Setup Init Block.
281 */
Paul Burton52505922014-04-07 16:41:46 +0100282 PCNET_DEBUG1("Init block at 0x%p: MAC", &lp->uc->init_block);
wdenkc6097192002-11-03 00:24:07 +0000283
Wolfgang Denk39158312008-04-24 23:44:26 +0200284 for (i = 0; i < 6; i++) {
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200285 lp->uc->init_block.phys_addr[i] = lp->enetaddr[i];
Paul Burton52505922014-04-07 16:41:46 +0100286 PCNET_DEBUG1(" %02x", lp->uc->init_block.phys_addr[i]);
Wolfgang Denk39158312008-04-24 23:44:26 +0200287 }
wdenkc6097192002-11-03 00:24:07 +0000288
Paul Burton52505922014-04-07 16:41:46 +0100289 uc->init_block.tlen_rlen = cpu_to_le16(TX_RING_LEN_BITS |
Paul Burton70ab8c02013-11-08 11:18:43 +0000290 RX_RING_LEN_BITS);
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200291 addr = pcnet_virt_to_mem(lp, uc->rx_ring);
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100292 uc->init_block.rx_ring = cpu_to_le32(addr);
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200293 addr = pcnet_virt_to_mem(lp, uc->tx_ring);
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100294 uc->init_block.tx_ring = cpu_to_le32(addr);
wdenkc6097192002-11-03 00:24:07 +0000295
Paul Burton70ab8c02013-11-08 11:18:43 +0000296 PCNET_DEBUG1("\ntlen_rlen=0x%x rx_ring=0x%x tx_ring=0x%x\n",
Paul Burton52505922014-04-07 16:41:46 +0100297 uc->init_block.tlen_rlen,
298 uc->init_block.rx_ring, uc->init_block.tx_ring);
wdenkc6097192002-11-03 00:24:07 +0000299
Wolfgang Denk39158312008-04-24 23:44:26 +0200300 /*
301 * Tell the controller where the Init Block is located.
302 */
Paul Burton52505922014-04-07 16:41:46 +0100303 barrier();
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200304 addr = pcnet_virt_to_mem(lp, &lp->uc->init_block);
Marek Vasutf7377ad2020-05-17 17:00:42 +0200305 pcnet_write_csr(lp, 1, addr & 0xffff);
306 pcnet_write_csr(lp, 2, (addr >> 16) & 0xffff);
wdenkc6097192002-11-03 00:24:07 +0000307
Marek Vasutf7377ad2020-05-17 17:00:42 +0200308 pcnet_write_csr(lp, 4, 0x0915);
309 pcnet_write_csr(lp, 0, 0x0001); /* start */
wdenkc6097192002-11-03 00:24:07 +0000310
Wolfgang Denk39158312008-04-24 23:44:26 +0200311 /* Wait for Init Done bit */
312 for (i = 10000; i > 0; i--) {
Marek Vasutf7377ad2020-05-17 17:00:42 +0200313 if (pcnet_read_csr(lp, 0) & 0x0100)
Wolfgang Denk39158312008-04-24 23:44:26 +0200314 break;
Paul Burton70ab8c02013-11-08 11:18:43 +0000315 udelay(10);
Wolfgang Denk39158312008-04-24 23:44:26 +0200316 }
317 if (i <= 0) {
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200318 printf("%s: TIMEOUT: controller init failed\n", lp->name);
Marek Vasutf7377ad2020-05-17 17:00:42 +0200319 pcnet_reset(lp);
Wolfgang Denk39158312008-04-24 23:44:26 +0200320 return -1;
321 }
wdenkc6097192002-11-03 00:24:07 +0000322
Wolfgang Denk39158312008-04-24 23:44:26 +0200323 /*
324 * Finally start network controller operation.
325 */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200326 pcnet_write_csr(lp, 0, 0x0002);
wdenkc6097192002-11-03 00:24:07 +0000327
Wolfgang Denk39158312008-04-24 23:44:26 +0200328 return 0;
wdenkc6097192002-11-03 00:24:07 +0000329}
330
Marek Vasut03304cc2020-05-17 17:28:31 +0200331static int pcnet_send_common(struct pcnet_priv *lp, void *packet, int pkt_len)
wdenkc6097192002-11-03 00:24:07 +0000332{
Wolfgang Denk39158312008-04-24 23:44:26 +0200333 int i, status;
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100334 u32 addr;
Paul Burton52505922014-04-07 16:41:46 +0100335 struct pcnet_tx_head *entry = &lp->uc->tx_ring[lp->cur_tx];
wdenkc6097192002-11-03 00:24:07 +0000336
Paul Burton70ab8c02013-11-08 11:18:43 +0000337 PCNET_DEBUG2("Tx%d: %d bytes from 0x%p ", lp->cur_tx, pkt_len,
338 packet);
wdenkc6097192002-11-03 00:24:07 +0000339
Paul Burton5edb7d82013-11-08 11:18:45 +0000340 flush_dcache_range((unsigned long)packet,
341 (unsigned long)packet + pkt_len);
342
Wolfgang Denk39158312008-04-24 23:44:26 +0200343 /* Wait for completion by testing the OWN bit */
344 for (i = 1000; i > 0; i--) {
Paul Burton14e47402014-04-07 16:41:48 +0100345 status = readw(&entry->status);
Wolfgang Denk39158312008-04-24 23:44:26 +0200346 if ((status & 0x8000) == 0)
347 break;
Paul Burton70ab8c02013-11-08 11:18:43 +0000348 udelay(100);
349 PCNET_DEBUG2(".");
Wolfgang Denk39158312008-04-24 23:44:26 +0200350 }
351 if (i <= 0) {
Paul Burton70ab8c02013-11-08 11:18:43 +0000352 printf("%s: TIMEOUT: Tx%d failed (status = 0x%x)\n",
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200353 lp->name, lp->cur_tx, status);
Wolfgang Denk39158312008-04-24 23:44:26 +0200354 pkt_len = 0;
355 goto failure;
356 }
wdenkc6097192002-11-03 00:24:07 +0000357
Wolfgang Denk39158312008-04-24 23:44:26 +0200358 /*
359 * Setup Tx ring. Caution: the write order is important here,
360 * set the status with the "ownership" bits last.
361 */
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200362 addr = pcnet_virt_to_mem(lp, packet);
Paul Burton14e47402014-04-07 16:41:48 +0100363 writew(-pkt_len, &entry->length);
364 writel(0, &entry->misc);
Daniel Schwierzeck9b3e6c62016-01-12 21:48:24 +0100365 writel(addr, &entry->base);
Paul Burton14e47402014-04-07 16:41:48 +0100366 writew(0x8300, &entry->status);
wdenkc6097192002-11-03 00:24:07 +0000367
Wolfgang Denk39158312008-04-24 23:44:26 +0200368 /* Trigger an immediate send poll. */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200369 pcnet_write_csr(lp, 0, 0x0008);
wdenkc6097192002-11-03 00:24:07 +0000370
Wolfgang Denk39158312008-04-24 23:44:26 +0200371 failure:
372 if (++lp->cur_tx >= TX_RING_SIZE)
373 lp->cur_tx = 0;
wdenkc6097192002-11-03 00:24:07 +0000374
Paul Burton70ab8c02013-11-08 11:18:43 +0000375 PCNET_DEBUG2("done\n");
Wolfgang Denk39158312008-04-24 23:44:26 +0200376 return pkt_len;
wdenkc6097192002-11-03 00:24:07 +0000377}
378
Marek Vasut03304cc2020-05-17 17:28:31 +0200379static int pcnet_recv_common(struct pcnet_priv *lp, unsigned char **bufp)
wdenkc6097192002-11-03 00:24:07 +0000380{
Wolfgang Denk39158312008-04-24 23:44:26 +0200381 struct pcnet_rx_head *entry;
Paul Burton7f3c38e2014-04-07 16:41:47 +0100382 unsigned char *buf;
Wolfgang Denk39158312008-04-24 23:44:26 +0200383 int pkt_len = 0;
Marek Vasut03304cc2020-05-17 17:28:31 +0200384 u16 err_status;
wdenkc6097192002-11-03 00:24:07 +0000385
Marek Vasut03304cc2020-05-17 17:28:31 +0200386 entry = &lp->uc->rx_ring[lp->cur_rx];
387 /*
388 * If we own the next entry, it's a new packet. Send it up.
389 */
390 lp->status = readw(&entry->status);
391 if ((lp->status & 0x8000) != 0)
392 return 0;
393 err_status = lp->status >> 8;
wdenkc6097192002-11-03 00:24:07 +0000394
Marek Vasut03304cc2020-05-17 17:28:31 +0200395 if (err_status != 0x03) { /* There was an error. */
396 printf("%s: Rx%d", lp->name, lp->cur_rx);
397 PCNET_DEBUG1(" (status=0x%x)", err_status);
398 if (err_status & 0x20)
399 printf(" Frame");
400 if (err_status & 0x10)
401 printf(" Overflow");
402 if (err_status & 0x08)
403 printf(" CRC");
404 if (err_status & 0x04)
405 printf(" Fifo");
406 printf(" Error\n");
407 lp->status &= 0x03ff;
408 return 0;
409 }
wdenkc6097192002-11-03 00:24:07 +0000410
Marek Vasut03304cc2020-05-17 17:28:31 +0200411 pkt_len = (readl(&entry->msg_length) & 0xfff) - 4;
412 if (pkt_len < 60) {
413 printf("%s: Rx%d: invalid packet length %d\n",
414 lp->name, lp->cur_rx, pkt_len);
415 return 0;
416 }
Paul Burton14e47402014-04-07 16:41:48 +0100417
Marek Vasut03304cc2020-05-17 17:28:31 +0200418 *bufp = lp->rx_buf[lp->cur_rx];
419 invalidate_dcache_range((unsigned long)*bufp,
420 (unsigned long)*bufp + pkt_len);
wdenkc6097192002-11-03 00:24:07 +0000421
Marek Vasut03304cc2020-05-17 17:28:31 +0200422 PCNET_DEBUG2("Rx%d: %d bytes from 0x%p\n",
423 lp->cur_rx, pkt_len, buf);
424
Wolfgang Denk39158312008-04-24 23:44:26 +0200425 return pkt_len;
wdenkc6097192002-11-03 00:24:07 +0000426}
427
Marek Vasut03304cc2020-05-17 17:28:31 +0200428static void pcnet_free_pkt_common(struct pcnet_priv *lp, unsigned int len)
wdenkc6097192002-11-03 00:24:07 +0000429{
Marek Vasut03304cc2020-05-17 17:28:31 +0200430 struct pcnet_rx_head *entry;
431
432 entry = &lp->uc->rx_ring[lp->cur_rx];
433
434 lp->status |= 0x8000;
435 writew(lp->status, &entry->status);
436
437 if (++lp->cur_rx >= RX_RING_SIZE)
438 lp->cur_rx = 0;
439}
440
441static void pcnet_halt_common(struct pcnet_priv *lp)
442{
Wolfgang Denk39158312008-04-24 23:44:26 +0200443 int i;
wdenkc6097192002-11-03 00:24:07 +0000444
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200445 PCNET_DEBUG1("%s: %s...\n", lp->name, __func__);
wdenkc6097192002-11-03 00:24:07 +0000446
Wolfgang Denk39158312008-04-24 23:44:26 +0200447 /* Reset the PCnet controller */
Marek Vasutf7377ad2020-05-17 17:00:42 +0200448 pcnet_reset(lp);
wdenkc6097192002-11-03 00:24:07 +0000449
Wolfgang Denk39158312008-04-24 23:44:26 +0200450 /* Wait for Stop bit */
451 for (i = 1000; i > 0; i--) {
Marek Vasutf7377ad2020-05-17 17:00:42 +0200452 if (pcnet_read_csr(lp, 0) & 0x4)
Wolfgang Denk39158312008-04-24 23:44:26 +0200453 break;
Paul Burton70ab8c02013-11-08 11:18:43 +0000454 udelay(10);
Wolfgang Denk39158312008-04-24 23:44:26 +0200455 }
Paul Burton70ab8c02013-11-08 11:18:43 +0000456 if (i <= 0)
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200457 printf("%s: TIMEOUT: controller reset failed\n", lp->name);
wdenkc6097192002-11-03 00:24:07 +0000458}
Marek Vasut2ba0a682020-05-17 16:31:41 +0200459
Marek Vasutcf8ec982020-05-17 17:43:22 +0200460#ifndef CONFIG_DM_ETH
Marek Vasut03304cc2020-05-17 17:28:31 +0200461static int pcnet_init(struct eth_device *dev, bd_t *bis)
462{
463 struct pcnet_priv *lp = dev->priv;
464
465 return pcnet_init_common(lp);
466}
467
468static int pcnet_send(struct eth_device *dev, void *packet, int pkt_len)
469{
470 struct pcnet_priv *lp = dev->priv;
471
472 return pcnet_send_common(lp, packet, pkt_len);
473}
474
475static int pcnet_recv(struct eth_device *dev)
476{
477 struct pcnet_priv *lp = dev->priv;
478 uchar *packet;
479 int ret;
480
481 ret = pcnet_recv_common(lp, &packet);
482 if (ret > 0)
483 net_process_received_packet(packet, ret);
484 if (ret)
485 pcnet_free_pkt_common(lp, ret);
486
487 return ret;
488}
489
490static void pcnet_halt(struct eth_device *dev)
491{
492 struct pcnet_priv *lp = dev->priv;
493
494 pcnet_halt_common(lp);
495}
496
Marek Vasut2ba0a682020-05-17 16:31:41 +0200497int pcnet_initialize(bd_t *bis)
498{
499 pci_dev_t devbusfn;
500 struct eth_device *dev;
Marek Vasutf3877712020-05-17 16:47:07 +0200501 struct pcnet_priv *lp;
Marek Vasut2ba0a682020-05-17 16:31:41 +0200502 u16 command, status;
503 int dev_nr = 0;
504 u32 bar;
505
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200506 PCNET_DEBUG1("\n%s...\n", __func__);
Marek Vasut2ba0a682020-05-17 16:31:41 +0200507
508 for (dev_nr = 0; ; dev_nr++) {
509 /*
510 * Find the PCnet PCI device(s).
511 */
512 devbusfn = pci_find_devices(supported, dev_nr);
513 if (devbusfn < 0)
514 break;
515
516 /*
517 * Allocate and pre-fill the device structure.
518 */
519 dev = calloc(1, sizeof(*dev));
520 if (!dev) {
521 printf("pcnet: Can not allocate memory\n");
522 break;
523 }
524
525 /*
526 * We only maintain one structure because the drivers will
527 * never be used concurrently. In 32bit mode the RX and TX
528 * ring entries must be aligned on 16-byte boundaries.
529 */
Marek Vasutf3877712020-05-17 16:47:07 +0200530 lp = malloc_cache_aligned(sizeof(*lp));
531 lp->uc = map_physmem((phys_addr_t)&lp->ucp,
532 sizeof(lp->ucp), MAP_NOCACHE);
Marek Vasut2b1c26a2020-05-17 16:31:04 +0200533 lp->dev = devbusfn;
Marek Vasutf3877712020-05-17 16:47:07 +0200534 flush_dcache_range((unsigned long)lp,
535 (unsigned long)lp + sizeof(*lp));
536 dev->priv = lp;
Marek Vasut2ba0a682020-05-17 16:31:41 +0200537 sprintf(dev->name, "pcnet#%d", dev_nr);
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200538 lp->name = dev->name;
539 lp->enetaddr = dev->enetaddr;
Marek Vasut2ba0a682020-05-17 16:31:41 +0200540
541 /*
542 * Setup the PCI device.
543 */
544 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &bar);
Marek Vasutf7377ad2020-05-17 17:00:42 +0200545 lp->iobase = (void *)(pci_mem_to_phys(devbusfn, bar) & ~0xf);
Marek Vasut2ba0a682020-05-17 16:31:41 +0200546
Marek Vasutf7377ad2020-05-17 17:00:42 +0200547 PCNET_DEBUG1("%s: devbusfn=0x%x iobase=0x%p: ",
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200548 lp->name, devbusfn, lp->iobase);
Marek Vasut2ba0a682020-05-17 16:31:41 +0200549
550 command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
551 pci_write_config_word(devbusfn, PCI_COMMAND, command);
552 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
553 if ((status & command) != command) {
554 printf("%s: Couldn't enable IO access or Bus Mastering\n",
Marek Vasut5cfe7be2020-05-17 17:04:19 +0200555 lp->name);
Marek Vasut2ba0a682020-05-17 16:31:41 +0200556 free(dev);
557 continue;
558 }
559
560 pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x40);
561
562 /*
563 * Probe the PCnet chip.
564 */
Marek Vasut03304cc2020-05-17 17:28:31 +0200565 if (pcnet_probe_common(lp) < 0) {
Marek Vasut2ba0a682020-05-17 16:31:41 +0200566 free(dev);
567 continue;
568 }
569
570 /*
571 * Setup device structure and register the driver.
572 */
573 dev->init = pcnet_init;
574 dev->halt = pcnet_halt;
575 dev->send = pcnet_send;
576 dev->recv = pcnet_recv;
577
578 eth_register(dev);
579 }
580
581 udelay(10 * 1000);
582
583 return dev_nr;
584}
Marek Vasutcf8ec982020-05-17 17:43:22 +0200585#else /* DM_ETH */
586static int pcnet_start(struct udevice *dev)
587{
588 struct eth_pdata *plat = dev_get_platdata(dev);
589 struct pcnet_priv *priv = dev_get_priv(dev);
590
591 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
592
593 return pcnet_init_common(priv);
594}
595
596static void pcnet_stop(struct udevice *dev)
597{
598 struct pcnet_priv *priv = dev_get_priv(dev);
599
600 pcnet_halt_common(priv);
601}
602
603static int pcnet_send(struct udevice *dev, void *packet, int length)
604{
605 struct pcnet_priv *priv = dev_get_priv(dev);
606 int ret;
607
608 ret = pcnet_send_common(priv, packet, length);
609
610 return ret ? 0 : -ETIMEDOUT;
611}
612
613static int pcnet_recv(struct udevice *dev, int flags, uchar **packetp)
614{
615 struct pcnet_priv *priv = dev_get_priv(dev);
616
617 return pcnet_recv_common(priv, packetp);
618}
619
620static int pcnet_free_pkt(struct udevice *dev, uchar *packet, int length)
621{
622 struct pcnet_priv *priv = dev_get_priv(dev);
623
624 pcnet_free_pkt_common(priv, length);
625
626 return 0;
627}
628
629static int pcnet_bind(struct udevice *dev)
630{
631 static int card_number;
632 char name[16];
633
634 sprintf(name, "pcnet#%u", card_number++);
635
636 return device_set_name(dev, name);
637}
638
639static int pcnet_probe(struct udevice *dev)
640{
641 struct eth_pdata *plat = dev_get_platdata(dev);
642 struct pcnet_priv *lp = dev_get_priv(dev);
643 u16 command, status;
644 u32 iobase;
645 int ret;
646
647 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_1, &iobase);
648 iobase &= ~0xf;
649
650 lp->uc = map_physmem((phys_addr_t)&lp->ucp,
651 sizeof(lp->ucp), MAP_NOCACHE);
652 lp->dev = dev;
653 lp->name = dev->name;
654 lp->enetaddr = plat->enetaddr;
655 lp->iobase = (void *)dm_pci_mem_to_phys(dev, iobase);
656
657 flush_dcache_range((unsigned long)lp,
658 (unsigned long)lp + sizeof(*lp));
659
660 command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
661 dm_pci_write_config16(dev, PCI_COMMAND, command);
662 dm_pci_read_config16(dev, PCI_COMMAND, &status);
663 if ((status & command) != command) {
664 printf("%s: Couldn't enable IO access or Bus Mastering\n",
665 lp->name);
666 return -EINVAL;
667 }
668
669 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x20);
670
671 ret = pcnet_probe_common(lp);
672 if (ret)
673 return ret;
674
675 return 0;
676}
677
678static const struct eth_ops pcnet_ops = {
679 .start = pcnet_start,
680 .send = pcnet_send,
681 .recv = pcnet_recv,
682 .stop = pcnet_stop,
683 .free_pkt = pcnet_free_pkt,
684};
685
686U_BOOT_DRIVER(eth_pcnet) = {
687 .name = "eth_pcnet",
688 .id = UCLASS_ETH,
689 .bind = pcnet_bind,
690 .probe = pcnet_probe,
691 .ops = &pcnet_ops,
692 .priv_auto_alloc_size = sizeof(struct pcnet_priv),
693 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
694 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
695};
696
697U_BOOT_PCI_DEVICE(eth_pcnet, supported);
698#endif