d008696b0ffb1918af21121067cfed53481d598d
[oweals/u-boot.git] / drivers / net / dc2114x.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <common.h>
4 #include <env.h>
5 #include <malloc.h>
6 #include <net.h>
7 #include <netdev.h>
8 #include <pci.h>
9
10 #define SROM_DLEVEL     0
11
12 #undef UPDATE_SROM
13
14 /* PCI Registers. */
15 #define PCI_CFDA_PSM    0x43
16
17 #define CFRV_RN         0x000000f0      /* Revision Number */
18
19 #define WAKEUP          0x00            /* Power Saving Wakeup */
20 #define SLEEP           0x80            /* Power Saving Sleep Mode */
21
22 #define DC2114x_BRK     0x0020  /* CFRV break between DC21142 & DC21143 */
23
24 /* Ethernet chip registers. */
25 #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
34 /* Register bits. */
35 #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
44 /* Descriptor bits. */
45 #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
61 #define SROM_HWADD      0x0014          /* Hardware Address offset in SROM */
62 #define SROM_RD         0x00004000      /* Read from Boot ROM */
63 #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. */
67 #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
75 #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
81 #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
89 struct de4x5_desc {
90         volatile s32 status;
91         u32 des1;
92         u32 buf;
93         u32 next;
94 };
95
96 /* RX and TX descriptor ring */
97 static struct de4x5_desc rx_ring[NUM_RX_DESC] __aligned(32);
98 static struct de4x5_desc tx_ring[NUM_TX_DESC] __aligned(32);
99 static int rx_new;      /* RX descriptor ring pointer */
100 static int tx_new;      /* TX descriptor ring pointer */
101
102 static char rx_ring_size;
103 static char tx_ring_size;
104
105 static u32 dc2114x_inl(struct eth_device *dev, u32 addr)
106 {
107         return le32_to_cpu(*(volatile u32 *)(addr + dev->iobase));
108 }
109
110 static void dc2114x_outl(struct eth_device *dev, u32 command, u32 addr)
111 {
112         *(volatile u32 *)(addr + dev->iobase) = cpu_to_le32(command);
113 }
114
115 static void reset_de4x5(struct eth_device *dev)
116 {
117         u32 i;
118
119         i = dc2114x_inl(dev, DE4X5_BMR);
120         mdelay(1);
121         dc2114x_outl(dev, i | BMR_SWR, DE4X5_BMR);
122         mdelay(1);
123         dc2114x_outl(dev, i, DE4X5_BMR);
124         mdelay(1);
125
126         for (i = 0; i < 5; i++) {
127                 dc2114x_inl(dev, DE4X5_BMR);
128                 mdelay(10);
129         }
130
131         mdelay(1);
132 }
133
134 static void start_de4x5(struct eth_device *dev)
135 {
136         u32 omr;
137
138         omr = dc2114x_inl(dev, DE4X5_OMR);
139         omr |= OMR_ST | OMR_SR;
140         dc2114x_outl(dev, omr, DE4X5_OMR);      /* Enable the TX and/or RX */
141 }
142
143 static void stop_de4x5(struct eth_device *dev)
144 {
145         u32 omr;
146
147         omr = dc2114x_inl(dev, DE4X5_OMR);
148         omr &= ~(OMR_ST | OMR_SR);
149         dc2114x_outl(dev, omr, DE4X5_OMR);      /* Disable the TX and/or RX */
150 }
151
152 /* SROM Read and write routines. */
153 static void sendto_srom(struct eth_device *dev, u_int command, u_long addr)
154 {
155         dc2114x_outl(dev, command, addr);
156         udelay(1);
157 }
158
159 static int getfrom_srom(struct eth_device *dev, u_long addr)
160 {
161         u32 tmp = dc2114x_inl(dev, addr);
162
163         udelay(1);
164         return tmp;
165 }
166
167 /* Note: this routine returns extra data bits for size detection. */
168 static 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;
174
175         sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
176         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
177
178         debug_cond(SROM_DLEVEL >= 1, " EEPROM read at %d ", location);
179
180         /* 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;
183
184                 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);
190                 debug_cond(SROM_DLEVEL >= 2, "%X",
191                            getfrom_srom(dev, ioaddr) & 15);
192                 retval = (retval << 1) |
193                          !!(getfrom_srom(dev, ioaddr) & EE_DATA_READ);
194         }
195
196         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
197
198         debug_cond(SROM_DLEVEL >= 2, " :%X:", getfrom_srom(dev, ioaddr) & 15);
199
200         for (i = 16; i > 0; i--) {
201                 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
202                 udelay(10);
203                 debug_cond(SROM_DLEVEL >= 2, "%X",
204                            getfrom_srom(dev, ioaddr) & 15);
205                 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         }
210
211         /* Terminate the EEPROM access. */
212         sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
213
214         debug_cond(SROM_DLEVEL >= 2, " EEPROM value at %d is %5.5x.\n",
215                    location, retval);
216
217         return retval;
218 }
219
220 /*
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  */
225 static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd,
226                          int cmd_len)
227 {
228         unsigned int retval = 0;
229
230         debug_cond(SROM_DLEVEL >= 1, " EEPROM op 0x%x: ", cmd);
231
232         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
233
234         /* Shift the command bits out. */
235         do {
236                 short dataval = (cmd & BIT(cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
237
238                 sendto_srom(dev, dataval, ioaddr);
239                 udelay(10);
240
241                 debug_cond(SROM_DLEVEL >= 2, "%X",
242                            getfrom_srom(dev, ioaddr) & 15);
243
244                 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);
249
250         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
251
252         /* Terminate the EEPROM access. */
253         sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
254
255         debug_cond(SROM_DLEVEL >= 1, " EEPROM result is 0x%5.5x.\n", retval);
256
257         return retval;
258 }
259
260 static int read_srom(struct eth_device *dev, u_long ioaddr, int index)
261 {
262         int ee_addr_size;
263
264         ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
265
266         return do_eeprom_cmd(dev, ioaddr, 0xffff |
267                              (((SROM_READ_CMD << ee_addr_size) | index) << 16),
268                              3 + ee_addr_size + 16);
269 }
270
271 #ifdef UPDATE_SROM
272 static int write_srom(struct eth_device *dev, u_long ioaddr, int index,
273                       int new_value)
274 {
275         unsigned short newval;
276         int ee_addr_size;
277         int i;
278
279         ee_addr_size = (do_read_eeprom(dev, ioaddr, 0xff, 8) & BIT(18)) ? 8 : 6;
280
281         udelay(10 * 1000); /* test-only */
282
283         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);
287
288         /* Enable programming modes. */
289         do_eeprom_cmd(dev, ioaddr, 0x4f << (ee_addr_size - 4),
290                       3 + ee_addr_size);
291
292         /* 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;
302         }
303
304         debug_cond(SROM_DLEVEL >= 1, " Write finished after %d ticks.\n", i);
305
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);
314
315         debug_cond(SROM_DLEVEL >= 1, "  New value at offset %d is %4.4x.\n",
316                    index, newval);
317
318         return 1;
319 }
320
321 static 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
357 static 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;
369         }
370
371         for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
372                 if (i < TOUT_LOOP)
373                         continue;
374
375                 printf("%s: tx error buffer not ready\n", dev->name);
376                 return;
377         }
378
379         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);
382
383         dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
384
385         for (i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
386                 if (i < TOUT_LOOP)
387                         continue;
388
389                 printf("%s: tx buffer not ready\n", dev->name);
390                 return;
391         }
392
393         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;
399 }
400
401 static int dc21x4x_send(struct eth_device *dev, void *packet, int length)
402 {
403         int status = -1;
404         int i;
405
406         if (length <= 0) {
407                 printf("%s: bad packet size: %d\n", dev->name, length);
408                 goto done;
409         }
410
411         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;
417         }
418
419         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);
421         tx_ring[tx_new].status = cpu_to_le32(T_OWN);
422
423         dc2114x_outl(dev, POLL_DEMAND, DE4X5_TPD);
424
425         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;
431         }
432
433         if (le32_to_cpu(tx_ring[tx_new].status) & TD_ES) {
434                 tx_ring[tx_new].status = 0x0;
435                 goto done;
436         }
437
438         status = length;
439
440 done:
441         tx_new = (tx_new + 1) % NUM_TX_DESC;
442         return status;
443 }
444
445 static int dc21x4x_recv(struct eth_device *dev)
446 {
447         int length = 0;
448         u32 status;
449
450         while (true) {
451                 status = le32_to_cpu(rx_ring[rx_new].status);
452
453                 if (status & R_OWN)
454                         break;
455
456                 if (status & RD_LS) {
457                         /* Valid frame status. */
458                         if (status & RD_ES) {
459                                 /* There was an error. */
460                                 printf("RX error status = 0x%08X\n", status);
461                         } else {
462                                 /* A valid frame received. */
463                                 length = (le32_to_cpu(rx_ring[rx_new].status)
464                                           >> 16);
465
466                                 /* Pass the packet up to the protocol layers */
467                                 net_process_received_packet
468                                         (net_rx_packets[rx_new], length - 4);
469                         }
470
471                         /*
472                          * Change buffer ownership for this frame,
473                          * back to the adapter.
474                          */
475                         rx_ring[rx_new].status = cpu_to_le32(R_OWN);
476                 }
477
478                 /* Update entry information. */
479                 rx_new = (rx_new + 1) % rx_ring_size;
480         }
481
482         return length;
483 }
484
485 static int dc21x4x_init(struct eth_device *dev, bd_t *bis)
486 {
487         int i;
488         int devbusfn = (int)dev->priv;
489
490         /* Ensure we're not sleeping. */
491         pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
492
493         reset_de4x5(dev);
494
495         if (dc2114x_inl(dev, DE4X5_STS) & (STS_TS | STS_RS)) {
496                 printf("Error: Cannot reset ethernet controller.\n");
497                 return -1;
498         }
499
500         dc2114x_outl(dev, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
501
502         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;
508         }
509
510         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;
515         }
516
517         rx_ring_size = NUM_RX_DESC;
518         tx_ring_size = NUM_TX_DESC;
519
520         /* 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);
523
524         /* 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);
527
528         start_de4x5(dev);
529
530         tx_new = 0;
531         rx_new = 0;
532
533         send_setup_frame(dev, bis);
534
535         return 0;
536 }
537
538 static void dc21x4x_halt(struct eth_device *dev)
539 {
540         int devbusfn = (int)dev->priv;
541
542         stop_de4x5(dev);
543         dc2114x_outl(dev, 0, DE4X5_SICR);
544
545         pci_write_config_byte(devbusfn, PCI_CFDA_PSM, SLEEP);
546 }
547
548 static void read_hw_addr(struct eth_device *dev, bd_t *bis)
549 {
550         u_short tmp, *p = (u_short *)(&dev->enetaddr[0]);
551         int i, j = 0;
552
553         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++;
557         }
558
559         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);
564 #endif
565         }
566 }
567
568 static 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 };
573
574 int dc21x4x_initialize(bd_t *bis)
575 {
576         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;
584
585         while (1) {
586                 devbusfn = pci_find_devices(supported, idx++);
587                 if (devbusfn == -1)
588                         break;
589
590                 /* Get the chip configuration revision register. */
591                 pci_read_config_dword(devbusfn, PCI_REVISION_ID, &cfrv);
592
593                 if ((cfrv & CFRV_RN) < DC2114x_BRK) {
594                         printf("Error: The chip is not DC21143.\n");
595                         continue;
596                 }
597
598                 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);
601
602                 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                 }
607
608                 if (!(status & PCI_COMMAND_MASTER)) {
609                         printf("Error: Can not enable Bus Mastering.\n");
610                         continue;
611                 }
612
613                 /* Check the latency timer for values >= 0x60. */
614                 pci_read_config_byte(devbusfn, PCI_LATENCY_TIMER, &timer);
615
616                 if (timer < 0x60) {
617                         pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER,
618                                               0x60);
619                 }
620
621                 /* 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);
625
626                 dev = (struct eth_device *)malloc(sizeof(*dev));
627                 if (!dev) {
628                         printf("Can not allocalte memory of dc21x4x\n");
629                         break;
630                 }
631
632                 memset(dev, 0, sizeof(*dev));
633
634                 sprintf(dev->name, "dc21x4x#%d", card_number);
635
636                 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;
642
643                 /* Ensure we're not sleeping. */
644                 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
645
646                 udelay(10 * 1000);
647
648                 read_hw_addr(dev, bis);
649
650                 eth_register(dev);
651
652                 card_number++;
653         }
654
655         return card_number;
656 }