net: pcnet: Replace memset+malloc with calloc
[platform/kernel/u-boot.git] / drivers / net / pcnet.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
7  */
8
9 #include <common.h>
10 #include <cpu_func.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <memalign.h>
14 #include <net.h>
15 #include <netdev.h>
16 #include <asm/cache.h>
17 #include <asm/io.h>
18 #include <pci.h>
19 #include <linux/delay.h>
20
21 #define PCNET_DEBUG_LEVEL       0       /* 0=off, 1=init, 2=rx/tx */
22
23 #define PCNET_DEBUG1(fmt,args...)       \
24         debug_cond(PCNET_DEBUG_LEVEL > 0, fmt ,##args)
25 #define PCNET_DEBUG2(fmt,args...)       \
26         debug_cond(PCNET_DEBUG_LEVEL > 1, fmt ,##args)
27
28 /*
29  * Set the number of Tx and Rx buffers, using Log_2(# buffers).
30  * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
31  * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
32  */
33 #define PCNET_LOG_TX_BUFFERS    0
34 #define PCNET_LOG_RX_BUFFERS    2
35
36 #define TX_RING_SIZE            (1 << (PCNET_LOG_TX_BUFFERS))
37 #define TX_RING_LEN_BITS        ((PCNET_LOG_TX_BUFFERS) << 12)
38
39 #define RX_RING_SIZE            (1 << (PCNET_LOG_RX_BUFFERS))
40 #define RX_RING_LEN_BITS        ((PCNET_LOG_RX_BUFFERS) << 4)
41
42 #define PKT_BUF_SZ              1544
43
44 /* The PCNET Rx and Tx ring descriptors. */
45 struct pcnet_rx_head {
46         u32 base;
47         s16 buf_length;
48         s16 status;
49         u32 msg_length;
50         u32 reserved;
51 };
52
53 struct pcnet_tx_head {
54         u32 base;
55         s16 length;
56         s16 status;
57         u32 misc;
58         u32 reserved;
59 };
60
61 /* The PCNET 32-Bit initialization block, described in databook. */
62 struct pcnet_init_block {
63         u16 mode;
64         u16 tlen_rlen;
65         u8 phys_addr[6];
66         u16 reserved;
67         u32 filter[2];
68         /* Receive and transmit ring base, along with extra bits. */
69         u32 rx_ring;
70         u32 tx_ring;
71         u32 reserved2;
72 };
73
74 struct pcnet_uncached_priv {
75         struct pcnet_rx_head rx_ring[RX_RING_SIZE];
76         struct pcnet_tx_head tx_ring[TX_RING_SIZE];
77         struct pcnet_init_block init_block;
78 } __aligned(ARCH_DMA_MINALIGN);
79
80 struct pcnet_priv {
81         struct pcnet_uncached_priv ucp;
82         /* Receive Buffer space */
83         unsigned char rx_buf[RX_RING_SIZE][PKT_BUF_SZ + 4];
84         struct pcnet_uncached_priv *uc;
85         int cur_rx;
86         int cur_tx;
87 };
88
89 static struct pcnet_priv *lp;
90
91 /* Offsets from base I/O address for WIO mode */
92 #define PCNET_RDP               0x10
93 #define PCNET_RAP               0x12
94 #define PCNET_RESET             0x14
95 #define PCNET_BDP               0x16
96
97 static u16 pcnet_read_csr(struct eth_device *dev, int index)
98 {
99         void __iomem *base = (void __iomem *)dev->iobase;
100
101         writew(index, base + PCNET_RAP);
102         return readw(base + PCNET_RDP);
103 }
104
105 static void pcnet_write_csr(struct eth_device *dev, int index, u16 val)
106 {
107         void __iomem *base = (void __iomem *)dev->iobase;
108
109         writew(index, base + PCNET_RAP);
110         writew(val, base + PCNET_RDP);
111 }
112
113 static u16 pcnet_read_bcr(struct eth_device *dev, int index)
114 {
115         void __iomem *base = (void __iomem *)dev->iobase;
116
117         writew(index, base + PCNET_RAP);
118         return readw(base + PCNET_BDP);
119 }
120
121 static void pcnet_write_bcr(struct eth_device *dev, int index, u16 val)
122 {
123         void __iomem *base = (void __iomem *)dev->iobase;
124
125         writew(index, base + PCNET_RAP);
126         writew(val, base + PCNET_BDP);
127 }
128
129 static void pcnet_reset(struct eth_device *dev)
130 {
131         void __iomem *base = (void __iomem *)dev->iobase;
132
133         readw(base + PCNET_RESET);
134 }
135
136 static int pcnet_check(struct eth_device *dev)
137 {
138         void __iomem *base = (void __iomem *)dev->iobase;
139
140         writew(88, base + PCNET_RAP);
141         return readw(base + PCNET_RAP) == 88;
142 }
143
144 static int pcnet_init (struct eth_device *dev, bd_t * bis);
145 static int pcnet_send(struct eth_device *dev, void *packet, int length);
146 static int pcnet_recv (struct eth_device *dev);
147 static void pcnet_halt (struct eth_device *dev);
148 static int pcnet_probe (struct eth_device *dev, bd_t * bis, int dev_num);
149
150 static inline pci_addr_t pcnet_virt_to_mem(const struct eth_device *dev,
151                                                 void *addr)
152 {
153         pci_dev_t devbusfn = (pci_dev_t)(unsigned long)dev->priv;
154         void *virt_addr = addr;
155
156         return pci_virt_to_mem(devbusfn, virt_addr);
157 }
158
159 static struct pci_device_id supported[] = {
160         { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE) },
161         {}
162 };
163
164
165 int pcnet_initialize(bd_t *bis)
166 {
167         pci_dev_t devbusfn;
168         struct eth_device *dev;
169         u16 command, status;
170         int dev_nr = 0;
171         u32 bar;
172
173         PCNET_DEBUG1("\npcnet_initialize...\n");
174
175         for (dev_nr = 0;; dev_nr++) {
176
177                 /*
178                  * Find the PCnet PCI device(s).
179                  */
180                 devbusfn = pci_find_devices(supported, dev_nr);
181                 if (devbusfn < 0)
182                         break;
183
184                 /*
185                  * Allocate and pre-fill the device structure.
186                  */
187                 dev = calloc(1, sizeof(*dev));
188                 if (!dev) {
189                         printf("pcnet: Can not allocate memory\n");
190                         break;
191                 }
192                 dev->priv = (void *)(unsigned long)devbusfn;
193                 sprintf(dev->name, "pcnet#%d", dev_nr);
194
195                 /*
196                  * Setup the PCI device.
197                  */
198                 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &bar);
199                 dev->iobase = pci_mem_to_phys(devbusfn, bar);
200                 dev->iobase &= ~0xf;
201
202                 PCNET_DEBUG1("%s: devbusfn=0x%x iobase=0x%lx: ",
203                              dev->name, devbusfn, (unsigned long)dev->iobase);
204
205                 command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
206                 pci_write_config_word(devbusfn, PCI_COMMAND, command);
207                 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
208                 if ((status & command) != command) {
209                         printf("%s: Couldn't enable IO access or Bus Mastering\n",
210                                dev->name);
211                         free(dev);
212                         continue;
213                 }
214
215                 pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x40);
216
217                 /*
218                  * Probe the PCnet chip.
219                  */
220                 if (pcnet_probe(dev, bis, dev_nr) < 0) {
221                         free(dev);
222                         continue;
223                 }
224
225                 /*
226                  * Setup device structure and register the driver.
227                  */
228                 dev->init = pcnet_init;
229                 dev->halt = pcnet_halt;
230                 dev->send = pcnet_send;
231                 dev->recv = pcnet_recv;
232
233                 eth_register(dev);
234         }
235
236         udelay(10 * 1000);
237
238         return dev_nr;
239 }
240
241 static int pcnet_probe(struct eth_device *dev, bd_t *bis, int dev_nr)
242 {
243         int chip_version;
244         char *chipname;
245         int i;
246
247         /* Reset the PCnet controller */
248         pcnet_reset(dev);
249
250         /* Check if register access is working */
251         if (pcnet_read_csr(dev, 0) != 4 || !pcnet_check(dev)) {
252                 printf("%s: CSR register access check failed\n", dev->name);
253                 return -1;
254         }
255
256         /* Identify the chip */
257         chip_version =
258                 pcnet_read_csr(dev, 88) | (pcnet_read_csr(dev, 89) << 16);
259         if ((chip_version & 0xfff) != 0x003)
260                 return -1;
261         chip_version = (chip_version >> 12) & 0xffff;
262         switch (chip_version) {
263         case 0x2621:
264                 chipname = "PCnet/PCI II 79C970A";      /* PCI */
265                 break;
266         case 0x2625:
267                 chipname = "PCnet/FAST III 79C973";     /* PCI */
268                 break;
269         case 0x2627:
270                 chipname = "PCnet/FAST III 79C975";     /* PCI */
271                 break;
272         default:
273                 printf("%s: PCnet version %#x not supported\n",
274                        dev->name, chip_version);
275                 return -1;
276         }
277
278         PCNET_DEBUG1("AMD %s\n", chipname);
279
280         /*
281          * In most chips, after a chip reset, the ethernet address is read from
282          * the station address PROM at the base address and programmed into the
283          * "Physical Address Registers" CSR12-14.
284          */
285         for (i = 0; i < 3; i++) {
286                 unsigned int val;
287
288                 val = pcnet_read_csr(dev, i + 12) & 0x0ffff;
289                 /* There may be endianness issues here. */
290                 dev->enetaddr[2 * i] = val & 0x0ff;
291                 dev->enetaddr[2 * i + 1] = (val >> 8) & 0x0ff;
292         }
293
294         return 0;
295 }
296
297 static int pcnet_init(struct eth_device *dev, bd_t *bis)
298 {
299         struct pcnet_uncached_priv *uc;
300         int i, val;
301         unsigned long addr;
302
303         PCNET_DEBUG1("%s: pcnet_init...\n", dev->name);
304
305         /* Switch pcnet to 32bit mode */
306         pcnet_write_bcr(dev, 20, 2);
307
308         /* Set/reset autoselect bit */
309         val = pcnet_read_bcr(dev, 2) & ~2;
310         val |= 2;
311         pcnet_write_bcr(dev, 2, val);
312
313         /* Enable auto negotiate, setup, disable fd */
314         val = pcnet_read_bcr(dev, 32) & ~0x98;
315         val |= 0x20;
316         pcnet_write_bcr(dev, 32, val);
317
318         /*
319          * Enable NOUFLO on supported controllers, with the transmit
320          * start point set to the full packet. This will cause entire
321          * packets to be buffered by the ethernet controller before
322          * transmission, eliminating underflows which are common on
323          * slower devices. Controllers which do not support NOUFLO will
324          * simply be left with a larger transmit FIFO threshold.
325          */
326         val = pcnet_read_bcr(dev, 18);
327         val |= 1 << 11;
328         pcnet_write_bcr(dev, 18, val);
329         val = pcnet_read_csr(dev, 80);
330         val |= 0x3 << 10;
331         pcnet_write_csr(dev, 80, val);
332
333         /*
334          * We only maintain one structure because the drivers will never
335          * be used concurrently. In 32bit mode the RX and TX ring entries
336          * must be aligned on 16-byte boundaries.
337          */
338         if (lp == NULL) {
339                 lp = malloc_cache_aligned(sizeof(*lp));
340                 lp->uc = map_physmem((phys_addr_t)&lp->ucp,
341                                      sizeof(lp->ucp), MAP_NOCACHE);
342                 flush_dcache_range((unsigned long)lp,
343                                    (unsigned long)lp + sizeof(*lp));
344         }
345
346         uc = lp->uc;
347
348         uc->init_block.mode = cpu_to_le16(0x0000);
349         uc->init_block.filter[0] = 0x00000000;
350         uc->init_block.filter[1] = 0x00000000;
351
352         /*
353          * Initialize the Rx ring.
354          */
355         lp->cur_rx = 0;
356         for (i = 0; i < RX_RING_SIZE; i++) {
357                 addr = pcnet_virt_to_mem(dev, lp->rx_buf[i]);
358                 uc->rx_ring[i].base = cpu_to_le32(addr);
359                 uc->rx_ring[i].buf_length = cpu_to_le16(-PKT_BUF_SZ);
360                 uc->rx_ring[i].status = cpu_to_le16(0x8000);
361                 PCNET_DEBUG1
362                         ("Rx%d: base=0x%x buf_length=0x%hx status=0x%hx\n", i,
363                          uc->rx_ring[i].base, uc->rx_ring[i].buf_length,
364                          uc->rx_ring[i].status);
365         }
366
367         /*
368          * Initialize the Tx ring. The Tx buffer address is filled in as
369          * needed, but we do need to clear the upper ownership bit.
370          */
371         lp->cur_tx = 0;
372         for (i = 0; i < TX_RING_SIZE; i++) {
373                 uc->tx_ring[i].base = 0;
374                 uc->tx_ring[i].status = 0;
375         }
376
377         /*
378          * Setup Init Block.
379          */
380         PCNET_DEBUG1("Init block at 0x%p: MAC", &lp->uc->init_block);
381
382         for (i = 0; i < 6; i++) {
383                 lp->uc->init_block.phys_addr[i] = dev->enetaddr[i];
384                 PCNET_DEBUG1(" %02x", lp->uc->init_block.phys_addr[i]);
385         }
386
387         uc->init_block.tlen_rlen = cpu_to_le16(TX_RING_LEN_BITS |
388                                                RX_RING_LEN_BITS);
389         addr = pcnet_virt_to_mem(dev, uc->rx_ring);
390         uc->init_block.rx_ring = cpu_to_le32(addr);
391         addr = pcnet_virt_to_mem(dev, uc->tx_ring);
392         uc->init_block.tx_ring = cpu_to_le32(addr);
393
394         PCNET_DEBUG1("\ntlen_rlen=0x%x rx_ring=0x%x tx_ring=0x%x\n",
395                      uc->init_block.tlen_rlen,
396                      uc->init_block.rx_ring, uc->init_block.tx_ring);
397
398         /*
399          * Tell the controller where the Init Block is located.
400          */
401         barrier();
402         addr = pcnet_virt_to_mem(dev, &lp->uc->init_block);
403         pcnet_write_csr(dev, 1, addr & 0xffff);
404         pcnet_write_csr(dev, 2, (addr >> 16) & 0xffff);
405
406         pcnet_write_csr(dev, 4, 0x0915);
407         pcnet_write_csr(dev, 0, 0x0001);        /* start */
408
409         /* Wait for Init Done bit */
410         for (i = 10000; i > 0; i--) {
411                 if (pcnet_read_csr(dev, 0) & 0x0100)
412                         break;
413                 udelay(10);
414         }
415         if (i <= 0) {
416                 printf("%s: TIMEOUT: controller init failed\n", dev->name);
417                 pcnet_reset(dev);
418                 return -1;
419         }
420
421         /*
422          * Finally start network controller operation.
423          */
424         pcnet_write_csr(dev, 0, 0x0002);
425
426         return 0;
427 }
428
429 static int pcnet_send(struct eth_device *dev, void *packet, int pkt_len)
430 {
431         int i, status;
432         u32 addr;
433         struct pcnet_tx_head *entry = &lp->uc->tx_ring[lp->cur_tx];
434
435         PCNET_DEBUG2("Tx%d: %d bytes from 0x%p ", lp->cur_tx, pkt_len,
436                      packet);
437
438         flush_dcache_range((unsigned long)packet,
439                            (unsigned long)packet + pkt_len);
440
441         /* Wait for completion by testing the OWN bit */
442         for (i = 1000; i > 0; i--) {
443                 status = readw(&entry->status);
444                 if ((status & 0x8000) == 0)
445                         break;
446                 udelay(100);
447                 PCNET_DEBUG2(".");
448         }
449         if (i <= 0) {
450                 printf("%s: TIMEOUT: Tx%d failed (status = 0x%x)\n",
451                        dev->name, lp->cur_tx, status);
452                 pkt_len = 0;
453                 goto failure;
454         }
455
456         /*
457          * Setup Tx ring. Caution: the write order is important here,
458          * set the status with the "ownership" bits last.
459          */
460         addr = pcnet_virt_to_mem(dev, packet);
461         writew(-pkt_len, &entry->length);
462         writel(0, &entry->misc);
463         writel(addr, &entry->base);
464         writew(0x8300, &entry->status);
465
466         /* Trigger an immediate send poll. */
467         pcnet_write_csr(dev, 0, 0x0008);
468
469       failure:
470         if (++lp->cur_tx >= TX_RING_SIZE)
471                 lp->cur_tx = 0;
472
473         PCNET_DEBUG2("done\n");
474         return pkt_len;
475 }
476
477 static int pcnet_recv (struct eth_device *dev)
478 {
479         struct pcnet_rx_head *entry;
480         unsigned char *buf;
481         int pkt_len = 0;
482         u16 status, err_status;
483
484         while (1) {
485                 entry = &lp->uc->rx_ring[lp->cur_rx];
486                 /*
487                  * If we own the next entry, it's a new packet. Send it up.
488                  */
489                 status = readw(&entry->status);
490                 if ((status & 0x8000) != 0)
491                         break;
492                 err_status = status >> 8;
493
494                 if (err_status != 0x03) {       /* There was an error. */
495                         printf("%s: Rx%d", dev->name, lp->cur_rx);
496                         PCNET_DEBUG1(" (status=0x%x)", err_status);
497                         if (err_status & 0x20)
498                                 printf(" Frame");
499                         if (err_status & 0x10)
500                                 printf(" Overflow");
501                         if (err_status & 0x08)
502                                 printf(" CRC");
503                         if (err_status & 0x04)
504                                 printf(" Fifo");
505                         printf(" Error\n");
506                         status &= 0x03ff;
507
508                 } else {
509                         pkt_len = (readl(&entry->msg_length) & 0xfff) - 4;
510                         if (pkt_len < 60) {
511                                 printf("%s: Rx%d: invalid packet length %d\n",
512                                        dev->name, lp->cur_rx, pkt_len);
513                         } else {
514                                 buf = lp->rx_buf[lp->cur_rx];
515                                 invalidate_dcache_range((unsigned long)buf,
516                                         (unsigned long)buf + pkt_len);
517                                 net_process_received_packet(buf, pkt_len);
518                                 PCNET_DEBUG2("Rx%d: %d bytes from 0x%p\n",
519                                              lp->cur_rx, pkt_len, buf);
520                         }
521                 }
522
523                 status |= 0x8000;
524                 writew(status, &entry->status);
525
526                 if (++lp->cur_rx >= RX_RING_SIZE)
527                         lp->cur_rx = 0;
528         }
529         return pkt_len;
530 }
531
532 static void pcnet_halt(struct eth_device *dev)
533 {
534         int i;
535
536         PCNET_DEBUG1("%s: pcnet_halt...\n", dev->name);
537
538         /* Reset the PCnet controller */
539         pcnet_reset(dev);
540
541         /* Wait for Stop bit */
542         for (i = 1000; i > 0; i--) {
543                 if (pcnet_read_csr(dev, 0) & 0x4)
544                         break;
545                 udelay(10);
546         }
547         if (i <= 0)
548                 printf("%s: TIMEOUT: controller reset failed\n", dev->name);
549 }