2 Ported to U-Boot by Christian Pellegrin <chri@ascensit.com>
4 Based on sources from the Linux kernel (pcnet_cs.c, 8390.h) and
5 eCOS(if_dp83902a.c, if_dp83902a.h). Both of these 2 wonderful world
6 are GPL, so this is, of course, GPL.
8 ==========================================================================
12 Ethernet device driver for NS DP83902a ethernet controller
14 ==========================================================================
15 ####ECOSGPLCOPYRIGHTBEGIN####
16 -------------------------------------------
17 This file is part of eCos, the Embedded Configurable Operating System.
18 Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
20 eCos is free software; you can redistribute it and/or modify it under
21 the terms of the GNU General Public License as published by the Free
22 Software Foundation; either version 2 or (at your option) any later version.
24 eCos is distributed in the hope that it will be useful, but WITHOUT ANY
25 WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 You should have received a copy of the GNU General Public License along
30 with eCos; if not, write to the Free Software Foundation, Inc.,
31 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
33 As a special exception, if other files instantiate templates or use macros
34 or inline functions from this file, or you compile this file and link it
35 with other works to produce a work based on this file, this file does not
36 by itself cause the resulting work to be covered by the GNU General Public
37 License. However the source code for this file must still be made available
38 in accordance with section (3) of the GNU General Public License.
40 This exception does not invalidate any other reasons why a work based on
41 this file might be covered by the GNU General Public License.
43 Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
44 at http://sources.redhat.com/ecos/ecos-license/
45 -------------------------------------------
46 ####ECOSGPLCOPYRIGHTEND####
47 ####BSDCOPYRIGHTBEGIN####
49 -------------------------------------------
51 Portions of this software may have been derived from OpenBSD or other sources,
52 and are covered by the appropriate copyright disclaimers included herein.
54 -------------------------------------------
56 ####BSDCOPYRIGHTEND####
57 ==========================================================================
58 #####DESCRIPTIONBEGIN####
61 Contributors: gthomas, jskov, rsandifo
66 FIXME: Will fail if pinged with large packets (1520 bytes)
70 ####DESCRIPTIONEND####
72 ==========================================================================
79 #include <linux/compiler.h>
81 /* forward definition of function used for the uboot interface */
82 void uboot_push_packet_len(int len);
83 void uboot_push_tx_done(int key, int val);
85 /* NE2000 base header file */
86 #include "ne2000_base.h"
88 #if defined(CONFIG_DRIVER_AX88796L)
89 /* AX88796L support */
92 /* Basic NE2000 chip support */
96 static dp83902a_priv_data_t nic; /* just one instance of the card supported */
99 * This function reads the MAC address from the serial EEPROM,
100 * used if PROM read fails. Does nothing for ax88796 chips (sh boards)
103 dp83902a_init(unsigned char *enetaddr)
105 dp83902a_priv_data_t *dp = &nic;
107 #if defined(NE2000_BASIC_INIT)
115 return false; /* No device found */
119 #if defined(NE2000_BASIC_INIT)
120 /* AX88796L doesn't need */
122 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
123 /* Use the address from the serial EEPROM */
124 for (i = 0; i < 6; i++)
125 DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
126 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
128 printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
137 memcpy(enetaddr, dp->esa, 6); /* Use MAC from serial EEPROM */
138 #endif /* NE2000_BASIC_INIT */
145 dp83902a_priv_data_t *dp = &nic;
150 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
151 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
152 DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
158 * This function is called to "start up" the interface. It may be called
159 * multiple times, even when the hardware is already running. It will be
160 * called whenever something "hardware oriented" changes and should leave
161 * the hardware ready to send/receive packets.
164 dp83902a_start(u8 * enaddr)
166 dp83902a_priv_data_t *dp = &nic;
170 debug("The MAC is %pM\n", enaddr);
174 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
175 DP_OUT(base, DP_DCR, DP_DCR_INIT);
176 DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
177 DP_OUT(base, DP_RBCL, 0);
178 DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
179 DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
180 DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
181 dp->tx1 = dp->tx2 = 0;
182 dp->tx_next = dp->tx_buf1;
183 dp->tx_started = false;
185 DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
186 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
187 DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
188 dp->rx_next = dp->rx_buf_start - 1;
190 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
191 DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
192 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
193 DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
195 for (i = 0; i < ETHER_ADDR_LEN; i++) {
197 /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
198 * 0x1400)) = enaddr[i];*/
199 DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
201 /* Enable and start device */
202 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
203 DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
204 DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
209 * This routine is called to start the transmitter. It is split out from the
210 * data handling routine so it may be called either when data becomes first
211 * available or when an Tx interrupt occurs
215 dp83902a_start_xmit(int start_page, int len)
217 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
223 printf("Tx pkt %d len %d\n", start_page, len);
225 printf("TX already started?!?\n");
228 DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
229 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
230 DP_OUT(base, DP_TBCL, len & 0xFF);
231 DP_OUT(base, DP_TBCH, len >> 8);
232 DP_OUT(base, DP_TPSR, start_page);
233 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
235 dp->tx_started = true;
239 * This routine is called to send data to the hardware. It is known a-priori
240 * that there is free buffer space (dp->tx_next).
243 dp83902a_send(u8 *data, int total_len, u32 key)
245 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
247 int len, start_page, pkt_len, i, isr;
254 len = pkt_len = total_len;
255 if (pkt_len < IEEE_8023_MIN_FRAME)
256 pkt_len = IEEE_8023_MIN_FRAME;
258 start_page = dp->tx_next;
259 if (dp->tx_next == dp->tx_buf1) {
260 dp->tx1 = start_page;
261 dp->tx1_len = pkt_len;
263 dp->tx_next = dp->tx_buf2;
265 dp->tx2 = start_page;
266 dp->tx2_len = pkt_len;
268 dp->tx_next = dp->tx_buf1;
272 printf("TX prep page %d len %d\n", start_page, pkt_len);
275 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
278 * Dummy read. The manual sez something slightly different,
279 * but the code is extended a bit to do what Hitachi's monitor
280 * does (i.e., also read data).
283 __maybe_unused u16 tmp;
286 DP_OUT(base, DP_RSAL, 0x100 - len);
287 DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
288 DP_OUT(base, DP_RBCL, len);
289 DP_OUT(base, DP_RBCH, 0);
290 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
291 DP_IN_DATA(dp->data, tmp);
294 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
296 * Stall for a bit before continuing to work around random data
297 * corruption problems on some platforms.
299 CYGACC_CALL_IF_DELAY_US(1);
302 /* Send data to device buffer(s) */
303 DP_OUT(base, DP_RSAL, 0);
304 DP_OUT(base, DP_RSAH, start_page);
305 DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
306 DP_OUT(base, DP_RBCH, pkt_len >> 8);
307 DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
309 /* Put data into buffer */
311 printf(" sg buf %08lx len %08x\n ", (u32)data, len);
316 printf(" %02x", *data);
317 if (0 == (++dx % 16)) printf("\n ");
320 DP_OUT_DATA(dp->data, *data++);
326 if (total_len < pkt_len) {
328 printf(" + %d bytes of padding\n", pkt_len - total_len);
330 /* Padding to 802.3 length was required */
331 for (i = total_len; i < pkt_len;) {
333 DP_OUT_DATA(dp->data, 0);
337 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
339 * After last data write, delay for a bit before accessing the
340 * device again, or we may get random data corruption in the last
341 * datum (on some platforms).
343 CYGACC_CALL_IF_DELAY_US(1);
346 /* Wait for DMA to complete */
348 DP_IN(base, DP_ISR, isr);
349 } while ((isr & DP_ISR_RDC) == 0);
351 /* Then disable DMA */
352 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
354 /* Start transmit if not already going */
355 if (!dp->tx_started) {
356 if (start_page == dp->tx1) {
357 dp->tx_int = 1; /* Expecting interrupt from BUF1 */
359 dp->tx_int = 2; /* Expecting interrupt from BUF2 */
361 dp83902a_start_xmit(start_page, pkt_len);
366 * This function is called when a packet has been received. It's job is
367 * to prepare to unload the packet from the hardware. Once the length of
368 * the packet is known, the upper layer of the driver can be told. When
369 * the upper layer is ready to unload the packet, the internal function
370 * 'dp83902a_recv' will be called to actually fetch it from the hardware.
373 dp83902a_RxEvent(void)
375 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
377 __maybe_unused u8 rsr;
379 int i, len, pkt, cur;
383 DP_IN(base, DP_RSR, rsr);
385 /* Read incoming packet header */
386 DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
387 DP_IN(base, DP_P1_CURP, cur);
388 DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
389 DP_IN(base, DP_BNDRY, pkt);
392 if (pkt == dp->rx_buf_end)
393 pkt = dp->rx_buf_start;
398 DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
399 DP_OUT(base, DP_RBCH, 0);
400 DP_OUT(base, DP_RSAL, 0);
401 DP_OUT(base, DP_RSAH, pkt);
402 if (dp->rx_next == pkt) {
403 if (cur == dp->rx_buf_start)
404 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
406 DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
410 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
411 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
412 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
413 CYGACC_CALL_IF_DELAY_US(10);
416 /* read header (get data size)*/
417 for (i = 0; i < sizeof(rcv_hdr);) {
418 DP_IN_DATA(dp->data, rcv_hdr[i++]);
422 printf("rx hdr %02x %02x %02x %02x\n",
423 rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
425 len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
428 uboot_push_packet_len(len);
430 if (rcv_hdr[1] == dp->rx_buf_start)
431 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
433 DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
438 * This function is called as a result of the "eth_drv_recv()" call above.
439 * It's job is to actually fetch data for a packet from the hardware once
440 * memory buffers have been allocated for the packet. Note that the buffers
441 * may come in pieces, using a scatter-gather list. This allows for more
442 * efficient processing in the upper layers of the stack.
445 dp83902a_recv(u8 *data, int len)
447 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
459 printf("Rx packet %d length %d\n", dp->rx_next, len);
462 /* Read incoming packet data */
463 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
464 DP_OUT(base, DP_RBCL, len & 0xFF);
465 DP_OUT(base, DP_RBCH, len >> 8);
466 DP_OUT(base, DP_RSAL, 4); /* Past header */
467 DP_OUT(base, DP_RSAH, dp->rx_next);
468 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
469 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
470 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
471 CYGACC_CALL_IF_DELAY_US(10);
475 for (i = 0; i < 1; i++) {
479 printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
483 /* Saved byte from previous loop? */
485 *data++ = saved_char;
493 DP_IN_DATA(dp->data, tmp);
495 printf(" %02x", tmp);
496 if (0 == (++dx % 16)) printf("\n ");
510 dp83902a_TxEvent(void)
512 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
514 __maybe_unused u8 tsr;
519 DP_IN(base, DP_TSR, tsr);
520 if (dp->tx_int == 1) {
527 /* Start next packet if one is ready */
528 dp->tx_started = false;
530 dp83902a_start_xmit(dp->tx1, dp->tx1_len);
532 } else if (dp->tx2) {
533 dp83902a_start_xmit(dp->tx2, dp->tx2_len);
538 /* Tell higher level we sent this packet */
539 uboot_push_tx_done(key, 0);
543 * Read the tally counters to clear them. Called in response to a CNT
547 dp83902a_ClearCounters(void)
549 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
551 __maybe_unused u8 cnt1, cnt2, cnt3;
553 DP_IN(base, DP_FER, cnt1);
554 DP_IN(base, DP_CER, cnt2);
555 DP_IN(base, DP_MISSED, cnt3);
556 DP_OUT(base, DP_ISR, DP_ISR_CNT);
560 * Deal with an overflow condition. This code follows the procedure set
561 * out in section 7.0 of the datasheet.
564 dp83902a_Overflow(void)
566 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
570 /* Issue a stop command and wait 1.6ms for it to complete. */
571 DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
572 CYGACC_CALL_IF_DELAY_US(1600);
574 /* Clear the remote byte counter registers. */
575 DP_OUT(base, DP_RBCL, 0);
576 DP_OUT(base, DP_RBCH, 0);
578 /* Enter loopback mode while we clear the buffer. */
579 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
580 DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
583 * Read in as many packets as we can and acknowledge any and receive
584 * interrupts. Since the buffer has overflowed, a receive event of
585 * some kind will have occured.
588 DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
590 /* Clear the overflow condition and leave loopback mode. */
591 DP_OUT(base, DP_ISR, DP_ISR_OFLW);
592 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
595 * If a transmit command was issued, but no transmit event has occured,
598 DP_IN(base, DP_ISR, isr);
599 if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
600 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
607 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
611 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
612 DP_IN(base, DP_ISR, isr);
615 * The CNT interrupt triggers when the MSB of one of the error
616 * counters is set. We don't much care about these counters, but
617 * we should read their values to reset them.
619 if (isr & DP_ISR_CNT) {
620 dp83902a_ClearCounters();
623 * Check for overflow. It's a special case, since there's a
624 * particular procedure that must be followed to get back into
627 if (isr & DP_ISR_OFLW) {
631 * Other kinds of interrupts can be acknowledged simply by
632 * clearing the relevant bits of the ISR. Do that now, then
633 * handle the interrupts we care about.
635 DP_OUT(base, DP_ISR, isr); /* Clear set bits */
636 if (!dp->running) break; /* Is this necessary? */
638 * Check for tx_started on TX event since these may happen
639 * spuriously it seems.
641 if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
644 if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
648 DP_IN(base, DP_ISR, isr);
653 /* U-boot specific routines */
654 static u8 *pbuf = NULL;
656 static int pkey = -1;
657 static int initialized = 0;
659 void uboot_push_packet_len(int len) {
660 PRINTK("pushed len = %d\n", len);
662 printf("NE2000: packet too big\n");
665 dp83902a_recv(&pbuf[0], len);
667 /*Just pass it to the upper layer*/
668 net_process_received_packet(&pbuf[0], len);
671 void uboot_push_tx_done(int key, int val) {
672 PRINTK("pushed key = %d\n", key);
677 * Setup the driver and init MAC address according to doc/README.enetaddr
678 * Called by ne2k_register() before registering the driver @eth layer
680 * @param struct ethdevice of this instance of the driver for dev->enetaddr
681 * @return 0 on success, -1 on error (causing caller to print error msg)
683 static int ne2k_setup_driver(struct eth_device *dev)
685 PRINTK("### ne2k_setup_driver\n");
690 printf("Cannot allocate rx buffer\n");
695 #ifdef CONFIG_DRIVER_NE2000_CCR
697 vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
699 PRINTK("CCR before is %x\n", *p);
700 *p = CONFIG_DRIVER_NE2000_VAL;
701 PRINTK("CCR after is %x\n", *p);
705 nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
707 nic.data = nic.base + DP_DATA;
708 nic.tx_buf1 = START_PG;
709 nic.tx_buf2 = START_PG2;
710 nic.rx_buf_start = RX_START;
711 nic.rx_buf_end = RX_END;
714 * According to doc/README.enetaddr, drivers shall give priority
715 * to the MAC address value in the environment, so we do not read
716 * it from the prom or eeprom if it is specified in the environment.
718 if (!eth_getenv_enetaddr("ethaddr", dev->enetaddr)) {
719 /* If the MAC address is not in the environment, get it: */
720 if (!get_prom(dev->enetaddr, nic.base)) /* get MAC from prom */
721 dp83902a_init(dev->enetaddr); /* fallback: seeprom */
722 /* And write it into the environment otherwise eth_write_hwaddr
723 * returns -1 due to eth_getenv_enetaddr_by_index() failing,
724 * and this causes "Warning: failed to set MAC address", and
725 * cmd_bdinfo has no ethaddr value which it can show: */
726 eth_setenv_enetaddr("ethaddr", dev->enetaddr);
731 static int ne2k_init(struct eth_device *dev, bd_t *bd)
733 dp83902a_start(dev->enetaddr);
738 static void ne2k_halt(struct eth_device *dev)
740 debug("### ne2k_halt\n");
746 static int ne2k_recv(struct eth_device *dev)
752 static int ne2k_send(struct eth_device *dev, void *packet, int length)
756 debug("### ne2k_send\n");
760 dp83902a_send((u8 *) packet, length, 666);
761 tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
765 PRINTK("Packet sucesfully sent\n");
768 if (get_timer (0) >= tmo) {
769 printf("transmission error (timoeut)\n");
778 * Setup the driver for use and register it with the eth layer
779 * @return 0 on success, -1 on error (causing caller to print error msg)
781 int ne2k_register(void)
783 struct eth_device *dev;
785 dev = calloc(sizeof(*dev), 1);
789 if (ne2k_setup_driver(dev))
792 dev->init = ne2k_init;
793 dev->halt = ne2k_halt;
794 dev->send = ne2k_send;
795 dev->recv = ne2k_recv;
797 sprintf(dev->name, "NE2000");
799 return eth_register(dev);