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 ==========================================================================
80 #define mdelay(n) udelay((n)*1000)
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 */
101 dp83902a_priv_data_t *dp = &nic;
103 #if defined(NE2000_BASIC_INIT)
111 return false; /* No device found */
115 #if defined(NE2000_BASIC_INIT)
116 /* AX88796L doesn't need */
118 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
119 /* Use the address from the serial EEPROM */
120 for (i = 0; i < 6; i++)
121 DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
122 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
124 printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
133 #endif /* NE2000_BASIC_INIT */
140 dp83902a_priv_data_t *dp = &nic;
145 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
146 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
147 DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
153 * This function is called to "start up" the interface. It may be called
154 * multiple times, even when the hardware is already running. It will be
155 * called whenever something "hardware oriented" changes and should leave
156 * the hardware ready to send/receive packets.
159 dp83902a_start(u8 * enaddr)
161 dp83902a_priv_data_t *dp = &nic;
167 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
168 DP_OUT(base, DP_DCR, DP_DCR_INIT);
169 DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
170 DP_OUT(base, DP_RBCL, 0);
171 DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
172 DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
173 DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
174 dp->tx1 = dp->tx2 = 0;
175 dp->tx_next = dp->tx_buf1;
176 dp->tx_started = false;
178 DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
179 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
180 DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
181 dp->rx_next = dp->rx_buf_start - 1;
183 DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
184 DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
185 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
186 DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
188 for (i = 0; i < ETHER_ADDR_LEN; i++) {
190 /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
191 * 0x1400)) = enaddr[i];*/
192 DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
194 /* Enable and start device */
195 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
196 DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
197 DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
202 * This routine is called to start the transmitter. It is split out from the
203 * data handling routine so it may be called either when data becomes first
204 * available or when an Tx interrupt occurs
208 dp83902a_start_xmit(int start_page, int len)
210 dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
216 printf("Tx pkt %d len %d\n", start_page, len);
218 printf("TX already started?!?\n");
221 DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
222 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
223 DP_OUT(base, DP_TBCL, len & 0xFF);
224 DP_OUT(base, DP_TBCH, len >> 8);
225 DP_OUT(base, DP_TPSR, start_page);
226 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
228 dp->tx_started = true;
232 * This routine is called to send data to the hardware. It is known a-priori
233 * that there is free buffer space (dp->tx_next).
236 dp83902a_send(u8 *data, int total_len, u32 key)
238 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
240 int len, start_page, pkt_len, i, isr;
247 len = pkt_len = total_len;
248 if (pkt_len < IEEE_8023_MIN_FRAME)
249 pkt_len = IEEE_8023_MIN_FRAME;
251 start_page = dp->tx_next;
252 if (dp->tx_next == dp->tx_buf1) {
253 dp->tx1 = start_page;
254 dp->tx1_len = pkt_len;
256 dp->tx_next = dp->tx_buf2;
258 dp->tx2 = start_page;
259 dp->tx2_len = pkt_len;
261 dp->tx_next = dp->tx_buf1;
265 printf("TX prep page %d len %d\n", start_page, pkt_len);
268 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
271 * Dummy read. The manual sez something slightly different,
272 * but the code is extended a bit to do what Hitachi's monitor
273 * does (i.e., also read data).
279 DP_OUT(base, DP_RSAL, 0x100 - len);
280 DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
281 DP_OUT(base, DP_RBCL, len);
282 DP_OUT(base, DP_RBCH, 0);
283 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
284 DP_IN_DATA(dp->data, tmp);
287 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
289 * Stall for a bit before continuing to work around random data
290 * corruption problems on some platforms.
292 CYGACC_CALL_IF_DELAY_US(1);
295 /* Send data to device buffer(s) */
296 DP_OUT(base, DP_RSAL, 0);
297 DP_OUT(base, DP_RSAH, start_page);
298 DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
299 DP_OUT(base, DP_RBCH, pkt_len >> 8);
300 DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
302 /* Put data into buffer */
304 printf(" sg buf %08lx len %08x\n ", (u32)data, len);
309 printf(" %02x", *data);
310 if (0 == (++dx % 16)) printf("\n ");
313 DP_OUT_DATA(dp->data, *data++);
319 if (total_len < pkt_len) {
321 printf(" + %d bytes of padding\n", pkt_len - total_len);
323 /* Padding to 802.3 length was required */
324 for (i = total_len; i < pkt_len;) {
326 DP_OUT_DATA(dp->data, 0);
330 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
332 * After last data write, delay for a bit before accessing the
333 * device again, or we may get random data corruption in the last
334 * datum (on some platforms).
336 CYGACC_CALL_IF_DELAY_US(1);
339 /* Wait for DMA to complete */
341 DP_IN(base, DP_ISR, isr);
342 } while ((isr & DP_ISR_RDC) == 0);
344 /* Then disable DMA */
345 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
347 /* Start transmit if not already going */
348 if (!dp->tx_started) {
349 if (start_page == dp->tx1) {
350 dp->tx_int = 1; /* Expecting interrupt from BUF1 */
352 dp->tx_int = 2; /* Expecting interrupt from BUF2 */
354 dp83902a_start_xmit(start_page, pkt_len);
359 * This function is called when a packet has been received. It's job is
360 * to prepare to unload the packet from the hardware. Once the length of
361 * the packet is known, the upper layer of the driver can be told. When
362 * the upper layer is ready to unload the packet, the internal function
363 * 'dp83902a_recv' will be called to actually fetch it from the hardware.
366 dp83902a_RxEvent(void)
368 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
372 int i, len, pkt, cur;
376 DP_IN(base, DP_RSR, rsr);
378 /* Read incoming packet header */
379 DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
380 DP_IN(base, DP_P1_CURP, cur);
381 DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
382 DP_IN(base, DP_BNDRY, pkt);
385 if (pkt == dp->rx_buf_end)
386 pkt = dp->rx_buf_start;
391 DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
392 DP_OUT(base, DP_RBCH, 0);
393 DP_OUT(base, DP_RSAL, 0);
394 DP_OUT(base, DP_RSAH, pkt);
395 if (dp->rx_next == pkt) {
396 if (cur == dp->rx_buf_start)
397 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
399 DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
403 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
404 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
405 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
406 CYGACC_CALL_IF_DELAY_US(10);
409 /* read header (get data size)*/
410 for (i = 0; i < sizeof(rcv_hdr);) {
411 DP_IN_DATA(dp->data, rcv_hdr[i++]);
415 printf("rx hdr %02x %02x %02x %02x\n",
416 rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
418 len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
421 uboot_push_packet_len(len);
423 if (rcv_hdr[1] == dp->rx_buf_start)
424 DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
426 DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
431 * This function is called as a result of the "eth_drv_recv()" call above.
432 * It's job is to actually fetch data for a packet from the hardware once
433 * memory buffers have been allocated for the packet. Note that the buffers
434 * may come in pieces, using a scatter-gather list. This allows for more
435 * efficient processing in the upper layers of the stack.
438 dp83902a_recv(u8 *data, int len)
440 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
452 printf("Rx packet %d length %d\n", dp->rx_next, len);
455 /* Read incoming packet data */
456 DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
457 DP_OUT(base, DP_RBCL, len & 0xFF);
458 DP_OUT(base, DP_RBCH, len >> 8);
459 DP_OUT(base, DP_RSAL, 4); /* Past header */
460 DP_OUT(base, DP_RSAH, dp->rx_next);
461 DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
462 DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
463 #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
464 CYGACC_CALL_IF_DELAY_US(10);
468 for (i = 0; i < 1; i++) {
472 printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
476 /* Saved byte from previous loop? */
478 *data++ = saved_char;
486 DP_IN_DATA(dp->data, tmp);
488 printf(" %02x", tmp);
489 if (0 == (++dx % 16)) printf("\n ");
503 dp83902a_TxEvent(void)
505 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
512 DP_IN(base, DP_TSR, tsr);
513 if (dp->tx_int == 1) {
520 /* Start next packet if one is ready */
521 dp->tx_started = false;
523 dp83902a_start_xmit(dp->tx1, dp->tx1_len);
525 } else if (dp->tx2) {
526 dp83902a_start_xmit(dp->tx2, dp->tx2_len);
531 /* Tell higher level we sent this packet */
532 uboot_push_tx_done(key, 0);
536 * Read the tally counters to clear them. Called in response to a CNT
540 dp83902a_ClearCounters(void)
542 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
546 DP_IN(base, DP_FER, cnt1);
547 DP_IN(base, DP_CER, cnt2);
548 DP_IN(base, DP_MISSED, cnt3);
549 DP_OUT(base, DP_ISR, DP_ISR_CNT);
553 * Deal with an overflow condition. This code follows the procedure set
554 * out in section 7.0 of the datasheet.
557 dp83902a_Overflow(void)
559 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
563 /* Issue a stop command and wait 1.6ms for it to complete. */
564 DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
565 CYGACC_CALL_IF_DELAY_US(1600);
567 /* Clear the remote byte counter registers. */
568 DP_OUT(base, DP_RBCL, 0);
569 DP_OUT(base, DP_RBCH, 0);
571 /* Enter loopback mode while we clear the buffer. */
572 DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
573 DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
576 * Read in as many packets as we can and acknowledge any and receive
577 * interrupts. Since the buffer has overflowed, a receive event of
578 * some kind will have occured.
581 DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
583 /* Clear the overflow condition and leave loopback mode. */
584 DP_OUT(base, DP_ISR, DP_ISR_OFLW);
585 DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
588 * If a transmit command was issued, but no transmit event has occured,
591 DP_IN(base, DP_ISR, isr);
592 if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
593 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
600 struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
604 DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
605 DP_IN(base, DP_ISR, isr);
608 * The CNT interrupt triggers when the MSB of one of the error
609 * counters is set. We don't much care about these counters, but
610 * we should read their values to reset them.
612 if (isr & DP_ISR_CNT) {
613 dp83902a_ClearCounters();
616 * Check for overflow. It's a special case, since there's a
617 * particular procedure that must be followed to get back into
620 if (isr & DP_ISR_OFLW) {
624 * Other kinds of interrupts can be acknowledged simply by
625 * clearing the relevant bits of the ISR. Do that now, then
626 * handle the interrupts we care about.
628 DP_OUT(base, DP_ISR, isr); /* Clear set bits */
629 if (!dp->running) break; /* Is this necessary? */
631 * Check for tx_started on TX event since these may happen
632 * spuriously it seems.
634 if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
637 if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
641 DP_IN(base, DP_ISR, isr);
646 /* U-boot specific routines */
647 static u8 *pbuf = NULL;
649 static int pkey = -1;
650 static int initialized = 0;
652 void uboot_push_packet_len(int len) {
653 PRINTK("pushed len = %d\n", len);
655 printf("NE2000: packet too big\n");
658 dp83902a_recv(&pbuf[0], len);
660 /*Just pass it to the upper layer*/
661 NetReceive(&pbuf[0], len);
664 void uboot_push_tx_done(int key, int val) {
665 PRINTK("pushed key = %d\n", key);
669 int eth_init(bd_t *bd) {
674 PRINTK("### eth_init\n");
679 printf("Cannot allocate rx buffer\n");
684 #ifdef CONFIG_DRIVER_NE2000_CCR
686 vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
688 PRINTK("CCR before is %x\n", *p);
689 *p = CONFIG_DRIVER_NE2000_VAL;
690 PRINTK("CCR after is %x\n", *p);
694 nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
696 r = get_prom(dev_addr, nic.base);
700 sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
701 dev_addr[0], dev_addr[1],
702 dev_addr[2], dev_addr[3],
703 dev_addr[4], dev_addr[5]) ;
704 PRINTK("Set environment from HW MAC addr = \"%s\"\n", ethaddr);
705 setenv ("ethaddr", ethaddr);
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;
713 if (dp83902a_init() == false)
716 dp83902a_start(dev_addr);
724 PRINTK("### eth_halt\n");
735 int eth_send(volatile void *packet, int length) {
738 PRINTK("### eth_send\n");
742 dp83902a_send((u8 *) packet, length, 666);
743 tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
747 PRINTK("Packet sucesfully sent\n");
750 if (get_timer (0) >= tmo) {
751 printf("transmission error (timoeut)\n");