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 ==========================================================================
78 #include <environment.h>
81 #include <linux/compiler.h>
83 /* forward definition of function used for the uboot interface */
84 void uboot_push_packet_len(int len);
85 void uboot_push_tx_done(int key, int val);
87 /* NE2000 base header file */
88 #include "ne2000_base.h"
90 #if defined(CONFIG_DRIVER_AX88796L)
91 /* AX88796L support */
94 /* Basic NE2000 chip support */
98 static dp83902a_priv_data_t nic; /* just one instance of the card supported */
101 * This function reads the MAC address from the serial EEPROM,
102 * used if PROM read fails. Does nothing for ax88796 chips (sh boards)
105 dp83902a_init(unsigned char *enetaddr)
107 dp83902a_priv_data_t *dp = &nic;
109 #if defined(NE2000_BASIC_INIT)
117 return false; /* No device found */
121 #if defined(NE2000_BASIC_INIT)
122 /* AX88796L doesn't need */
124 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
125 /* Use the address from the serial EEPROM */
126 for (i = 0; i < 6; i++)
127 DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
128 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
130 printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
139 memcpy(enetaddr, dp->esa, 6); /* Use MAC from serial EEPROM */
140 #endif /* NE2000_BASIC_INIT */
147 dp83902a_priv_data_t *dp = &nic;
152 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
153 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
154 DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
160 * This function is called to "start up" the interface. It may be called
161 * multiple times, even when the hardware is already running. It will be
162 * called whenever something "hardware oriented" changes and should leave
163 * the hardware ready to send/receive packets.
166 dp83902a_start(u8 * enaddr)
168 dp83902a_priv_data_t *dp = &nic;
172 debug("The MAC is %pM\n", enaddr);
176 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
177 DP_OUT(base, DP_DCR, DP_DCR_INIT);
178 DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
179 DP_OUT(base, DP_RBCL, 0);
180 DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
181 DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
182 DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
183 dp->tx1 = dp->tx2 = 0;
184 dp->tx_next = dp->tx_buf1;
185 dp->tx_started = false;
187 DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
188 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
189 DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
190 dp->rx_next = dp->rx_buf_start - 1;
192 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
193 DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
194 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
195 DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
197 for (i = 0; i < ETHER_ADDR_LEN; i++) {
199 /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
200 * 0x1400)) = enaddr[i];*/
201 DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
203 /* Enable and start device */
204 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
205 DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
206 DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
211 * This routine is called to start the transmitter. It is split out from the
212 * data handling routine so it may be called either when data becomes first
213 * available or when an Tx interrupt occurs
217 dp83902a_start_xmit(int start_page, int len)
219 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
225 printf("Tx pkt %d len %d\n", start_page, len);
227 printf("TX already started?!?\n");
230 DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
231 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
232 DP_OUT(base, DP_TBCL, len & 0xFF);
233 DP_OUT(base, DP_TBCH, len >> 8);
234 DP_OUT(base, DP_TPSR, start_page);
235 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
237 dp->tx_started = true;
241 * This routine is called to send data to the hardware. It is known a-priori
242 * that there is free buffer space (dp->tx_next).
245 dp83902a_send(u8 *data, int total_len, u32 key)
247 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
249 int len, start_page, pkt_len, i, isr;
256 len = pkt_len = total_len;
257 if (pkt_len < IEEE_8023_MIN_FRAME)
258 pkt_len = IEEE_8023_MIN_FRAME;
260 start_page = dp->tx_next;
261 if (dp->tx_next == dp->tx_buf1) {
262 dp->tx1 = start_page;
263 dp->tx1_len = pkt_len;
265 dp->tx_next = dp->tx_buf2;
267 dp->tx2 = start_page;
268 dp->tx2_len = pkt_len;
270 dp->tx_next = dp->tx_buf1;
274 printf("TX prep page %d len %d\n", start_page, pkt_len);
277 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
280 * Dummy read. The manual sez something slightly different,
281 * but the code is extended a bit to do what Hitachi's monitor
282 * does (i.e., also read data).
285 __maybe_unused u16 tmp;
288 DP_OUT(base, DP_RSAL, 0x100 - len);
289 DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
290 DP_OUT(base, DP_RBCL, len);
291 DP_OUT(base, DP_RBCH, 0);
292 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
293 DP_IN_DATA(dp->data, tmp);
296 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
298 * Stall for a bit before continuing to work around random data
299 * corruption problems on some platforms.
301 CYGACC_CALL_IF_DELAY_US(1);
304 /* Send data to device buffer(s) */
305 DP_OUT(base, DP_RSAL, 0);
306 DP_OUT(base, DP_RSAH, start_page);
307 DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
308 DP_OUT(base, DP_RBCH, pkt_len >> 8);
309 DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
311 /* Put data into buffer */
313 printf(" sg buf %08lx len %08x\n ", (u32)data, len);
318 printf(" %02x", *data);
319 if (0 == (++dx % 16)) printf("\n ");
322 DP_OUT_DATA(dp->data, *data++);
328 if (total_len < pkt_len) {
330 printf(" + %d bytes of padding\n", pkt_len - total_len);
332 /* Padding to 802.3 length was required */
333 for (i = total_len; i < pkt_len;) {
335 DP_OUT_DATA(dp->data, 0);
339 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
341 * After last data write, delay for a bit before accessing the
342 * device again, or we may get random data corruption in the last
343 * datum (on some platforms).
345 CYGACC_CALL_IF_DELAY_US(1);
348 /* Wait for DMA to complete */
350 DP_IN(base, DP_ISR, isr);
351 } while ((isr & DP_ISR_RDC) == 0);
353 /* Then disable DMA */
354 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
356 /* Start transmit if not already going */
357 if (!dp->tx_started) {
358 if (start_page == dp->tx1) {
359 dp->tx_int = 1; /* Expecting interrupt from BUF1 */
361 dp->tx_int = 2; /* Expecting interrupt from BUF2 */
363 dp83902a_start_xmit(start_page, pkt_len);
368 * This function is called when a packet has been received. It's job is
369 * to prepare to unload the packet from the hardware. Once the length of
370 * the packet is known, the upper layer of the driver can be told. When
371 * the upper layer is ready to unload the packet, the internal function
372 * 'dp83902a_recv' will be called to actually fetch it from the hardware.
375 dp83902a_RxEvent(void)
377 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
379 __maybe_unused u8 rsr;
381 int i, len, pkt, cur;
385 DP_IN(base, DP_RSR, rsr);
387 /* Read incoming packet header */
388 DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
389 DP_IN(base, DP_P1_CURP, cur);
390 DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
391 DP_IN(base, DP_BNDRY, pkt);
394 if (pkt == dp->rx_buf_end)
395 pkt = dp->rx_buf_start;
400 DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
401 DP_OUT(base, DP_RBCH, 0);
402 DP_OUT(base, DP_RSAL, 0);
403 DP_OUT(base, DP_RSAH, pkt);
404 if (dp->rx_next == pkt) {
405 if (cur == dp->rx_buf_start)
406 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
408 DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
412 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
413 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
414 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
415 CYGACC_CALL_IF_DELAY_US(10);
418 /* read header (get data size)*/
419 for (i = 0; i < sizeof(rcv_hdr);) {
420 DP_IN_DATA(dp->data, rcv_hdr[i++]);
424 printf("rx hdr %02x %02x %02x %02x\n",
425 rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
427 len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
430 uboot_push_packet_len(len);
432 if (rcv_hdr[1] == dp->rx_buf_start)
433 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
435 DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
440 * This function is called as a result of the "eth_drv_recv()" call above.
441 * It's job is to actually fetch data for a packet from the hardware once
442 * memory buffers have been allocated for the packet. Note that the buffers
443 * may come in pieces, using a scatter-gather list. This allows for more
444 * efficient processing in the upper layers of the stack.
447 dp83902a_recv(u8 *data, int len)
449 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
461 printf("Rx packet %d length %d\n", dp->rx_next, len);
464 /* Read incoming packet data */
465 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
466 DP_OUT(base, DP_RBCL, len & 0xFF);
467 DP_OUT(base, DP_RBCH, len >> 8);
468 DP_OUT(base, DP_RSAL, 4); /* Past header */
469 DP_OUT(base, DP_RSAH, dp->rx_next);
470 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
471 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
472 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
473 CYGACC_CALL_IF_DELAY_US(10);
477 for (i = 0; i < 1; i++) {
481 printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
485 /* Saved byte from previous loop? */
487 *data++ = saved_char;
495 DP_IN_DATA(dp->data, tmp);
497 printf(" %02x", tmp);
498 if (0 == (++dx % 16)) printf("\n ");
512 dp83902a_TxEvent(void)
514 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
516 __maybe_unused u8 tsr;
521 DP_IN(base, DP_TSR, tsr);
522 if (dp->tx_int == 1) {
529 /* Start next packet if one is ready */
530 dp->tx_started = false;
532 dp83902a_start_xmit(dp->tx1, dp->tx1_len);
534 } else if (dp->tx2) {
535 dp83902a_start_xmit(dp->tx2, dp->tx2_len);
540 /* Tell higher level we sent this packet */
541 uboot_push_tx_done(key, 0);
545 * Read the tally counters to clear them. Called in response to a CNT
549 dp83902a_ClearCounters(void)
551 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
553 __maybe_unused u8 cnt1, cnt2, cnt3;
555 DP_IN(base, DP_FER, cnt1);
556 DP_IN(base, DP_CER, cnt2);
557 DP_IN(base, DP_MISSED, cnt3);
558 DP_OUT(base, DP_ISR, DP_ISR_CNT);
562 * Deal with an overflow condition. This code follows the procedure set
563 * out in section 7.0 of the datasheet.
566 dp83902a_Overflow(void)
568 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
572 /* Issue a stop command and wait 1.6ms for it to complete. */
573 DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
574 CYGACC_CALL_IF_DELAY_US(1600);
576 /* Clear the remote byte counter registers. */
577 DP_OUT(base, DP_RBCL, 0);
578 DP_OUT(base, DP_RBCH, 0);
580 /* Enter loopback mode while we clear the buffer. */
581 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
582 DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
585 * Read in as many packets as we can and acknowledge any and receive
586 * interrupts. Since the buffer has overflowed, a receive event of
587 * some kind will have occurred.
590 DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
592 /* Clear the overflow condition and leave loopback mode. */
593 DP_OUT(base, DP_ISR, DP_ISR_OFLW);
594 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
597 * If a transmit command was issued, but no transmit event has occurred,
600 DP_IN(base, DP_ISR, isr);
601 if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
602 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
609 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
613 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
614 DP_IN(base, DP_ISR, isr);
617 * The CNT interrupt triggers when the MSB of one of the error
618 * counters is set. We don't much care about these counters, but
619 * we should read their values to reset them.
621 if (isr & DP_ISR_CNT) {
622 dp83902a_ClearCounters();
625 * Check for overflow. It's a special case, since there's a
626 * particular procedure that must be followed to get back into
629 if (isr & DP_ISR_OFLW) {
633 * Other kinds of interrupts can be acknowledged simply by
634 * clearing the relevant bits of the ISR. Do that now, then
635 * handle the interrupts we care about.
637 DP_OUT(base, DP_ISR, isr); /* Clear set bits */
638 if (!dp->running) break; /* Is this necessary? */
640 * Check for tx_started on TX event since these may happen
641 * spuriously it seems.
643 if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
646 if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
650 DP_IN(base, DP_ISR, isr);
655 /* U-Boot specific routines */
656 static u8 *pbuf = NULL;
658 static int pkey = -1;
659 static int initialized = 0;
661 void uboot_push_packet_len(int len) {
662 PRINTK("pushed len = %d\n", len);
664 printf("NE2000: packet too big\n");
667 dp83902a_recv(&pbuf[0], len);
669 /*Just pass it to the upper layer*/
670 net_process_received_packet(&pbuf[0], len);
673 void uboot_push_tx_done(int key, int val) {
674 PRINTK("pushed key = %d\n", key);
679 * Setup the driver and init MAC address according to doc/README.enetaddr
680 * Called by ne2k_register() before registering the driver @eth layer
682 * @param struct ethdevice of this instance of the driver for dev->enetaddr
683 * @return 0 on success, -1 on error (causing caller to print error msg)
685 static int ne2k_setup_driver(struct eth_device *dev)
687 PRINTK("### ne2k_setup_driver\n");
692 printf("Cannot allocate rx buffer\n");
697 #ifdef CONFIG_DRIVER_NE2000_CCR
699 vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
701 PRINTK("CCR before is %x\n", *p);
702 *p = CONFIG_DRIVER_NE2000_VAL;
703 PRINTK("CCR after is %x\n", *p);
707 nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
709 nic.data = nic.base + DP_DATA;
710 nic.tx_buf1 = START_PG;
711 nic.tx_buf2 = START_PG2;
712 nic.rx_buf_start = RX_START;
713 nic.rx_buf_end = RX_END;
716 * According to doc/README.enetaddr, drivers shall give priority
717 * to the MAC address value in the environment, so we do not read
718 * it from the prom or eeprom if it is specified in the environment.
720 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) {
721 /* If the MAC address is not in the environment, get it: */
722 if (!get_prom(dev->enetaddr, nic.base)) /* get MAC from prom */
723 dp83902a_init(dev->enetaddr); /* fallback: seeprom */
724 /* And write it into the environment otherwise eth_write_hwaddr
725 * returns -1 due to eth_env_get_enetaddr_by_index() failing,
726 * and this causes "Warning: failed to set MAC address", and
727 * cmd_bdinfo has no ethaddr value which it can show: */
728 eth_env_set_enetaddr("ethaddr", dev->enetaddr);
733 static int ne2k_init(struct eth_device *dev, bd_t *bd)
735 dp83902a_start(dev->enetaddr);
740 static void ne2k_halt(struct eth_device *dev)
742 debug("### ne2k_halt\n");
748 static int ne2k_recv(struct eth_device *dev)
754 static int ne2k_send(struct eth_device *dev, void *packet, int length)
758 debug("### ne2k_send\n");
762 dp83902a_send((u8 *) packet, length, 666);
763 tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
767 PRINTK("Packet sucesfully sent\n");
770 if (get_timer (0) >= tmo) {
771 printf("transmission error (timoeut)\n");
780 * Setup the driver for use and register it with the eth layer
781 * @return 0 on success, -1 on error (causing caller to print error msg)
783 int ne2k_register(void)
785 struct eth_device *dev;
787 dev = calloc(sizeof(*dev), 1);
791 if (ne2k_setup_driver(dev))
794 dev->init = ne2k_init;
795 dev->halt = ne2k_halt;
796 dev->send = ne2k_send;
797 dev->recv = ne2k_recv;
799 strcpy(dev->name, "NE2000");
801 return eth_register(dev);