3 * Reinhard Meyer, EMK Elektronik, reinhard.meyer@emk-elektronik.de
4 * Martin Krause, Martin.Krause@tqs.de
5 * reworked original enc28j60.c
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * IMPORTANT: spi_claim_bus() and spi_release_bus()
33 * are called at begin and end of each of the following functions:
34 * enc_miiphy_read(), enc_miiphy_write(), enc_write_hwaddr(),
35 * enc_init(), enc_recv(), enc_send(), enc_halt()
36 * ALL other functions assume that the bus has already been claimed!
37 * Since NetReceive() might call enc_send() in return, the bus must be
38 * released, NetReceive() called and claimed again.
42 * Controller memory layout.
43 * We only allow 1 frame for transmission and reserve the rest
44 * for reception to handle as many broadcast packets as possible.
45 * Also use the memory from 0x0000 for receiver buffer. See errata pt. 5
46 * 0x0000 - 0x19ff 6656 bytes receive buffer
47 * 0x1a00 - 0x1fff 1536 bytes transmit buffer =
48 * control(1)+frame(1518)+status(7)+reserve(10).
50 #define ENC_RX_BUF_START 0x0000
51 #define ENC_RX_BUF_END 0x19ff
52 #define ENC_TX_BUF_START 0x1a00
53 #define ENC_TX_BUF_END 0x1fff
54 #define ENC_MAX_FRM_LEN 1518
55 #define RX_RESET_COUNTER 1000
58 * For non data transfer functions, like phy read/write, set hwaddr, init
59 * we do not need a full, time consuming init including link ready wait.
60 * This enum helps to bring the chip through the minimum necessary inits.
62 enum enc_initstate {none=0, setupdone, linkready};
63 typedef struct enc_device {
64 struct eth_device *dev; /* back pointer */
65 struct spi_slave *slave;
68 u8 bank; /* current bank in enc28j60 */
69 enum enc_initstate initstate;
73 * enc_bset: set bits in a common register
74 * enc_bclr: clear bits in a common register
76 * making the reg parameter u8 will give a compile time warning if the
77 * functions are called with a register not accessible in all Banks
79 static void enc_bset(enc_dev_t *enc, const u8 reg, const u8 data)
83 dout[0] = CMD_BFS(reg);
85 spi_xfer(enc->slave, 2 * 8, dout, NULL,
86 SPI_XFER_BEGIN | SPI_XFER_END);
89 static void enc_bclr(enc_dev_t *enc, const u8 reg, const u8 data)
93 dout[0] = CMD_BFC(reg);
95 spi_xfer(enc->slave, 2 * 8, dout, NULL,
96 SPI_XFER_BEGIN | SPI_XFER_END);
100 * high byte of the register contains bank number:
101 * 0: no bank switch necessary
102 * 1: switch to bank 0
103 * 2: switch to bank 1
104 * 3: switch to bank 2
105 * 4: switch to bank 3
107 static void enc_set_bank(enc_dev_t *enc, const u16 reg)
109 u8 newbank = reg >> 8;
111 if (newbank == 0 || newbank == enc->bank)
115 enc_bclr(enc, CTL_REG_ECON1,
116 ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1);
119 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_BSEL0);
120 enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_BSEL1);
123 enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_BSEL0);
124 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_BSEL1);
127 enc_bset(enc, CTL_REG_ECON1,
128 ENC_ECON1_BSEL0 | ENC_ECON1_BSEL1);
135 * local functions to access SPI
137 * reg: register inside ENC28J60
138 * data: 8/16 bits to write
139 * c: number of retries
141 * enc_r8: read 8 bits
142 * enc_r16: read 16 bits
143 * enc_w8: write 8 bits
144 * enc_w16: write 16 bits
145 * enc_w8_retry: write 8 bits, verify and retry
146 * enc_rbuf: read from ENC28J60 into buffer
147 * enc_wbuf: write from buffer into ENC28J60
151 * MAC and MII registers need a 3 byte SPI transfer to read,
152 * all other registers need a 2 byte SPI transfer.
154 static int enc_reg2nbytes(const u16 reg)
156 /* check if MAC or MII register */
157 return ((reg >= CTL_REG_MACON1 && reg <= CTL_REG_MIRDH) ||
158 (reg >= CTL_REG_MAADR1 && reg <= CTL_REG_MAADR4) ||
159 (reg == CTL_REG_MISTAT)) ? 3 : 2;
163 * Read a byte register
165 static u8 enc_r8(enc_dev_t *enc, const u16 reg)
169 int nbytes = enc_reg2nbytes(reg);
171 enc_set_bank(enc, reg);
172 dout[0] = CMD_RCR(reg);
173 spi_xfer(enc->slave, nbytes * 8, dout, din,
174 SPI_XFER_BEGIN | SPI_XFER_END);
175 return din[nbytes-1];
179 * Read a L/H register pair and return a word.
180 * Must be called with the L register's address.
182 static u16 enc_r16(enc_dev_t *enc, const u16 reg)
187 int nbytes = enc_reg2nbytes(reg);
189 enc_set_bank(enc, reg);
190 dout[0] = CMD_RCR(reg);
191 spi_xfer(enc->slave, nbytes * 8, dout, din,
192 SPI_XFER_BEGIN | SPI_XFER_END);
193 result = din[nbytes-1];
194 dout[0]++; /* next register */
195 spi_xfer(enc->slave, nbytes * 8, dout, din,
196 SPI_XFER_BEGIN | SPI_XFER_END);
197 result |= din[nbytes-1] << 8;
202 * Write a byte register
204 static void enc_w8(enc_dev_t *enc, const u16 reg, const u8 data)
208 enc_set_bank(enc, reg);
209 dout[0] = CMD_WCR(reg);
211 spi_xfer(enc->slave, 2 * 8, dout, NULL,
212 SPI_XFER_BEGIN | SPI_XFER_END);
216 * Write a L/H register pair.
217 * Must be called with the L register's address.
219 static void enc_w16(enc_dev_t *enc, const u16 reg, const u16 data)
223 enc_set_bank(enc, reg);
224 dout[0] = CMD_WCR(reg);
226 spi_xfer(enc->slave, 2 * 8, dout, NULL,
227 SPI_XFER_BEGIN | SPI_XFER_END);
228 dout[0]++; /* next register */
230 spi_xfer(enc->slave, 2 * 8, dout, NULL,
231 SPI_XFER_BEGIN | SPI_XFER_END);
235 * Write a byte register, verify and retry
237 static void enc_w8_retry(enc_dev_t *enc, const u16 reg, const u8 data, const int c)
243 enc_set_bank(enc, reg);
244 for (i = 0; i < c; i++) {
245 dout[0] = CMD_WCR(reg);
247 spi_xfer(enc->slave, 2 * 8, dout, NULL,
248 SPI_XFER_BEGIN | SPI_XFER_END);
249 readback = enc_r8(enc, reg);
250 if (readback == data)
256 printf("%s: write reg 0x%03x failed\n", enc->dev->name, reg);
261 * Read ENC RAM into buffer
263 static void enc_rbuf(enc_dev_t *enc, const u16 length, u8 *buf)
268 spi_xfer(enc->slave, 8, dout, NULL, SPI_XFER_BEGIN);
269 spi_xfer(enc->slave, length * 8, NULL, buf, SPI_XFER_END);
272 print_buffer(0, buf, 1, length, 0);
277 * Write buffer into ENC RAM
279 static void enc_wbuf(enc_dev_t *enc, const u16 length, const u8 *buf, const u8 control)
284 spi_xfer(enc->slave, 2 * 8, dout, NULL, SPI_XFER_BEGIN);
285 spi_xfer(enc->slave, length * 8, buf, NULL, SPI_XFER_END);
288 print_buffer(0, buf, 1, length, 0);
293 * Try to claim the SPI bus.
294 * Print error message on failure.
296 static int enc_claim_bus(enc_dev_t *enc)
298 int rc = spi_claim_bus(enc->slave);
300 printf("%s: failed to claim SPI bus\n", enc->dev->name);
305 * Release previously claimed SPI bus.
306 * This function is mainly for symmetry to enc_claim_bus().
307 * Let the toolchain decide to inline it...
309 static void enc_release_bus(enc_dev_t *enc)
311 spi_release_bus(enc->slave);
317 static u16 enc_phy_read(enc_dev_t *enc, const u8 addr)
322 enc_w8(enc, CTL_REG_MIREGADR, addr);
323 enc_w8(enc, CTL_REG_MICMD, ENC_MICMD_MIIRD);
324 /* 1 second timeout - only happens on hardware problem */
325 etime = get_ticks() + get_tbclk();
326 /* poll MISTAT.BUSY bit until operation is complete */
329 status = enc_r8(enc, CTL_REG_MISTAT);
330 } while (get_ticks() <= etime && (status & ENC_MISTAT_BUSY));
331 if (status & ENC_MISTAT_BUSY) {
332 printf("%s: timeout reading phy\n", enc->dev->name);
335 enc_w8(enc, CTL_REG_MICMD, 0);
336 return enc_r16(enc, CTL_REG_MIRDL);
342 static void enc_phy_write(enc_dev_t *enc, const u8 addr, const u16 data)
347 enc_w8(enc, CTL_REG_MIREGADR, addr);
348 enc_w16(enc, CTL_REG_MIWRL, data);
349 /* 1 second timeout - only happens on hardware problem */
350 etime = get_ticks() + get_tbclk();
351 /* poll MISTAT.BUSY bit until operation is complete */
354 status = enc_r8(enc, CTL_REG_MISTAT);
355 } while (get_ticks() <= etime && (status & ENC_MISTAT_BUSY));
356 if (status & ENC_MISTAT_BUSY) {
357 printf("%s: timeout writing phy\n", enc->dev->name);
363 * Verify link status, wait if necessary
365 * Note: with a 10 MBit/s only PHY there is no autonegotiation possible,
366 * half/full duplex is a pure setup matter. For the time being, this driver
367 * will setup in half duplex mode only.
369 static int enc_phy_link_wait(enc_dev_t *enc)
375 #ifdef CONFIG_ENC_SILENTLINK
376 /* check if we have a link, then just return */
377 status = enc_phy_read(enc, PHY_REG_PHSTAT1);
378 if (status & ENC_PHSTAT1_LLSTAT)
382 /* wait for link with 1 second timeout */
383 etime = get_ticks() + get_tbclk();
384 while (get_ticks() <= etime) {
385 status = enc_phy_read(enc, PHY_REG_PHSTAT1);
386 if (status & ENC_PHSTAT1_LLSTAT) {
387 /* now we have a link */
388 status = enc_phy_read(enc, PHY_REG_PHSTAT2);
389 duplex = (status & ENC_PHSTAT2_DPXSTAT) ? 1 : 0;
390 printf("%s: link up, 10Mbps %s-duplex\n",
391 enc->dev->name, duplex ? "full" : "half");
397 /* timeout occured */
398 printf("%s: link down\n", enc->dev->name);
403 * This function resets the receiver only.
405 static void enc_reset_rx(enc_dev_t *enc)
409 econ1 = enc_r8(enc, CTL_REG_ECON1);
410 if ((econ1 & ENC_ECON1_RXRST) == 0) {
411 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXRST);
412 enc->rx_reset_counter = RX_RESET_COUNTER;
417 * Reset receiver and reenable it.
419 static void enc_reset_rx_call(enc_dev_t *enc)
421 enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_RXRST);
422 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
426 * Copy a packet from the receive ring and forward it to
427 * the protocol stack.
429 static void enc_receive(enc_dev_t *enc)
431 u8 *packet = (u8 *)NetRxPackets[0];
439 enc_w16(enc, CTL_REG_ERDPTL, enc->next_pointer);
441 enc_rbuf(enc, 6, hbuf);
442 enc->next_pointer = hbuf[0] | (hbuf[1] << 8);
443 pkt_len = hbuf[2] | (hbuf[3] << 8);
444 status = hbuf[4] | (hbuf[5] << 8);
445 debug("next_pointer=$%04x pkt_len=%u status=$%04x\n",
446 enc->next_pointer, pkt_len, status);
447 if (pkt_len <= ENC_MAX_FRM_LEN)
451 if ((status & (1L << 7)) == 0) /* check Received Ok bit */
453 /* check if next pointer is resonable */
454 if (enc->next_pointer >= ENC_TX_BUF_START)
457 enc_rbuf(enc, copy_len, packet);
459 /* advance read pointer to next pointer */
460 enc_w16(enc, CTL_REG_ERDPTL, enc->next_pointer);
461 /* decrease packet counter */
462 enc_bset(enc, CTL_REG_ECON2, ENC_ECON2_PKTDEC);
464 * Only odd values should be written to ERXRDPTL,
465 * see errata B4 pt.13
467 rxbuf_rdpt = enc->next_pointer - 1;
468 if ((rxbuf_rdpt < enc_r16(enc, CTL_REG_ERXSTL)) ||
469 (rxbuf_rdpt > enc_r16(enc, CTL_REG_ERXNDL))) {
470 enc_w16(enc, CTL_REG_ERXRDPTL,
471 enc_r16(enc, CTL_REG_ERXNDL));
473 enc_w16(enc, CTL_REG_ERXRDPTL, rxbuf_rdpt);
476 pkt_cnt = enc_r8(enc, CTL_REG_EPKTCNT);
478 (void)enc_r8(enc, CTL_REG_EIR);
480 printf("%s: receive copy_len=0\n", enc->dev->name);
484 * Because NetReceive() might call enc_send(), we need to
485 * release the SPI bus, call NetReceive(), reclaim the bus
487 enc_release_bus(enc);
488 NetReceive(packet, pkt_len);
489 if (enc_claim_bus(enc))
491 (void)enc_r8(enc, CTL_REG_EIR);
493 /* Use EPKTCNT not EIR.PKTIF flag, see errata pt. 6 */
497 * Poll for completely received packets.
499 static void enc_poll(enc_dev_t *enc)
504 #ifdef CONFIG_USE_IRQ
505 /* clear global interrupt enable bit in enc28j60 */
506 enc_bclr(enc, CTL_REG_EIE, ENC_EIE_INTIE);
508 (void)enc_r8(enc, CTL_REG_ESTAT);
509 eir_reg = enc_r8(enc, CTL_REG_EIR);
510 if (eir_reg & ENC_EIR_TXIF) {
511 /* clear TXIF bit in EIR */
512 enc_bclr(enc, CTL_REG_EIR, ENC_EIR_TXIF);
514 /* We have to use pktcnt and not pktif bit, see errata pt. 6 */
515 pkt_cnt = enc_r8(enc, CTL_REG_EPKTCNT);
517 if ((eir_reg & ENC_EIR_PKTIF) == 0) {
518 debug("enc_poll: pkt cnt > 0, but pktif not set\n");
522 * clear PKTIF bit in EIR, this should not need to be done
523 * but it seems like we get problems if we do not
525 enc_bclr(enc, CTL_REG_EIR, ENC_EIR_PKTIF);
527 if (eir_reg & ENC_EIR_RXERIF) {
528 printf("%s: rx error\n", enc->dev->name);
529 enc_bclr(enc, CTL_REG_EIR, ENC_EIR_RXERIF);
531 if (eir_reg & ENC_EIR_TXERIF) {
532 printf("%s: tx error\n", enc->dev->name);
533 enc_bclr(enc, CTL_REG_EIR, ENC_EIR_TXERIF);
535 #ifdef CONFIG_USE_IRQ
536 /* set global interrupt enable bit in enc28j60 */
537 enc_bset(enc, CTL_REG_EIE, ENC_EIE_INTIE);
542 * Completely Reset the ENC
544 static void enc_reset(enc_dev_t *enc)
549 spi_xfer(enc->slave, 8, dout, NULL,
550 SPI_XFER_BEGIN | SPI_XFER_END);
551 /* sleep 1 ms. See errata pt. 2 */
556 * Initialisation data for most of the ENC registers
558 static const u16 enc_initdata[] = {
560 * Setup the buffer space. The reset values are valid for the
563 * We shall not write to ERXST, see errata pt. 5. Instead we
564 * have to make sure that ENC_RX_BUS_START is 0.
566 CTL_REG_ERXSTL, ENC_RX_BUF_START,
567 CTL_REG_ERXSTH, ENC_RX_BUF_START >> 8,
568 CTL_REG_ERXNDL, ENC_RX_BUF_END,
569 CTL_REG_ERXNDH, ENC_RX_BUF_END >> 8,
570 CTL_REG_ERDPTL, ENC_RX_BUF_START,
571 CTL_REG_ERDPTH, ENC_RX_BUF_START >> 8,
573 * Set the filter to receive only good-CRC, unicast and broadcast
575 * Note: some DHCP servers return their answers as broadcasts!
576 * So its unwise to remove broadcast from this. This driver
577 * might incur receiver overruns with packet loss on a broadcast
580 CTL_REG_ERXFCON, ENC_RFR_BCEN | ENC_RFR_UCEN | ENC_RFR_CRCEN,
582 /* enable MAC to receive frames */
584 ENC_MACON1_MARXEN | ENC_MACON1_TXPAUS | ENC_MACON1_RXPAUS,
586 /* configure pad, tx-crc and duplex */
588 ENC_MACON3_PADCFG0 | ENC_MACON3_TXCRCEN |
591 /* Allow infinite deferals if the medium is continously busy */
592 CTL_REG_MACON4, ENC_MACON4_DEFER,
594 /* Late collisions occur beyond 63 bytes */
595 CTL_REG_MACLCON2, 63,
598 * Set (low byte) Non-Back-to_Back Inter-Packet Gap.
601 CTL_REG_MAIPGL, 0x12,
604 * Set (high byte) Non-Back-to_Back Inter-Packet Gap.
605 * Recommended 0x0c for half-duplex. Nothing for full-duplex
607 CTL_REG_MAIPGH, 0x0C,
609 /* set maximum frame length */
610 CTL_REG_MAMXFLL, ENC_MAX_FRM_LEN,
611 CTL_REG_MAMXFLH, ENC_MAX_FRM_LEN >> 8,
614 * Set MAC back-to-back inter-packet gap.
615 * Recommended 0x12 for half duplex
616 * and 0x15 for full duplex.
618 CTL_REG_MABBIPG, 0x12,
625 * Wait for the XTAL oscillator to become ready
627 static int enc_clock_wait(enc_dev_t *enc)
631 /* one second timeout */
632 etime = get_ticks() + get_tbclk();
635 * Wait for CLKRDY to become set (i.e., check that we can
636 * communicate with the ENC)
640 if (enc_r8(enc, CTL_REG_ESTAT) & ENC_ESTAT_CLKRDY)
642 } while (get_ticks() <= etime);
644 printf("%s: timeout waiting for CLKRDY\n", enc->dev->name);
649 * Write the MAC address into the ENC
651 static int enc_write_macaddr(enc_dev_t *enc)
653 unsigned char *p = enc->dev->enetaddr;
655 enc_w8_retry(enc, CTL_REG_MAADR5, *p++, 5);
656 enc_w8_retry(enc, CTL_REG_MAADR4, *p++, 5);
657 enc_w8_retry(enc, CTL_REG_MAADR3, *p++, 5);
658 enc_w8_retry(enc, CTL_REG_MAADR2, *p++, 5);
659 enc_w8_retry(enc, CTL_REG_MAADR1, *p++, 5);
660 enc_w8_retry(enc, CTL_REG_MAADR0, *p, 5);
665 * Setup most of the ENC registers
667 static int enc_setup(enc_dev_t *enc)
673 /* reset enc struct values */
674 enc->next_pointer = ENC_RX_BUF_START;
675 enc->rx_reset_counter = RX_RESET_COUNTER;
676 enc->bank = 0xff; /* invalidate current bank in enc28j60 */
678 /* verify PHY identification */
679 phid1 = enc_phy_read(enc, PHY_REG_PHID1);
680 phid2 = enc_phy_read(enc, PHY_REG_PHID2) & ENC_PHID2_MASK;
681 if (phid1 != ENC_PHID1_VALUE || phid2 != ENC_PHID2_VALUE) {
682 printf("%s: failed to identify PHY. Found %04x:%04x\n",
683 enc->dev->name, phid1, phid2);
687 /* now program registers */
688 for (tp = enc_initdata; *tp != 0xffff; tp += 2)
689 enc_w8_retry(enc, tp[0], tp[1], 10);
692 * Prevent automatic loopback of data beeing transmitted by setting
695 enc_phy_write(enc, PHY_REG_PHCON2, (1<<8));
699 * LEDA: LACFG = 0100 -> display link status
700 * LEDB: LBCFG = 0111 -> display TX & RX activity
701 * STRCH = 1 -> LED pulses
703 enc_phy_write(enc, PHY_REG_PHLCON, 0x0472);
705 /* Reset PDPXMD-bit => half duplex */
706 enc_phy_write(enc, PHY_REG_PHCON1, 0);
708 #ifdef CONFIG_USE_IRQ
709 /* enable interrupts */
710 enc_bset(enc, CTL_REG_EIE, ENC_EIE_PKTIE);
711 enc_bset(enc, CTL_REG_EIE, ENC_EIE_TXIE);
712 enc_bset(enc, CTL_REG_EIE, ENC_EIE_RXERIE);
713 enc_bset(enc, CTL_REG_EIE, ENC_EIE_TXERIE);
714 enc_bset(enc, CTL_REG_EIE, ENC_EIE_INTIE);
721 * Check if ENC has been initialized.
722 * If not, try to initialize it.
723 * Remember initialized state in struct.
725 static int enc_initcheck(enc_dev_t *enc, const enum enc_initstate requiredstate)
727 if (enc->initstate >= requiredstate)
730 if (enc->initstate < setupdone) {
731 /* Initialize the ENC only */
733 /* if any of functions fails, skip the rest and return an error */
734 if (enc_clock_wait(enc) || enc_setup(enc) || enc_write_macaddr(enc)) {
737 enc->initstate = setupdone;
739 /* if that's all we need, return here */
740 if (enc->initstate >= requiredstate)
743 /* now wait for link ready condition */
744 if (enc_phy_link_wait(enc)) {
747 enc->initstate = linkready;
751 #if defined(CONFIG_CMD_MII)
753 * Read a PHY register.
755 * This function is registered with miiphy_register().
757 int enc_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
759 struct eth_device *dev = eth_get_dev_by_name(devname);
762 if (!dev || phy_adr != 0)
766 if (enc_claim_bus(enc))
768 if (enc_initcheck(enc, setupdone)) {
769 enc_release_bus(enc);
772 *value = enc_phy_read(enc, reg);
773 enc_release_bus(enc);
778 * Write a PHY register.
780 * This function is registered with miiphy_register().
782 int enc_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
784 struct eth_device *dev = eth_get_dev_by_name(devname);
787 if (!dev || phy_adr != 0)
791 if (enc_claim_bus(enc))
793 if (enc_initcheck(enc, setupdone)) {
794 enc_release_bus(enc);
797 enc_phy_write(enc, reg, value);
798 enc_release_bus(enc);
804 * Write hardware (MAC) address.
806 * This function entered into eth_device structure.
808 static int enc_write_hwaddr(struct eth_device *dev)
810 enc_dev_t *enc = dev->priv;
812 if (enc_claim_bus(enc))
814 if (enc_initcheck(enc, setupdone)) {
815 enc_release_bus(enc);
818 enc_release_bus(enc);
823 * Initialize ENC28J60 for use.
825 * This function entered into eth_device structure.
827 static int enc_init(struct eth_device *dev, bd_t *bis)
829 enc_dev_t *enc = dev->priv;
831 if (enc_claim_bus(enc))
833 if (enc_initcheck(enc, linkready)) {
834 enc_release_bus(enc);
838 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
839 enc_release_bus(enc);
844 * Check for received packets.
846 * This function entered into eth_device structure.
848 static int enc_recv(struct eth_device *dev)
850 enc_dev_t *enc = dev->priv;
852 if (enc_claim_bus(enc))
854 if (enc_initcheck(enc, linkready)) {
855 enc_release_bus(enc);
858 /* Check for dead receiver */
859 if (enc->rx_reset_counter > 0)
860 enc->rx_reset_counter--;
862 enc_reset_rx_call(enc);
864 enc_release_bus(enc);
871 * This function entered into eth_device structure.
873 * Should we wait here until we have a Link? Or shall we leave that to
877 struct eth_device *dev,
881 enc_dev_t *enc = dev->priv;
883 if (enc_claim_bus(enc))
885 if (enc_initcheck(enc, linkready)) {
886 enc_release_bus(enc);
889 /* setup transmit pointers */
890 enc_w16(enc, CTL_REG_EWRPTL, ENC_TX_BUF_START);
891 enc_w16(enc, CTL_REG_ETXNDL, length + ENC_TX_BUF_START);
892 enc_w16(enc, CTL_REG_ETXSTL, ENC_TX_BUF_START);
893 /* write packet to ENC */
894 enc_wbuf(enc, length, (u8 *) packet, 0x00);
896 * Check that the internal transmit logic has not been altered
897 * by excessive collisions. Reset transmitter if so.
898 * See Errata B4 12 and 14.
900 if (enc_r8(enc, CTL_REG_EIR) & ENC_EIR_TXERIF) {
901 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_TXRST);
902 enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_TXRST);
904 enc_bclr(enc, CTL_REG_EIR, (ENC_EIR_TXERIF | ENC_EIR_TXIF));
905 /* start transmitting */
906 enc_bset(enc, CTL_REG_ECON1, ENC_ECON1_TXRTS);
907 enc_release_bus(enc);
914 * This function entered into eth_device structure.
916 static void enc_halt(struct eth_device *dev)
918 enc_dev_t *enc = dev->priv;
920 if (enc_claim_bus(enc))
922 /* Just disable receiver */
923 enc_bclr(enc, CTL_REG_ECON1, ENC_ECON1_RXEN);
924 enc_release_bus(enc);
928 * This is the only exported function.
930 * It may be called several times with different bus:cs combinations.
932 int enc28j60_initialize(unsigned int bus, unsigned int cs,
933 unsigned int max_hz, unsigned int mode)
935 struct eth_device *dev;
938 /* try to allocate, check and clear eth_device object */
939 dev = malloc(sizeof(*dev));
943 memset(dev, 0, sizeof(*dev));
945 /* try to allocate, check and clear enc_dev_t object */
946 enc = malloc(sizeof(*enc));
951 memset(enc, 0, sizeof(*enc));
953 /* try to setup the SPI slave */
954 enc->slave = spi_setup_slave(bus, cs, max_hz, mode);
956 printf("enc28j60: invalid SPI device %i:%i\n", bus, cs);
963 /* now fill the eth_device object */
965 dev->init = enc_init;
966 dev->halt = enc_halt;
967 dev->send = enc_send;
968 dev->recv = enc_recv;
969 dev->write_hwaddr = enc_write_hwaddr;
970 sprintf(dev->name, "enc%i.%i", bus, cs);
972 #if defined(CONFIG_CMD_MII)
973 miiphy_register(dev->name, enc_miiphy_read, enc_miiphy_write);