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 ==========================================================================
77 #include <environment.h>
80 #include <linux/compiler.h>
82 /* forward definition of function used for the uboot interface */
83 void uboot_push_packet_len(int len);
84 void uboot_push_tx_done(int key, int val);
86 /* NE2000 base header file */
87 #include "ne2000_base.h"
89 #if defined(CONFIG_DRIVER_AX88796L)
90 /* AX88796L support */
93 /* Basic NE2000 chip support */
97 static dp83902a_priv_data_t nic; /* just one instance of the card supported */
100 * This function reads the MAC address from the serial EEPROM,
101 * used if PROM read fails. Does nothing for ax88796 chips (sh boards)
104 dp83902a_init(unsigned char *enetaddr)
106 dp83902a_priv_data_t *dp = &nic;
108 #if defined(NE2000_BASIC_INIT)
116 return false; /* No device found */
120 #if defined(NE2000_BASIC_INIT)
121 /* AX88796L doesn't need */
123 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
124 /* Use the address from the serial EEPROM */
125 for (i = 0; i < 6; i++)
126 DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
127 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
129 printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
138 memcpy(enetaddr, dp->esa, 6); /* Use MAC from serial EEPROM */
139 #endif /* NE2000_BASIC_INIT */
146 dp83902a_priv_data_t *dp = &nic;
151 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
152 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
153 DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
159 * This function is called to "start up" the interface. It may be called
160 * multiple times, even when the hardware is already running. It will be
161 * called whenever something "hardware oriented" changes and should leave
162 * the hardware ready to send/receive packets.
165 dp83902a_start(u8 * enaddr)
167 dp83902a_priv_data_t *dp = &nic;
171 debug("The MAC is %pM\n", enaddr);
175 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
176 DP_OUT(base, DP_DCR, DP_DCR_INIT);
177 DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
178 DP_OUT(base, DP_RBCL, 0);
179 DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
180 DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
181 DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
182 dp->tx1 = dp->tx2 = 0;
183 dp->tx_next = dp->tx_buf1;
184 dp->tx_started = false;
186 DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
187 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
188 DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
189 dp->rx_next = dp->rx_buf_start - 1;
191 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
192 DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
193 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
194 DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
196 for (i = 0; i < ETHER_ADDR_LEN; i++) {
198 /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
199 * 0x1400)) = enaddr[i];*/
200 DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
202 /* Enable and start device */
203 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
204 DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
205 DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
210 * This routine is called to start the transmitter. It is split out from the
211 * data handling routine so it may be called either when data becomes first
212 * available or when an Tx interrupt occurs
216 dp83902a_start_xmit(int start_page, int len)
218 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
224 printf("Tx pkt %d len %d\n", start_page, len);
226 printf("TX already started?!?\n");
229 DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
230 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
231 DP_OUT(base, DP_TBCL, len & 0xFF);
232 DP_OUT(base, DP_TBCH, len >> 8);
233 DP_OUT(base, DP_TPSR, start_page);
234 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
236 dp->tx_started = true;
240 * This routine is called to send data to the hardware. It is known a-priori
241 * that there is free buffer space (dp->tx_next).
244 dp83902a_send(u8 *data, int total_len, u32 key)
246 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
248 int len, start_page, pkt_len, i, isr;
255 len = pkt_len = total_len;
256 if (pkt_len < IEEE_8023_MIN_FRAME)
257 pkt_len = IEEE_8023_MIN_FRAME;
259 start_page = dp->tx_next;
260 if (dp->tx_next == dp->tx_buf1) {
261 dp->tx1 = start_page;
262 dp->tx1_len = pkt_len;
264 dp->tx_next = dp->tx_buf2;
266 dp->tx2 = start_page;
267 dp->tx2_len = pkt_len;
269 dp->tx_next = dp->tx_buf1;
273 printf("TX prep page %d len %d\n", start_page, pkt_len);
276 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
279 * Dummy read. The manual sez something slightly different,
280 * but the code is extended a bit to do what Hitachi's monitor
281 * does (i.e., also read data).
284 __maybe_unused u16 tmp;
287 DP_OUT(base, DP_RSAL, 0x100 - len);
288 DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
289 DP_OUT(base, DP_RBCL, len);
290 DP_OUT(base, DP_RBCH, 0);
291 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
292 DP_IN_DATA(dp->data, tmp);
295 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
297 * Stall for a bit before continuing to work around random data
298 * corruption problems on some platforms.
300 CYGACC_CALL_IF_DELAY_US(1);
303 /* Send data to device buffer(s) */
304 DP_OUT(base, DP_RSAL, 0);
305 DP_OUT(base, DP_RSAH, start_page);
306 DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
307 DP_OUT(base, DP_RBCH, pkt_len >> 8);
308 DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
310 /* Put data into buffer */
312 printf(" sg buf %08lx len %08x\n ", (u32)data, len);
317 printf(" %02x", *data);
318 if (0 == (++dx % 16)) printf("\n ");
321 DP_OUT_DATA(dp->data, *data++);
327 if (total_len < pkt_len) {
329 printf(" + %d bytes of padding\n", pkt_len - total_len);
331 /* Padding to 802.3 length was required */
332 for (i = total_len; i < pkt_len;) {
334 DP_OUT_DATA(dp->data, 0);
338 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
340 * After last data write, delay for a bit before accessing the
341 * device again, or we may get random data corruption in the last
342 * datum (on some platforms).
344 CYGACC_CALL_IF_DELAY_US(1);
347 /* Wait for DMA to complete */
349 DP_IN(base, DP_ISR, isr);
350 } while ((isr & DP_ISR_RDC) == 0);
352 /* Then disable DMA */
353 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
355 /* Start transmit if not already going */
356 if (!dp->tx_started) {
357 if (start_page == dp->tx1) {
358 dp->tx_int = 1; /* Expecting interrupt from BUF1 */
360 dp->tx_int = 2; /* Expecting interrupt from BUF2 */
362 dp83902a_start_xmit(start_page, pkt_len);
367 * This function is called when a packet has been received. It's job is
368 * to prepare to unload the packet from the hardware. Once the length of
369 * the packet is known, the upper layer of the driver can be told. When
370 * the upper layer is ready to unload the packet, the internal function
371 * 'dp83902a_recv' will be called to actually fetch it from the hardware.
374 dp83902a_RxEvent(void)
376 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
378 __maybe_unused u8 rsr;
380 int i, len, pkt, cur;
384 DP_IN(base, DP_RSR, rsr);
386 /* Read incoming packet header */
387 DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
388 DP_IN(base, DP_P1_CURP, cur);
389 DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
390 DP_IN(base, DP_BNDRY, pkt);
393 if (pkt == dp->rx_buf_end)
394 pkt = dp->rx_buf_start;
399 DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
400 DP_OUT(base, DP_RBCH, 0);
401 DP_OUT(base, DP_RSAL, 0);
402 DP_OUT(base, DP_RSAH, pkt);
403 if (dp->rx_next == pkt) {
404 if (cur == dp->rx_buf_start)
405 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
407 DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
411 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
412 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
413 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
414 CYGACC_CALL_IF_DELAY_US(10);
417 /* read header (get data size)*/
418 for (i = 0; i < sizeof(rcv_hdr);) {
419 DP_IN_DATA(dp->data, rcv_hdr[i++]);
423 printf("rx hdr %02x %02x %02x %02x\n",
424 rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
426 len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
429 uboot_push_packet_len(len);
431 if (rcv_hdr[1] == dp->rx_buf_start)
432 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
434 DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
439 * This function is called as a result of the "eth_drv_recv()" call above.
440 * It's job is to actually fetch data for a packet from the hardware once
441 * memory buffers have been allocated for the packet. Note that the buffers
442 * may come in pieces, using a scatter-gather list. This allows for more
443 * efficient processing in the upper layers of the stack.
446 dp83902a_recv(u8 *data, int len)
448 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
460 printf("Rx packet %d length %d\n", dp->rx_next, len);
463 /* Read incoming packet data */
464 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
465 DP_OUT(base, DP_RBCL, len & 0xFF);
466 DP_OUT(base, DP_RBCH, len >> 8);
467 DP_OUT(base, DP_RSAL, 4); /* Past header */
468 DP_OUT(base, DP_RSAH, dp->rx_next);
469 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
470 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
471 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
472 CYGACC_CALL_IF_DELAY_US(10);
476 for (i = 0; i < 1; i++) {
480 printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
484 /* Saved byte from previous loop? */
486 *data++ = saved_char;
494 DP_IN_DATA(dp->data, tmp);
496 printf(" %02x", tmp);
497 if (0 == (++dx % 16)) printf("\n ");
511 dp83902a_TxEvent(void)
513 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
515 __maybe_unused u8 tsr;
520 DP_IN(base, DP_TSR, tsr);
521 if (dp->tx_int == 1) {
528 /* Start next packet if one is ready */
529 dp->tx_started = false;
531 dp83902a_start_xmit(dp->tx1, dp->tx1_len);
533 } else if (dp->tx2) {
534 dp83902a_start_xmit(dp->tx2, dp->tx2_len);
539 /* Tell higher level we sent this packet */
540 uboot_push_tx_done(key, 0);
544 * Read the tally counters to clear them. Called in response to a CNT
548 dp83902a_ClearCounters(void)
550 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
552 __maybe_unused u8 cnt1, cnt2, cnt3;
554 DP_IN(base, DP_FER, cnt1);
555 DP_IN(base, DP_CER, cnt2);
556 DP_IN(base, DP_MISSED, cnt3);
557 DP_OUT(base, DP_ISR, DP_ISR_CNT);
561 * Deal with an overflow condition. This code follows the procedure set
562 * out in section 7.0 of the datasheet.
565 dp83902a_Overflow(void)
567 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
571 /* Issue a stop command and wait 1.6ms for it to complete. */
572 DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
573 CYGACC_CALL_IF_DELAY_US(1600);
575 /* Clear the remote byte counter registers. */
576 DP_OUT(base, DP_RBCL, 0);
577 DP_OUT(base, DP_RBCH, 0);
579 /* Enter loopback mode while we clear the buffer. */
580 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
581 DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
584 * Read in as many packets as we can and acknowledge any and receive
585 * interrupts. Since the buffer has overflowed, a receive event of
586 * some kind will have occurred.
589 DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
591 /* Clear the overflow condition and leave loopback mode. */
592 DP_OUT(base, DP_ISR, DP_ISR_OFLW);
593 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
596 * If a transmit command was issued, but no transmit event has occurred,
599 DP_IN(base, DP_ISR, isr);
600 if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
601 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
608 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
612 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
613 DP_IN(base, DP_ISR, isr);
616 * The CNT interrupt triggers when the MSB of one of the error
617 * counters is set. We don't much care about these counters, but
618 * we should read their values to reset them.
620 if (isr & DP_ISR_CNT) {
621 dp83902a_ClearCounters();
624 * Check for overflow. It's a special case, since there's a
625 * particular procedure that must be followed to get back into
628 if (isr & DP_ISR_OFLW) {
632 * Other kinds of interrupts can be acknowledged simply by
633 * clearing the relevant bits of the ISR. Do that now, then
634 * handle the interrupts we care about.
636 DP_OUT(base, DP_ISR, isr); /* Clear set bits */
637 if (!dp->running) break; /* Is this necessary? */
639 * Check for tx_started on TX event since these may happen
640 * spuriously it seems.
642 if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
645 if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
649 DP_IN(base, DP_ISR, isr);
654 /* U-Boot specific routines */
655 static u8 *pbuf = NULL;
657 static int pkey = -1;
658 static int initialized = 0;
660 void uboot_push_packet_len(int len) {
661 PRINTK("pushed len = %d\n", len);
663 printf("NE2000: packet too big\n");
666 dp83902a_recv(&pbuf[0], len);
668 /*Just pass it to the upper layer*/
669 net_process_received_packet(&pbuf[0], len);
672 void uboot_push_tx_done(int key, int val) {
673 PRINTK("pushed key = %d\n", key);
678 * Setup the driver and init MAC address according to doc/README.enetaddr
679 * Called by ne2k_register() before registering the driver @eth layer
681 * @param struct ethdevice of this instance of the driver for dev->enetaddr
682 * @return 0 on success, -1 on error (causing caller to print error msg)
684 static int ne2k_setup_driver(struct eth_device *dev)
686 PRINTK("### ne2k_setup_driver\n");
691 printf("Cannot allocate rx buffer\n");
696 #ifdef CONFIG_DRIVER_NE2000_CCR
698 vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
700 PRINTK("CCR before is %x\n", *p);
701 *p = CONFIG_DRIVER_NE2000_VAL;
702 PRINTK("CCR after is %x\n", *p);
706 nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
708 nic.data = nic.base + DP_DATA;
709 nic.tx_buf1 = START_PG;
710 nic.tx_buf2 = START_PG2;
711 nic.rx_buf_start = RX_START;
712 nic.rx_buf_end = RX_END;
715 * According to doc/README.enetaddr, drivers shall give priority
716 * to the MAC address value in the environment, so we do not read
717 * it from the prom or eeprom if it is specified in the environment.
719 if (!eth_env_get_enetaddr("ethaddr", dev->enetaddr)) {
720 /* If the MAC address is not in the environment, get it: */
721 if (!get_prom(dev->enetaddr, nic.base)) /* get MAC from prom */
722 dp83902a_init(dev->enetaddr); /* fallback: seeprom */
723 /* And write it into the environment otherwise eth_write_hwaddr
724 * returns -1 due to eth_env_get_enetaddr_by_index() failing,
725 * and this causes "Warning: failed to set MAC address", and
726 * cmd_bdinfo has no ethaddr value which it can show: */
727 eth_env_set_enetaddr("ethaddr", dev->enetaddr);
732 static int ne2k_init(struct eth_device *dev, bd_t *bd)
734 dp83902a_start(dev->enetaddr);
739 static void ne2k_halt(struct eth_device *dev)
741 debug("### ne2k_halt\n");
747 static int ne2k_recv(struct eth_device *dev)
753 static int ne2k_send(struct eth_device *dev, void *packet, int length)
757 debug("### ne2k_send\n");
761 dp83902a_send((u8 *) packet, length, 666);
762 tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
766 PRINTK("Packet sucesfully sent\n");
769 if (get_timer (0) >= tmo) {
770 printf("transmission error (timoeut)\n");
779 * Setup the driver for use and register it with the eth layer
780 * @return 0 on success, -1 on error (causing caller to print error msg)
782 int ne2k_register(void)
784 struct eth_device *dev;
786 dev = calloc(sizeof(*dev), 1);
790 if (ne2k_setup_driver(dev))
793 dev->init = ne2k_init;
794 dev->halt = ne2k_halt;
795 dev->send = ne2k_send;
796 dev->recv = ne2k_recv;
798 strcpy(dev->name, "NE2000");
800 return eth_register(dev);