net: dm9000: Drop volatiles
[platform/kernel/u-boot.git] / drivers / net / dm9000x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3   dm9000.c: Version 1.2 12/15/2003
4
5         A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
6         Copyright (C) 1997  Sten Wang
7
8   (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
9
10 V0.11   06/20/2001      REG_0A bit3=1, default enable BP with DA match
11         06/22/2001      Support DM9801 progrmming
12                         E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
13                         E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
14                 R17 = (R17 & 0xfff0) | NF + 3
15                         E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
16                 R17 = (R17 & 0xfff0) | NF
17
18 v1.00                   modify by simon 2001.9.5
19                         change for kernel 2.4.x
20
21 v1.1   11/09/2001       fix force mode bug
22
23 v1.2   03/18/2003       Weilun Huang <weilun_huang@davicom.com.tw>:
24                         Fixed phy reset.
25                         Added tx/rx 32 bit mode.
26                         Cleaned up for kernel merge.
27
28 --------------------------------------
29
30        12/15/2003       Initial port to u-boot by
31                         Sascha Hauer <saschahauer@web.de>
32
33        06/03/2008       Remy Bohmer <linux@bohmer.net>
34                         - Fixed the driver to work with DM9000A.
35                           (check on ISR receive status bit before reading the
36                           FIFO as described in DM9000 programming guide and
37                           application notes)
38                         - Added autodetect of databus width.
39                         - Made debug code compile again.
40                         - Adapt eth_send such that it matches the DM9000*
41                           application notes. Needed to make it work properly
42                           for DM9000A.
43                         - Adapted reset procedure to match DM9000 application
44                           notes (i.e. double reset)
45                         - some minor code cleanups
46                         These changes are tested with DM9000{A,EP,E} together
47                         with a 200MHz Atmel AT91SAM9261 core
48
49 TODO: external MII is not functional, only internal at the moment.
50 */
51
52 #include <common.h>
53 #include <command.h>
54 #include <net.h>
55 #include <asm/io.h>
56 #include <dm9000.h>
57 #include <linux/delay.h>
58
59 #include "dm9000x.h"
60
61 /* Structure/enum declaration ------------------------------- */
62 typedef struct board_info {
63         u32 runt_length_counter;        /* counter: RX length < 64byte */
64         u32 long_length_counter;        /* counter: RX length > 1514byte */
65         u32 reset_counter;      /* counter: RESET */
66         u32 reset_tx_timeout;   /* RESET caused by TX Timeout */
67         u32 reset_rx_status;    /* RESET caused by RX Statsus wrong */
68         u16 tx_pkt_cnt;
69         u16 queue_start_addr;
70         u16 dbug_cnt;
71         u8 phy_addr;
72         u8 device_wait_reset;   /* device state */
73         unsigned char srom[128];
74         void (*outblk)(void *data_ptr, int count);
75         void (*inblk)(void *data_ptr, int count);
76         void (*rx_status)(u16 *rxstatus, u16 *rxlen);
77         struct eth_device netdev;
78 } board_info_t;
79 static board_info_t dm9000_info;
80
81
82 /* function declaration ------------------------------------- */
83 static int dm9000_probe(void);
84 static u16 dm9000_phy_read(int);
85 static void dm9000_phy_write(int, u16);
86 static u8 dm9000_ior(int);
87 static void dm9000_iow(int reg, u8 value);
88
89 /* DM9000 network board routine ---------------------------- */
90 #ifndef CONFIG_DM9000_BYTE_SWAPPED
91 #define dm9000_outb(d,r) writeb((d), (r))
92 #define dm9000_outw(d,r) writew((d), (r))
93 #define dm9000_outl(d,r) writel((d), (r))
94 #define dm9000_inb(r) readb(r)
95 #define dm9000_inw(r) readw(r)
96 #define dm9000_inl(r) readl(r)
97 #else
98 #define dm9000_outb(d, r) __raw_writeb(d, r)
99 #define dm9000_outw(d, r) __raw_writew(d, r)
100 #define dm9000_outl(d, r) __raw_writel(d, r)
101 #define dm9000_inb(r) __raw_readb(r)
102 #define dm9000_inw(r) __raw_readw(r)
103 #define dm9000_inl(r) __raw_readl(r)
104 #endif
105
106 #ifdef DEBUG
107 static void dm9000_dump_packet(const char *func, u8 *packet, int length)
108 {
109         int i;
110
111         printf("%s: length: %d\n", func, length);
112
113         for (i = 0; i < length; i++) {
114                 if (i % 8 == 0)
115                         printf("\n%s: %02x: ", func, i);
116                 printf("%02x ", packet[i]);
117         }
118
119         printf("\n");
120 }
121 #else
122 static void dm9000_dump_packet(const char *func, u8 *packet, int length) {}
123 #endif
124
125 static void dm9000_outblk_8bit(void *data_ptr, int count)
126 {
127         int i;
128         for (i = 0; i < count; i++)
129                 dm9000_outb((((u8 *) data_ptr)[i] & 0xff), DM9000_DATA);
130 }
131
132 static void dm9000_outblk_16bit(void *data_ptr, int count)
133 {
134         int i;
135         u32 tmplen = (count + 1) / 2;
136
137         for (i = 0; i < tmplen; i++)
138                 dm9000_outw(((u16 *) data_ptr)[i], DM9000_DATA);
139 }
140 static void dm9000_outblk_32bit(void *data_ptr, int count)
141 {
142         int i;
143         u32 tmplen = (count + 3) / 4;
144
145         for (i = 0; i < tmplen; i++)
146                 dm9000_outl(((u32 *) data_ptr)[i], DM9000_DATA);
147 }
148
149 static void dm9000_inblk_8bit(void *data_ptr, int count)
150 {
151         int i;
152         for (i = 0; i < count; i++)
153                 ((u8 *) data_ptr)[i] = dm9000_inb(DM9000_DATA);
154 }
155
156 static void dm9000_inblk_16bit(void *data_ptr, int count)
157 {
158         int i;
159         u32 tmplen = (count + 1) / 2;
160
161         for (i = 0; i < tmplen; i++)
162                 ((u16 *) data_ptr)[i] = dm9000_inw(DM9000_DATA);
163 }
164 static void dm9000_inblk_32bit(void *data_ptr, int count)
165 {
166         int i;
167         u32 tmplen = (count + 3) / 4;
168
169         for (i = 0; i < tmplen; i++)
170                 ((u32 *) data_ptr)[i] = dm9000_inl(DM9000_DATA);
171 }
172
173 static void dm9000_rx_status_32bit(u16 *rxstatus, u16 *rxlen)
174 {
175         u32 tmpdata;
176
177         dm9000_outb(DM9000_MRCMD, DM9000_IO);
178
179         tmpdata = dm9000_inl(DM9000_DATA);
180         *rxstatus = __le16_to_cpu(tmpdata);
181         *rxlen = __le16_to_cpu(tmpdata >> 16);
182 }
183
184 static void dm9000_rx_status_16bit(u16 *rxstatus, u16 *rxlen)
185 {
186         dm9000_outb(DM9000_MRCMD, DM9000_IO);
187
188         *rxstatus = __le16_to_cpu(dm9000_inw(DM9000_DATA));
189         *rxlen = __le16_to_cpu(dm9000_inw(DM9000_DATA));
190 }
191
192 static void dm9000_rx_status_8bit(u16 *rxstatus, u16 *rxlen)
193 {
194         dm9000_outb(DM9000_MRCMD, DM9000_IO);
195
196         *rxstatus =
197             __le16_to_cpu(dm9000_inb(DM9000_DATA) +
198                           (dm9000_inb(DM9000_DATA) << 8));
199         *rxlen =
200             __le16_to_cpu(dm9000_inb(DM9000_DATA) +
201                           (dm9000_inb(DM9000_DATA) << 8));
202 }
203
204 /*
205   Search DM9000 board, allocate space and register it
206 */
207 int
208 dm9000_probe(void)
209 {
210         u32 id_val;
211         id_val = dm9000_ior(DM9000_VIDL);
212         id_val |= dm9000_ior(DM9000_VIDH) << 8;
213         id_val |= dm9000_ior(DM9000_PIDL) << 16;
214         id_val |= dm9000_ior(DM9000_PIDH) << 24;
215         if (id_val == DM9000_ID) {
216                 printf("dm9000 i/o: 0x%x, id: 0x%x \n", CONFIG_DM9000_BASE,
217                        id_val);
218                 return 0;
219         } else {
220                 printf("dm9000 not found at 0x%08x id: 0x%08x\n",
221                        CONFIG_DM9000_BASE, id_val);
222                 return -1;
223         }
224 }
225
226 /* General Purpose dm9000 reset routine */
227 static void
228 dm9000_reset(void)
229 {
230         debug("resetting DM9000\n");
231
232         /* Reset DM9000,
233            see DM9000 Application Notes V1.22 Jun 11, 2004 page 29 */
234
235         /* DEBUG: Make all GPIO0 outputs, all others inputs */
236         dm9000_iow(DM9000_GPCR, GPCR_GPIO0_OUT);
237         /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */
238         dm9000_iow(DM9000_GPR, 0);
239         /* Step 2: Software reset */
240         dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST));
241
242         do {
243                 debug("resetting the DM9000, 1st reset\n");
244                 udelay(25); /* Wait at least 20 us */
245         } while (dm9000_ior(DM9000_NCR) & 1);
246
247         dm9000_iow(DM9000_NCR, 0);
248         dm9000_iow(DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */
249
250         do {
251                 debug("resetting the DM9000, 2nd reset\n");
252                 udelay(25); /* Wait at least 20 us */
253         } while (dm9000_ior(DM9000_NCR) & 1);
254
255         /* Check whether the ethernet controller is present */
256         if ((dm9000_ior(DM9000_PIDL) != 0x0) ||
257             (dm9000_ior(DM9000_PIDH) != 0x90))
258                 printf("ERROR: resetting DM9000 -> not responding\n");
259 }
260
261 /* Initialize dm9000 board
262 */
263 static int dm9000_init(struct eth_device *dev, struct bd_info *bd)
264 {
265         int i, oft, lnk;
266         u8 io_mode;
267         struct board_info *db = &dm9000_info;
268
269         debug("%s\n", __func__);
270
271         /* RESET device */
272         dm9000_reset();
273
274         if (dm9000_probe() < 0)
275                 return -1;
276
277         /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
278         io_mode = dm9000_ior(DM9000_ISR) >> 6;
279
280         switch (io_mode) {
281         case 0x0:  /* 16-bit mode */
282                 printf("DM9000: running in 16 bit mode\n");
283                 db->outblk    = dm9000_outblk_16bit;
284                 db->inblk     = dm9000_inblk_16bit;
285                 db->rx_status = dm9000_rx_status_16bit;
286                 break;
287         case 0x01:  /* 32-bit mode */
288                 printf("DM9000: running in 32 bit mode\n");
289                 db->outblk    = dm9000_outblk_32bit;
290                 db->inblk     = dm9000_inblk_32bit;
291                 db->rx_status = dm9000_rx_status_32bit;
292                 break;
293         case 0x02: /* 8 bit mode */
294                 printf("DM9000: running in 8 bit mode\n");
295                 db->outblk    = dm9000_outblk_8bit;
296                 db->inblk     = dm9000_inblk_8bit;
297                 db->rx_status = dm9000_rx_status_8bit;
298                 break;
299         default:
300                 /* Assume 8 bit mode, will probably not work anyway */
301                 printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
302                 db->outblk    = dm9000_outblk_8bit;
303                 db->inblk     = dm9000_inblk_8bit;
304                 db->rx_status = dm9000_rx_status_8bit;
305                 break;
306         }
307
308         /* Program operating register, only internal phy supported */
309         dm9000_iow(DM9000_NCR, 0x0);
310         /* TX Polling clear */
311         dm9000_iow(DM9000_TCR, 0);
312         /* Less 3Kb, 200us */
313         dm9000_iow(DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US);
314         /* Flow Control : High/Low Water */
315         dm9000_iow(DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8));
316         /* SH FIXME: This looks strange! Flow Control */
317         dm9000_iow(DM9000_FCR, 0x0);
318         /* Special Mode */
319         dm9000_iow(DM9000_SMCR, 0);
320         /* clear TX status */
321         dm9000_iow(DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
322         /* Clear interrupt status */
323         dm9000_iow(DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS);
324
325         printf("MAC: %pM\n", dev->enetaddr);
326         if (!is_valid_ethaddr(dev->enetaddr)) {
327                 printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n");
328         }
329
330         /* fill device MAC address registers */
331         for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
332                 dm9000_iow(oft, dev->enetaddr[i]);
333         for (i = 0, oft = 0x16; i < 8; i++, oft++)
334                 dm9000_iow(oft, 0xff);
335
336         /* read back mac, just to be sure */
337         for (i = 0, oft = 0x10; i < 6; i++, oft++)
338                 debug("%02x:", dm9000_ior(oft));
339         debug("\n");
340
341         /* Activate DM9000 */
342         /* RX enable */
343         dm9000_iow(DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
344         /* Enable TX/RX interrupt mask */
345         dm9000_iow(DM9000_IMR, IMR_PAR);
346
347         i = 0;
348         while (!(dm9000_phy_read(1) & 0x20)) {  /* autonegation complete bit */
349                 udelay(1000);
350                 i++;
351                 if (i == 10000) {
352                         printf("could not establish link\n");
353                         return 0;
354                 }
355         }
356
357         /* see what we've got */
358         lnk = dm9000_phy_read(17) >> 12;
359         printf("operating at ");
360         switch (lnk) {
361         case 1:
362                 printf("10M half duplex ");
363                 break;
364         case 2:
365                 printf("10M full duplex ");
366                 break;
367         case 4:
368                 printf("100M half duplex ");
369                 break;
370         case 8:
371                 printf("100M full duplex ");
372                 break;
373         default:
374                 printf("unknown: %d ", lnk);
375                 break;
376         }
377         printf("mode\n");
378         return 0;
379 }
380
381 /*
382   Hardware start transmission.
383   Send a packet to media from the upper layer.
384 */
385 static int dm9000_send(struct eth_device *netdev, void *packet, int length)
386 {
387         int tmo;
388         struct board_info *db = &dm9000_info;
389
390         dm9000_dump_packet(__func__ , packet, length);
391
392         dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
393
394         /* Move data to DM9000 TX RAM */
395         dm9000_outb(DM9000_MWCMD, DM9000_IO); /* Prepare for TX-data */
396
397         /* push the data to the TX-fifo */
398         (db->outblk)(packet, length);
399
400         /* Set TX length to DM9000 */
401         dm9000_iow(DM9000_TXPLL, length & 0xff);
402         dm9000_iow(DM9000_TXPLH, (length >> 8) & 0xff);
403
404         /* Issue TX polling command */
405         dm9000_iow(DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
406
407         /* wait for end of transmission */
408         tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
409         while ( !(dm9000_ior(DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) ||
410                 !(dm9000_ior(DM9000_ISR) & IMR_PTM) ) {
411                 if (get_timer(0) >= tmo) {
412                         printf("transmission timeout\n");
413                         break;
414                 }
415         }
416         dm9000_iow(DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
417
418         debug("transmit done\n\n");
419         return 0;
420 }
421
422 /*
423   Stop the interface.
424   The interface is stopped when it is brought.
425 */
426 static void dm9000_halt(struct eth_device *netdev)
427 {
428         debug("%s\n", __func__);
429
430         /* RESET devie */
431         dm9000_phy_write(0, 0x8000);    /* PHY RESET */
432         dm9000_iow(DM9000_GPR, 0x01);   /* Power-Down PHY */
433         dm9000_iow(DM9000_IMR, 0x80);   /* Disable all interrupt */
434         dm9000_iow(DM9000_RCR, 0x00);   /* Disable RX */
435 }
436
437 /*
438   Received a packet and pass to upper layer
439 */
440 static int dm9000_rx(struct eth_device *netdev)
441 {
442         u8 rxbyte;
443         u8 *rdptr = (u8 *)net_rx_packets[0];
444         u16 rxstatus, rxlen = 0;
445         struct board_info *db = &dm9000_info;
446
447         /* Check packet ready or not, we must check
448            the ISR status first for DM9000A */
449         if (!(dm9000_ior(DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */
450                 return 0;
451
452         dm9000_iow(DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */
453
454         /* There is _at least_ 1 package in the fifo, read them all */
455         for (;;) {
456                 dm9000_ior(DM9000_MRCMDX);      /* Dummy read */
457
458                 /* Get most updated data,
459                    only look at bits 0:1, See application notes DM9000 */
460                 rxbyte = dm9000_inb(DM9000_DATA) & 0x03;
461
462                 /* Status check: this byte must be 0 or 1 */
463                 if (rxbyte > DM9000_PKT_RDY) {
464                         dm9000_iow(DM9000_RCR, 0x00);   /* Stop Device */
465                         dm9000_iow(DM9000_ISR, 0x80);   /* Stop INT request */
466                         printf("DM9000 error: status check fail: 0x%x\n",
467                                 rxbyte);
468                         return 0;
469                 }
470
471                 if (rxbyte != DM9000_PKT_RDY)
472                         return 0; /* No packet received, ignore */
473
474                 debug("receiving packet\n");
475
476                 /* A packet ready now  & Get status/length */
477                 (db->rx_status)(&rxstatus, &rxlen);
478
479                 debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen);
480
481                 /* Move data from DM9000 */
482                 /* Read received packet from RX SRAM */
483                 (db->inblk)(rdptr, rxlen);
484
485                 if ((rxstatus & 0xbf00) || (rxlen < 0x40)
486                         || (rxlen > DM9000_PKT_MAX)) {
487                         if (rxstatus & 0x100) {
488                                 printf("rx fifo error\n");
489                         }
490                         if (rxstatus & 0x200) {
491                                 printf("rx crc error\n");
492                         }
493                         if (rxstatus & 0x8000) {
494                                 printf("rx length error\n");
495                         }
496                         if (rxlen > DM9000_PKT_MAX) {
497                                 printf("rx length too big\n");
498                                 dm9000_reset();
499                         }
500                 } else {
501                         dm9000_dump_packet(__func__ , rdptr, rxlen);
502
503                         debug("passing packet to upper layer\n");
504                         net_process_received_packet(net_rx_packets[0], rxlen);
505                 }
506         }
507         return 0;
508 }
509
510 /*
511   Read a word data from SROM
512 */
513 #if !defined(CONFIG_DM9000_NO_SROM)
514 void dm9000_read_srom_word(int offset, u8 *to)
515 {
516         dm9000_iow(DM9000_EPAR, offset);
517         dm9000_iow(DM9000_EPCR, 0x4);
518         udelay(8000);
519         dm9000_iow(DM9000_EPCR, 0x0);
520         to[0] = dm9000_ior(DM9000_EPDRL);
521         to[1] = dm9000_ior(DM9000_EPDRH);
522 }
523
524 void dm9000_write_srom_word(int offset, u16 val)
525 {
526         dm9000_iow(DM9000_EPAR, offset);
527         dm9000_iow(DM9000_EPDRH, ((val >> 8) & 0xff));
528         dm9000_iow(DM9000_EPDRL, (val & 0xff));
529         dm9000_iow(DM9000_EPCR, 0x12);
530         udelay(8000);
531         dm9000_iow(DM9000_EPCR, 0);
532 }
533 #endif
534
535 static void dm9000_get_enetaddr(struct eth_device *dev)
536 {
537 #if !defined(CONFIG_DM9000_NO_SROM)
538         int i;
539         for (i = 0; i < 3; i++)
540                 dm9000_read_srom_word(i, dev->enetaddr + (2 * i));
541 #endif
542 }
543
544 /*
545    Read a byte from I/O port
546 */
547 static u8
548 dm9000_ior(int reg)
549 {
550         dm9000_outb(reg, DM9000_IO);
551         return dm9000_inb(DM9000_DATA);
552 }
553
554 /*
555    Write a byte to I/O port
556 */
557 static void
558 dm9000_iow(int reg, u8 value)
559 {
560         dm9000_outb(reg, DM9000_IO);
561         dm9000_outb(value, DM9000_DATA);
562 }
563
564 /*
565    Read a word from phyxcer
566 */
567 static u16
568 dm9000_phy_read(int reg)
569 {
570         u16 val;
571
572         /* Fill the phyxcer register into REG_0C */
573         dm9000_iow(DM9000_EPAR, DM9000_PHY | reg);
574         dm9000_iow(DM9000_EPCR, 0xc);   /* Issue phyxcer read command */
575         udelay(100);                    /* Wait read complete */
576         dm9000_iow(DM9000_EPCR, 0x0);   /* Clear phyxcer read command */
577         val = (dm9000_ior(DM9000_EPDRH) << 8) | dm9000_ior(DM9000_EPDRL);
578
579         /* The read data keeps on REG_0D & REG_0E */
580         debug("dm9000_phy_read(0x%x): 0x%x\n", reg, val);
581         return val;
582 }
583
584 /*
585    Write a word to phyxcer
586 */
587 static void
588 dm9000_phy_write(int reg, u16 value)
589 {
590
591         /* Fill the phyxcer register into REG_0C */
592         dm9000_iow(DM9000_EPAR, DM9000_PHY | reg);
593
594         /* Fill the written data into REG_0D & REG_0E */
595         dm9000_iow(DM9000_EPDRL, (value & 0xff));
596         dm9000_iow(DM9000_EPDRH, ((value >> 8) & 0xff));
597         dm9000_iow(DM9000_EPCR, 0xa);   /* Issue phyxcer write command */
598         udelay(500);                    /* Wait write complete */
599         dm9000_iow(DM9000_EPCR, 0x0);   /* Clear phyxcer write command */
600         debug("dm9000_phy_write(reg:0x%x, value:0x%x)\n", reg, value);
601 }
602
603 int dm9000_initialize(struct bd_info *bis)
604 {
605         struct eth_device *dev = &(dm9000_info.netdev);
606
607         /* Load MAC address from EEPROM */
608         dm9000_get_enetaddr(dev);
609
610         dev->init = dm9000_init;
611         dev->halt = dm9000_halt;
612         dev->send = dm9000_send;
613         dev->recv = dm9000_rx;
614         strcpy(dev->name, "dm9000");
615
616         eth_register(dev);
617
618         return 0;
619 }