* Patch by André Schwarz, 8 Dec 2003:
[oweals/u-boot.git] / drivers / dc2114x.c
1 /*
2  * See file CREDITS for list of people who contributed to this
3  * project.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18  * MA 02111-1307 USA
19  */
20
21 #include <common.h>
22
23 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) \
24         && defined(CONFIG_TULIP)
25
26 #include <malloc.h>
27 #include <net.h>
28 #include <pci.h>
29
30 #undef DEBUG
31 #undef DEBUG_SROM
32 #undef DEBUG_SROM2
33
34 #undef UPDATE_SROM
35
36 /* PCI Registers.
37  */
38 #define PCI_CFDA_PSM            0x43
39
40 #define CFRV_RN         0x000000f0      /* Revision Number */
41
42 #define WAKEUP          0x00            /* Power Saving Wakeup */
43 #define SLEEP           0x80            /* Power Saving Sleep Mode */
44
45 #define DC2114x_BRK     0x0020          /* CFRV break between DC21142 & DC21143 */
46
47 /* Ethernet chip registers.
48  */
49 #define DE4X5_BMR       0x000           /* Bus Mode Register */
50 #define DE4X5_TPD       0x008           /* Transmit Poll Demand Reg */
51 #define DE4X5_RRBA      0x018           /* RX Ring Base Address Reg */
52 #define DE4X5_TRBA      0x020           /* TX Ring Base Address Reg */
53 #define DE4X5_STS       0x028           /* Status Register */
54 #define DE4X5_OMR       0x030           /* Operation Mode Register */
55 #define DE4X5_SICR      0x068           /* SIA Connectivity Register */
56 #define DE4X5_APROM     0x048           /* Ethernet Address PROM */
57
58 /* Register bits.
59  */
60 #define BMR_SWR         0x00000001      /* Software Reset */
61 #define STS_TS          0x00700000      /* Transmit Process State */
62 #define STS_RS          0x000e0000      /* Receive Process State */
63 #define OMR_ST          0x00002000      /* Start/Stop Transmission Command */
64 #define OMR_SR          0x00000002      /* Start/Stop Receive */
65 #define OMR_PS          0x00040000      /* Port Select */
66 #define OMR_SDP         0x02000000      /* SD Polarity - MUST BE ASSERTED */
67 #define OMR_PM          0x00000080      /* Pass All Multicast */
68
69 /* Descriptor bits.
70  */
71 #define R_OWN           0x80000000      /* Own Bit */
72 #define RD_RER          0x02000000      /* Receive End Of Ring */
73 #define RD_LS           0x00000100      /* Last Descriptor */
74 #define RD_ES           0x00008000      /* Error Summary */
75 #define TD_TER          0x02000000      /* Transmit End Of Ring */
76 #define T_OWN           0x80000000      /* Own Bit */
77 #define TD_LS           0x40000000      /* Last Segment */
78 #define TD_FS           0x20000000      /* First Segment */
79 #define TD_ES           0x00008000      /* Error Summary */
80 #define TD_SET          0x08000000      /* Setup Packet */
81
82 /* The EEPROM commands include the alway-set leading bit. */
83 #define SROM_WRITE_CMD  5
84 #define SROM_READ_CMD   6
85 #define SROM_ERASE_CMD  7
86
87 #define SROM_HWADD          0x0014      /* Hardware Address offset in SROM */
88 #define SROM_RD         0x00004000      /* Read from Boot ROM */
89 #define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
90 #define EE_WRITE_0              0x4801
91 #define EE_WRITE_1              0x4805
92 #define EE_DATA_READ    0x08    /* EEPROM chip data out. */
93 #define SROM_SR         0x00000800      /* Select Serial ROM when set */
94
95 #define DT_IN           0x00000004      /* Serial Data In */
96 #define DT_CLK          0x00000002      /* Serial ROM Clock */
97 #define DT_CS           0x00000001      /* Serial ROM Chip Select */
98
99 #define POLL_DEMAND     1
100
101 #ifdef CONFIG_TULIP_FIX_DAVICOM
102 #define RESET_DM9102(dev) {\
103     unsigned long i;\
104     i=INL(dev, 0x0);\
105     udelay(1000);\
106     OUTL(dev, i | BMR_SWR, DE4X5_BMR);\
107     udelay(1000);\
108 }
109 #else
110 #define RESET_DE4X5(dev) {\
111     int i;\
112     i=INL(dev, DE4X5_BMR);\
113     udelay(1000);\
114     OUTL(dev, i | BMR_SWR, DE4X5_BMR);\
115     udelay(1000);\
116     OUTL(dev, i, DE4X5_BMR);\
117     udelay(1000);\
118     for (i=0;i<5;i++) {INL(dev, DE4X5_BMR); udelay(10000);}\
119     udelay(1000);\
120 }
121 #endif
122
123 #define START_DE4X5(dev) {\
124     s32 omr; \
125     omr = INL(dev, DE4X5_OMR);\
126     omr |= OMR_ST | OMR_SR;\
127     OUTL(dev, omr, DE4X5_OMR);          /* Enable the TX and/or RX */\
128 }
129
130 #define STOP_DE4X5(dev) {\
131     s32 omr; \
132     omr = INL(dev, DE4X5_OMR);\
133     omr &= ~(OMR_ST|OMR_SR);\
134     OUTL(dev, omr, DE4X5_OMR);          /* Disable the TX and/or RX */ \
135 }
136
137 #define NUM_RX_DESC PKTBUFSRX
138 #ifndef CONFIG_TULIP_FIX_DAVICOM
139         #define NUM_TX_DESC 1                   /* Number of TX descriptors   */
140 #else
141         #define NUM_TX_DESC 4
142 #endif
143 #define RX_BUFF_SZ  PKTSIZE_ALIGN
144
145 #define TOUT_LOOP   1000000
146
147 #define SETUP_FRAME_LEN 192
148 #define ETH_ALEN        6
149
150 struct de4x5_desc {
151         volatile s32 status;
152         u32 des1;
153         u32 buf;
154         u32 next;
155 };
156
157 static struct de4x5_desc rx_ring[NUM_RX_DESC] __attribute__ ((aligned(32))); /* RX descriptor ring         */
158 static struct de4x5_desc tx_ring[NUM_TX_DESC] __attribute__ ((aligned(32))); /* TX descriptor ring         */
159 static int rx_new;                             /* RX descriptor ring pointer */
160 static int tx_new;                             /* TX descriptor ring pointer */
161
162 static char rxRingSize;
163 static char txRingSize;
164
165 static void  sendto_srom(struct eth_device* dev, u_int command, u_long addr);
166 static int   getfrom_srom(struct eth_device* dev, u_long addr);
167 static int   do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd, int cmd_len);
168 static int   do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location, int addr_len);
169 static int   read_srom(struct eth_device *dev, u_long ioaddr, int index);
170 #ifdef UPDATE_SROM
171 static int   write_srom(struct eth_device *dev, u_long ioaddr, int index, int new_value);
172 static void  update_srom(struct eth_device *dev, bd_t *bis);
173 #endif
174 static void  read_hw_addr(struct eth_device* dev, bd_t * bis);
175 static void  send_setup_frame(struct eth_device* dev, bd_t * bis);
176
177 static int   dc21x4x_init(struct eth_device* dev, bd_t* bis);
178 static int   dc21x4x_send(struct eth_device* dev, volatile void *packet, int length);
179 static int   dc21x4x_recv(struct eth_device* dev);
180 static void  dc21x4x_halt(struct eth_device* dev);
181 #ifdef CONFIG_TULIP_SELECT_MEDIA
182 extern void  dc21x4x_select_media(struct eth_device* dev);
183 #endif
184
185 #if defined(CONFIG_E500)
186 #define phys_to_bus(a) (a)
187 #else
188 #define phys_to_bus(a)  pci_phys_to_mem((pci_dev_t)dev->priv, a)
189 #endif
190
191 static int INL(struct eth_device* dev, u_long addr)
192 {
193         return le32_to_cpu(*(volatile u_long *)(addr + dev->iobase));
194 }
195
196 static void OUTL(struct eth_device* dev, int command, u_long addr)
197 {
198         *(volatile u_long *)(addr + dev->iobase) = cpu_to_le32(command);
199 }
200
201 static struct pci_device_id supported[] = {
202         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST },
203         { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142 },
204 #ifdef CONFIG_TULIP_FIX_DAVICOM
205         { PCI_VENDOR_ID_DAVICOM, PCI_DEVICE_ID_DAVICOM_DM9102A },
206 #endif
207         { }
208 };
209
210 int dc21x4x_initialize(bd_t *bis)
211 {
212         int                     idx=0;
213         int                     card_number = 0;
214         int                     cfrv;
215         unsigned char           timer;
216         pci_dev_t               devbusfn;
217         unsigned int            iobase;
218         unsigned short          status;
219         struct eth_device*      dev;
220
221         while(1) {
222                 devbusfn =  pci_find_devices(supported, idx++);
223                 if (devbusfn == -1) {
224                         break;
225                 }
226
227                 /* Get the chip configuration revision register. */
228                 pci_read_config_dword(devbusfn, PCI_REVISION_ID, &cfrv);
229
230 #ifndef CONFIG_TULIP_FIX_DAVICOM
231                 if ((cfrv & CFRV_RN) < DC2114x_BRK ) {
232                         printf("Error: The chip is not DC21143.\n");
233                         continue;
234                 }
235 #endif
236
237                 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
238                 status |=
239 #ifdef CONFIG_TULIP_USE_IO
240                   PCI_COMMAND_IO |
241 #else
242                   PCI_COMMAND_MEMORY |
243 #endif
244                   PCI_COMMAND_MASTER;
245                 pci_write_config_word(devbusfn, PCI_COMMAND, status);
246
247                 pci_read_config_word(devbusfn, PCI_COMMAND, &status);
248                 if (!(status & PCI_COMMAND_IO)) {
249                         printf("Error: Can not enable I/O access.\n");
250                         continue;
251                 }
252
253                 if (!(status & PCI_COMMAND_IO)) {
254                         printf("Error: Can not enable I/O access.\n");
255                         continue;
256                 }
257
258                 if (!(status & PCI_COMMAND_MASTER)) {
259                         printf("Error: Can not enable Bus Mastering.\n");
260                         continue;
261                 }
262
263                 /* Check the latency timer for values >= 0x60. */
264                 pci_read_config_byte(devbusfn, PCI_LATENCY_TIMER, &timer);
265
266                 if (timer < 0x60) {
267                         pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x60);
268                 }
269
270 #ifdef CONFIG_TULIP_USE_IO
271                 /* read BAR for memory space access */
272                 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_0, &iobase);
273                 iobase &= PCI_BASE_ADDRESS_IO_MASK;
274 #else
275                 /* read BAR for memory space access */
276                 pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &iobase);
277                 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
278 #endif
279
280 #ifdef DEBUG
281                 printf("dc21x4x: DEC 21142 PCI Device @0x%x\n", iobase);
282 #endif
283
284                 dev = (struct eth_device*) malloc(sizeof *dev);
285
286 #ifdef CONFIG_TULIP_FIX_DAVICOM
287             sprintf(dev->name, "Davicom#%d", card_number);
288 #else
289             sprintf(dev->name, "dc21x4x#%d", card_number);
290 #endif
291
292 #ifdef CONFIG_TULIP_USE_IO
293                 dev->iobase = pci_io_to_phys(devbusfn, iobase);
294 #else
295                 dev->iobase = pci_mem_to_phys(devbusfn, iobase);
296 #endif
297                 dev->priv   = (void*) devbusfn;
298                 dev->init   = dc21x4x_init;
299                 dev->halt   = dc21x4x_halt;
300                 dev->send   = dc21x4x_send;
301                 dev->recv   = dc21x4x_recv;
302
303                 /* Ensure we're not sleeping. */
304                 pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
305
306                 udelay(10 * 1000);
307
308 #ifndef CONFIG_TULIP_FIX_DAVICOM
309         read_hw_addr(dev, bis);
310 #endif
311                 eth_register(dev);
312
313                 card_number++;
314         }
315
316         return card_number;
317 }
318
319 static int dc21x4x_init(struct eth_device* dev, bd_t* bis)
320 {
321         int             i;
322         int             devbusfn = (int) dev->priv;
323
324         /* Ensure we're not sleeping. */
325         pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
326
327 #ifdef CONFIG_TULIP_FIX_DAVICOM
328         RESET_DM9102(dev);
329 #else
330         RESET_DE4X5(dev);
331 #endif
332
333         if ((INL(dev, DE4X5_STS) & (STS_TS | STS_RS)) != 0) {
334                 printf("Error: Cannot reset ethernet controller.\n");
335                 return 0;
336         }
337
338 #ifdef CONFIG_TULIP_SELECT_MEDIA
339         dc21x4x_select_media(dev);
340 #else
341         OUTL(dev, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
342 #endif
343
344         for (i = 0; i < NUM_RX_DESC; i++) {
345                 rx_ring[i].status = cpu_to_le32(R_OWN);
346                 rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
347                 rx_ring[i].buf = cpu_to_le32(phys_to_bus((u32) NetRxPackets[i]));
348 #ifdef CONFIG_TULIP_FIX_DAVICOM
349                 rx_ring[i].next = cpu_to_le32(phys_to_bus((u32) &rx_ring[(i+1) % NUM_RX_DESC]));
350 #else
351                 rx_ring[i].next = 0;
352 #endif
353         }
354
355         for (i=0; i < NUM_TX_DESC; i++) {
356                 tx_ring[i].status = 0;
357                 tx_ring[i].des1 = 0;
358                 tx_ring[i].buf = 0;
359
360 #ifdef CONFIG_TULIP_FIX_DAVICOM
361         tx_ring[i].next = cpu_to_le32(phys_to_bus((u32) &tx_ring[(i+1) % NUM_TX_DESC]));
362 #else
363                 tx_ring[i].next = 0;
364 #endif
365         }
366
367         rxRingSize = NUM_RX_DESC;
368         txRingSize = NUM_TX_DESC;
369
370         /* Write the end of list marker to the descriptor lists. */
371         rx_ring[rxRingSize - 1].des1 |= cpu_to_le32(RD_RER);
372         tx_ring[txRingSize - 1].des1 |= cpu_to_le32(TD_TER);
373
374         /* Tell the adapter where the TX/RX rings are located. */
375         OUTL(dev, phys_to_bus((u32) &rx_ring), DE4X5_RRBA);
376         OUTL(dev, phys_to_bus((u32) &tx_ring), DE4X5_TRBA);
377
378         START_DE4X5(dev);
379
380         tx_new = 0;
381         rx_new = 0;
382
383         send_setup_frame(dev, bis);
384
385         return 1;
386 }
387
388 static int dc21x4x_send(struct eth_device* dev, volatile void *packet, int length)
389 {
390         int             status = -1;
391         int             i;
392
393         if (length <= 0) {
394                 printf("%s: bad packet size: %d\n", dev->name, length);
395                 goto Done;
396         }
397
398         for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
399                 if (i >= TOUT_LOOP) {
400                         printf("%s: tx error buffer not ready\n", dev->name);
401                         goto Done;
402                 }
403         }
404
405         tx_ring[tx_new].buf    = cpu_to_le32(phys_to_bus((u32) packet));
406         tx_ring[tx_new].des1   = cpu_to_le32(TD_TER | TD_LS | TD_FS | length);
407         tx_ring[tx_new].status = cpu_to_le32(T_OWN);
408
409         OUTL(dev, POLL_DEMAND, DE4X5_TPD);
410
411         for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
412                 if (i >= TOUT_LOOP) {
413                         printf(".%s: tx buffer not ready\n", dev->name);
414                         goto Done;
415                 }
416         }
417
418         if (le32_to_cpu(tx_ring[tx_new].status) & TD_ES) {
419 #if 0 /* test-only */
420                 printf("TX error status = 0x%08X\n",
421                        le32_to_cpu(tx_ring[tx_new].status));
422 #endif
423                 tx_ring[tx_new].status = 0x0;
424                 goto Done;
425         }
426
427         status = length;
428
429  Done:
430     tx_new = (tx_new+1) % NUM_TX_DESC;
431         return status;
432 }
433
434 static int dc21x4x_recv(struct eth_device* dev)
435 {
436         s32             status;
437         int             length    = 0;
438
439         for ( ; ; ) {
440                 status = (s32)le32_to_cpu(rx_ring[rx_new].status);
441
442                 if (status & R_OWN) {
443                         break;
444                 }
445
446                 if (status & RD_LS) {
447                         /* Valid frame status.
448                          */
449                         if (status & RD_ES) {
450
451                                 /* There was an error.
452                                  */
453                                 printf("RX error status = 0x%08X\n", status);
454                         } else {
455                                 /* A valid frame received.
456                                  */
457                                 length = (le32_to_cpu(rx_ring[rx_new].status) >> 16);
458
459                                 /* Pass the packet up to the protocol
460                                  * layers.
461                                  */
462                                 NetReceive(NetRxPackets[rx_new], length - 4);
463                         }
464
465                         /* Change buffer ownership for this frame, back
466                          * to the adapter.
467                          */
468                         rx_ring[rx_new].status = cpu_to_le32(R_OWN);
469                 }
470
471                 /* Update entry information.
472                  */
473                 rx_new = (rx_new + 1) % rxRingSize;
474         }
475
476         return length;
477 }
478
479 static void dc21x4x_halt(struct eth_device* dev)
480 {
481         int             devbusfn = (int) dev->priv;
482
483         STOP_DE4X5(dev);
484         OUTL(dev, 0, DE4X5_SICR);
485
486         pci_write_config_byte(devbusfn, PCI_CFDA_PSM, SLEEP);
487 }
488
489 static void send_setup_frame(struct eth_device* dev, bd_t *bis)
490 {
491         int             i;
492         char    setup_frame[SETUP_FRAME_LEN];
493         char    *pa = &setup_frame[0];
494
495         memset(pa, 0xff, SETUP_FRAME_LEN);
496
497         for (i = 0; i < ETH_ALEN; i++) {
498                 *(pa + (i & 1)) = dev->enetaddr[i];
499                 if (i & 0x01) {
500                         pa += 4;
501                 }
502         }
503
504         for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
505                 if (i >= TOUT_LOOP) {
506                         printf("%s: tx error buffer not ready\n", dev->name);
507                         goto Done;
508                 }
509         }
510
511         tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32) &setup_frame[0]));
512         tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_SET| SETUP_FRAME_LEN);
513         tx_ring[tx_new].status = cpu_to_le32(T_OWN);
514
515         OUTL(dev, POLL_DEMAND, DE4X5_TPD);
516
517         for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
518                 if (i >= TOUT_LOOP) {
519                         printf("%s: tx buffer not ready\n", dev->name);
520                         goto Done;
521                 }
522         }
523
524         if (le32_to_cpu(tx_ring[tx_new].status) != 0x7FFFFFFF) {
525                 printf("TX error status2 = 0x%08X\n", le32_to_cpu(tx_ring[tx_new].status));
526         }
527         tx_new = (tx_new+1) % NUM_TX_DESC;
528
529 Done:
530         return;
531 }
532
533 /* SROM Read and write routines.
534  */
535
536 static void
537 sendto_srom(struct eth_device* dev, u_int command, u_long addr)
538 {
539         OUTL(dev, command, addr);
540         udelay(1);
541 }
542
543 static int
544 getfrom_srom(struct eth_device* dev, u_long addr)
545 {
546         s32 tmp;
547
548         tmp = INL(dev, addr);
549         udelay(1);
550
551         return tmp;
552 }
553
554 /* Note: this routine returns extra data bits for size detection. */
555 static int do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location, int addr_len)
556 {
557         int i;
558         unsigned retval = 0;
559         int read_cmd = location | (SROM_READ_CMD << addr_len);
560
561         sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
562         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
563
564 #ifdef DEBUG_SROM
565         printf(" EEPROM read at %d ", location);
566 #endif
567
568         /* Shift the read command bits out. */
569         for (i = 4 + addr_len; i >= 0; i--) {
570                 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
571                 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval, ioaddr);
572                 udelay(10);
573                 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK, ioaddr);
574                 udelay(10);
575 #ifdef DEBUG_SROM2
576                 printf("%X", getfrom_srom(dev, ioaddr) & 15);
577 #endif
578                 retval = (retval << 1) | ((getfrom_srom(dev, ioaddr) & EE_DATA_READ) ? 1 : 0);
579         }
580
581         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
582
583 #ifdef DEBUG_SROM2
584         printf(" :%X:", getfrom_srom(dev, ioaddr) & 15);
585 #endif
586
587         for (i = 16; i > 0; i--) {
588                 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
589                 udelay(10);
590 #ifdef DEBUG_SROM2
591                 printf("%X", getfrom_srom(dev, ioaddr) & 15);
592 #endif
593                 retval = (retval << 1) | ((getfrom_srom(dev, ioaddr) & EE_DATA_READ) ? 1 : 0);
594                 sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
595                 udelay(10);
596         }
597
598         /* Terminate the EEPROM access. */
599         sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
600
601 #ifdef DEBUG_SROM2
602         printf(" EEPROM value at %d is %5.5x.\n", location, retval);
603 #endif
604
605         return retval;
606 }
607
608 /* This executes a generic EEPROM command, typically a write or write enable.
609    It returns the data output from the EEPROM, and thus may also be used for
610    reads. */
611 static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd, int cmd_len)
612 {
613         unsigned retval = 0;
614
615 #ifdef DEBUG_SROM
616         printf(" EEPROM op 0x%x: ", cmd);
617 #endif
618
619         sendto_srom(dev,SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
620
621         /* Shift the command bits out. */
622         do {
623                 short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
624                 sendto_srom(dev,dataval, ioaddr);
625                 udelay(10);
626
627 #ifdef DEBUG_SROM2
628                 printf("%X", getfrom_srom(dev,ioaddr) & 15);
629 #endif
630
631                 sendto_srom(dev,dataval | DT_CLK, ioaddr);
632                 udelay(10);
633                 retval = (retval << 1) | ((getfrom_srom(dev,ioaddr) & EE_DATA_READ) ? 1 : 0);
634         } while (--cmd_len >= 0);
635         sendto_srom(dev,SROM_RD | SROM_SR | DT_CS, ioaddr);
636
637         /* Terminate the EEPROM access. */
638         sendto_srom(dev,SROM_RD | SROM_SR, ioaddr);
639
640 #ifdef DEBUG_SROM
641         printf(" EEPROM result is 0x%5.5x.\n", retval);
642 #endif
643
644         return retval;
645 }
646
647 static int read_srom(struct eth_device *dev, u_long ioaddr, int index)
648 {
649         int ee_addr_size = do_read_eeprom(dev, ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
650
651         return do_eeprom_cmd(dev, ioaddr,
652                              (((SROM_READ_CMD << ee_addr_size) | index) << 16)
653                              | 0xffff, 3 + ee_addr_size + 16);
654 }
655
656 #ifdef UPDATE_SROM
657 static int write_srom(struct eth_device *dev, u_long ioaddr, int index, int new_value)
658 {
659         int ee_addr_size = do_read_eeprom(dev, ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
660         int i;
661         unsigned short newval;
662
663         udelay(10*1000); /* test-only */
664
665 #ifdef DEBUG_SROM
666         printf("ee_addr_size=%d.\n", ee_addr_size);
667         printf("Writing new entry 0x%4.4x to offset %d.\n", new_value, index);
668 #endif
669
670         /* Enable programming modes. */
671         do_eeprom_cmd(dev, ioaddr, (0x4f << (ee_addr_size-4)), 3+ee_addr_size);
672
673         /* Do the actual write. */
674         do_eeprom_cmd(dev, ioaddr,
675                       (((SROM_WRITE_CMD<<ee_addr_size)|index) << 16) | new_value,
676                       3 + ee_addr_size + 16);
677
678         /* Poll for write finished. */
679         sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
680         for (i = 0; i < 10000; i++)                     /* Typical 2000 ticks */
681                 if (getfrom_srom(dev, ioaddr) & EE_DATA_READ)
682                         break;
683
684 #ifdef DEBUG_SROM
685         printf(" Write finished after %d ticks.\n", i);
686 #endif
687
688         /* Disable programming. */
689         do_eeprom_cmd(dev, ioaddr, (0x40 << (ee_addr_size-4)), 3 + ee_addr_size);
690
691         /* And read the result. */
692         newval = do_eeprom_cmd(dev, ioaddr,
693                                (((SROM_READ_CMD<<ee_addr_size)|index) << 16)
694                                | 0xffff, 3 + ee_addr_size + 16);
695 #ifdef DEBUG_SROM
696         printf("  New value at offset %d is %4.4x.\n", index, newval);
697 #endif
698         return 1;
699 }
700 #endif
701
702 static void read_hw_addr(struct eth_device *dev, bd_t *bis)
703 {
704         u_short tmp, *p = (short *)(&dev->enetaddr[0]);
705         int i, j = 0;
706
707         for (i = 0; i < (ETH_ALEN >> 1); i++) {
708                 tmp = read_srom(dev, DE4X5_APROM, ((SROM_HWADD >> 1) + i));
709                 *p = le16_to_cpu(tmp);
710                 j += *p++;
711         }
712
713         if ((j == 0) || (j == 0x2fffd)) {
714                 memset (dev->enetaddr, 0, ETH_ALEN);
715 #ifdef DEBUG
716                 printf("Warning: can't read HW address from SROM.\n");
717 #endif
718                 goto Done;
719         }
720
721         return;
722
723 Done:
724 #ifdef UPDATE_SROM
725         update_srom(dev, bis);
726 #endif
727         return;
728 }
729
730 #ifdef UPDATE_SROM
731 static void update_srom(struct eth_device *dev, bd_t *bis)
732 {
733         int i;
734         static unsigned short eeprom[0x40] = {
735                 0x140b, 0x6610, 0x0000, 0x0000,         /* 00 */
736                 0x0000, 0x0000, 0x0000, 0x0000,         /* 04 */
737                 0x00a3, 0x0103, 0x0000, 0x0000,         /* 08 */
738                 0x0000, 0x1f00, 0x0000, 0x0000,         /* 0c */
739                 0x0108, 0x038d, 0x0000, 0x0000,         /* 10 */
740                 0xe078, 0x0001, 0x0040, 0x0018,         /* 14 */
741                 0x0000, 0x0000, 0x0000, 0x0000,         /* 18 */
742                 0x0000, 0x0000, 0x0000, 0x0000,         /* 1c */
743                 0x0000, 0x0000, 0x0000, 0x0000,         /* 20 */
744                 0x0000, 0x0000, 0x0000, 0x0000,         /* 24 */
745                 0x0000, 0x0000, 0x0000, 0x0000,         /* 28 */
746                 0x0000, 0x0000, 0x0000, 0x0000,         /* 2c */
747                 0x0000, 0x0000, 0x0000, 0x0000,         /* 30 */
748                 0x0000, 0x0000, 0x0000, 0x0000,         /* 34 */
749                 0x0000, 0x0000, 0x0000, 0x0000,         /* 38 */
750                 0x0000, 0x0000, 0x0000, 0x4e07,         /* 3c */
751         };
752
753         /* Ethernet Addr... */
754         eeprom[0x0a] = ((bis->bi_enetaddr[1] & 0xff) << 8) | (bis->bi_enetaddr[0] & 0xff);
755         eeprom[0x0b] = ((bis->bi_enetaddr[3] & 0xff) << 8) | (bis->bi_enetaddr[2] & 0xff);
756         eeprom[0x0c] = ((bis->bi_enetaddr[5] & 0xff) << 8) | (bis->bi_enetaddr[4] & 0xff);
757
758         for (i=0; i<0x40; i++)
759         {
760                 write_srom(dev, DE4X5_APROM, i, eeprom[i]);
761         }
762 }
763 #endif
764
765 #endif