Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 1 | /* |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 2 | * Driver for Blackfin On-Chip MAC device |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 3 | * |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 4 | * Copyright (c) 2005-2008 Analog Device, Inc. |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 5 | * |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 6 | * Licensed under the GPL-2 or later. |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <config.h> |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 11 | #include <net.h> |
Ben Warren | 89973f8 | 2008-08-31 22:22:04 -0700 | [diff] [blame] | 12 | #include <netdev.h> |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 13 | #include <command.h> |
| 14 | #include <malloc.h> |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 15 | #include <miiphy.h> |
| 16 | #include <linux/mii.h> |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 17 | |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 18 | #include <asm/blackfin.h> |
Tom Rini | 130fbeb | 2014-02-20 10:14:10 -0500 | [diff] [blame] | 19 | #include <asm/clock.h> |
Mike Frysinger | 8339ad7 | 2010-06-02 05:56:22 -0400 | [diff] [blame] | 20 | #include <asm/portmux.h> |
Mike Frysinger | d4d7730 | 2008-02-04 19:26:55 -0500 | [diff] [blame] | 21 | #include <asm/mach-common/bits/dma.h> |
| 22 | #include <asm/mach-common/bits/emac.h> |
| 23 | #include <asm/mach-common/bits/pll.h> |
| 24 | |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 25 | #include "bfin_mac.h" |
| 26 | |
Mike Frysinger | a7ec6ac | 2008-10-20 13:59:51 -0400 | [diff] [blame] | 27 | #ifndef CONFIG_PHY_ADDR |
| 28 | # define CONFIG_PHY_ADDR 1 |
| 29 | #endif |
| 30 | #ifndef CONFIG_PHY_CLOCK_FREQ |
| 31 | # define CONFIG_PHY_CLOCK_FREQ 2500000 |
| 32 | #endif |
| 33 | |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 34 | #ifdef CONFIG_POST |
| 35 | #include <post.h> |
| 36 | #endif |
| 37 | |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 38 | #define RXBUF_BASE_ADDR 0xFF900000 |
| 39 | #define TXBUF_BASE_ADDR 0xFF800000 |
| 40 | #define TX_BUF_CNT 1 |
| 41 | |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 42 | #define TOUT_LOOP 1000000 |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 43 | |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 44 | static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT]; |
| 45 | static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX]; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 46 | static u16 txIdx; /* index of the current RX buffer */ |
| 47 | static u16 rxIdx; /* index of the current TX buffer */ |
| 48 | |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 49 | /* DMAx_CONFIG values at DMA Restart */ |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 50 | static const union { |
| 51 | u16 data; |
| 52 | ADI_DMA_CONFIG_REG reg; |
| 53 | } txdmacfg = { |
| 54 | .reg = { |
| 55 | .b_DMA_EN = 1, /* enabled */ |
| 56 | .b_WNR = 0, /* read from memory */ |
| 57 | .b_WDSIZE = 2, /* wordsize is 32 bits */ |
| 58 | .b_DMA2D = 0, |
| 59 | .b_RESTART = 0, |
| 60 | .b_DI_SEL = 0, |
| 61 | .b_DI_EN = 0, /* no interrupt */ |
| 62 | .b_NDSIZE = 5, /* 5 half words is desc size */ |
| 63 | .b_FLOW = 7 /* large desc flow */ |
| 64 | }, |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 65 | }; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 66 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 67 | static int bfin_miiphy_wait(void) |
| 68 | { |
| 69 | /* poll the STABUSY bit */ |
| 70 | while (bfin_read_EMAC_STAADD() & STABUSY) |
| 71 | continue; |
| 72 | return 0; |
| 73 | } |
| 74 | |
Mike Frysinger | 5700bb6 | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 75 | static int bfin_miiphy_read(const char *devname, uchar addr, uchar reg, ushort *val) |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 76 | { |
| 77 | if (bfin_miiphy_wait()) |
| 78 | return 1; |
| 79 | bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY); |
| 80 | if (bfin_miiphy_wait()) |
| 81 | return 1; |
| 82 | *val = bfin_read_EMAC_STADAT(); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Mike Frysinger | 5700bb6 | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 86 | static int bfin_miiphy_write(const char *devname, uchar addr, uchar reg, ushort val) |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 87 | { |
| 88 | if (bfin_miiphy_wait()) |
| 89 | return 1; |
| 90 | bfin_write_EMAC_STADAT(val); |
| 91 | bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 95 | int bfin_EMAC_initialize(bd_t *bis) |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 96 | { |
| 97 | struct eth_device *dev; |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 98 | dev = malloc(sizeof(*dev)); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 99 | if (dev == NULL) |
| 100 | hang(); |
| 101 | |
| 102 | memset(dev, 0, sizeof(*dev)); |
Mike Frysinger | 94060a1 | 2010-06-09 21:50:48 -0400 | [diff] [blame] | 103 | strcpy(dev->name, "bfin_mac"); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 104 | |
| 105 | dev->iobase = 0; |
| 106 | dev->priv = 0; |
| 107 | dev->init = bfin_EMAC_init; |
| 108 | dev->halt = bfin_EMAC_halt; |
| 109 | dev->send = bfin_EMAC_send; |
| 110 | dev->recv = bfin_EMAC_recv; |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 111 | dev->write_hwaddr = bfin_EMAC_setup_addr; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 112 | |
| 113 | eth_register(dev); |
| 114 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 115 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 116 | miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write); |
| 117 | #endif |
| 118 | |
Ben Warren | 9149473 | 2008-07-11 23:15:28 -0700 | [diff] [blame] | 119 | return 0; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 122 | static int bfin_EMAC_send(struct eth_device *dev, void *packet, int length) |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | int result = 0; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 126 | |
| 127 | if (length <= 0) { |
| 128 | printf("Ethernet: bad packet size: %d\n", length); |
| 129 | goto out; |
| 130 | } |
| 131 | |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 132 | if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) { |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 133 | printf("Ethernet: tx DMA error\n"); |
| 134 | goto out; |
| 135 | } |
| 136 | |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 137 | for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) { |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 138 | if (i > TOUT_LOOP) { |
| 139 | puts("Ethernet: tx time out\n"); |
| 140 | goto out; |
| 141 | } |
| 142 | } |
| 143 | txbuf[txIdx]->FrmData->NoBytes = length; |
| 144 | memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length); |
| 145 | txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData; |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 146 | bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma); |
| 147 | bfin_write_DMA2_CONFIG(txdmacfg.data); |
| 148 | bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 149 | |
| 150 | for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) { |
| 151 | if (i > TOUT_LOOP) { |
| 152 | puts("Ethernet: tx error\n"); |
| 153 | goto out; |
| 154 | } |
| 155 | } |
| 156 | result = txbuf[txIdx]->StatusWord; |
| 157 | txbuf[txIdx]->StatusWord = 0; |
| 158 | if ((txIdx + 1) >= TX_BUF_CNT) |
| 159 | txIdx = 0; |
| 160 | else |
| 161 | txIdx++; |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 162 | out: |
Mike Frysinger | 8eed6ca | 2008-11-05 06:36:15 -0500 | [diff] [blame] | 163 | debug("BFIN EMAC send: length = %d\n", length); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 164 | return result; |
| 165 | } |
| 166 | |
| 167 | static int bfin_EMAC_recv(struct eth_device *dev) |
| 168 | { |
| 169 | int length = 0; |
| 170 | |
| 171 | for (;;) { |
| 172 | if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) { |
| 173 | length = -1; |
| 174 | break; |
| 175 | } |
| 176 | if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) { |
| 177 | printf("Ethernet: rx dma overrun\n"); |
| 178 | break; |
| 179 | } |
| 180 | if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) { |
| 181 | printf("Ethernet: rx error\n"); |
| 182 | break; |
| 183 | } |
| 184 | length = rxbuf[rxIdx]->StatusWord & 0x000007FF; |
| 185 | if (length <= 4) { |
| 186 | printf("Ethernet: bad frame\n"); |
| 187 | break; |
| 188 | } |
Robin Getz | 488feef | 2009-08-24 10:33:39 -0400 | [diff] [blame] | 189 | |
| 190 | debug("%s: len = %d\n", __func__, length - 4); |
| 191 | |
Mike Frysinger | 6705e03 | 2012-07-19 00:08:47 -0400 | [diff] [blame] | 192 | NetRxPackets[rxIdx] = rxbuf[rxIdx]->FrmData->Dest; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 193 | NetReceive(NetRxPackets[rxIdx], length - 4); |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 194 | bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 195 | rxbuf[rxIdx]->StatusWord = 0x00000000; |
| 196 | if ((rxIdx + 1) >= PKTBUFSRX) |
| 197 | rxIdx = 0; |
| 198 | else |
| 199 | rxIdx++; |
| 200 | } |
| 201 | |
| 202 | return length; |
| 203 | } |
| 204 | |
| 205 | /************************************************************** |
| 206 | * |
| 207 | * Ethernet Initialization Routine |
| 208 | * |
| 209 | *************************************************************/ |
| 210 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 211 | /* MDC = SCLK / MDC_freq / 2 - 1 */ |
| 212 | #define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1) |
| 213 | |
Mike Frysinger | 8339ad7 | 2010-06-02 05:56:22 -0400 | [diff] [blame] | 214 | #ifndef CONFIG_BFIN_MAC_PINS |
| 215 | # ifdef CONFIG_RMII |
| 216 | # define CONFIG_BFIN_MAC_PINS P_RMII0 |
| 217 | # else |
| 218 | # define CONFIG_BFIN_MAC_PINS P_MII0 |
| 219 | # endif |
| 220 | #endif |
| 221 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 222 | static int bfin_miiphy_init(struct eth_device *dev, int *opmode) |
| 223 | { |
Mike Frysinger | 8339ad7 | 2010-06-02 05:56:22 -0400 | [diff] [blame] | 224 | const unsigned short pins[] = CONFIG_BFIN_MAC_PINS; |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 225 | u16 phydat; |
| 226 | size_t count; |
| 227 | |
| 228 | /* Enable PHY output */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 229 | bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 230 | |
| 231 | /* Set all the pins to peripheral mode */ |
Mike Frysinger | 8339ad7 | 2010-06-02 05:56:22 -0400 | [diff] [blame] | 232 | peripheral_request_list(pins, "bfin_mac"); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 233 | |
| 234 | /* Odd word alignment for Receive Frame DMA word */ |
| 235 | /* Configure checksum support and rcve frame word alignment */ |
Mike Frysinger | a7ec6ac | 2008-10-20 13:59:51 -0400 | [diff] [blame] | 236 | bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ))); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 237 | |
| 238 | /* turn on auto-negotiation and wait for link to come up */ |
Mike Frysinger | a7ec6ac | 2008-10-20 13:59:51 -0400 | [diff] [blame] | 239 | bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 240 | count = 0; |
| 241 | while (1) { |
| 242 | ++count; |
Mike Frysinger | a7ec6ac | 2008-10-20 13:59:51 -0400 | [diff] [blame] | 243 | if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat)) |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 244 | return -1; |
| 245 | if (phydat & BMSR_LSTATUS) |
| 246 | break; |
| 247 | if (count > 30000) { |
| 248 | printf("%s: link down, check cable\n", dev->name); |
| 249 | return -1; |
| 250 | } |
| 251 | udelay(100); |
| 252 | } |
| 253 | |
| 254 | /* see what kind of link we have */ |
Mike Frysinger | a7ec6ac | 2008-10-20 13:59:51 -0400 | [diff] [blame] | 255 | if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat)) |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 256 | return -1; |
| 257 | if (phydat & LPA_DUPLEX) |
| 258 | *opmode = FDMODE; |
| 259 | else |
| 260 | *opmode = 0; |
| 261 | |
| 262 | bfin_write_EMAC_MMC_CTL(RSTC | CROLL); |
Aaron Wu | 819ca38 | 2011-11-23 11:23:56 +0800 | [diff] [blame] | 263 | bfin_write_EMAC_VLAN1(EMAC_VLANX_DEF_VAL); |
| 264 | bfin_write_EMAC_VLAN2(EMAC_VLANX_DEF_VAL); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 265 | |
| 266 | /* Initialize the TX DMA channel registers */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 267 | bfin_write_DMA2_X_COUNT(0); |
| 268 | bfin_write_DMA2_X_MODIFY(4); |
| 269 | bfin_write_DMA2_Y_COUNT(0); |
| 270 | bfin_write_DMA2_Y_MODIFY(0); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 271 | |
| 272 | /* Initialize the RX DMA channel registers */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 273 | bfin_write_DMA1_X_COUNT(0); |
| 274 | bfin_write_DMA1_X_MODIFY(4); |
| 275 | bfin_write_DMA1_Y_COUNT(0); |
| 276 | bfin_write_DMA1_Y_MODIFY(0); |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 281 | static int bfin_EMAC_setup_addr(struct eth_device *dev) |
| 282 | { |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 283 | bfin_write_EMAC_ADDRLO( |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 284 | dev->enetaddr[0] | |
| 285 | dev->enetaddr[1] << 8 | |
| 286 | dev->enetaddr[2] << 16 | |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 287 | dev->enetaddr[3] << 24 |
| 288 | ); |
| 289 | bfin_write_EMAC_ADDRHI( |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 290 | dev->enetaddr[4] | |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 291 | dev->enetaddr[5] << 8 |
| 292 | ); |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |
Mike Frysinger | 395bce4 | 2008-02-24 23:58:13 -0500 | [diff] [blame] | 296 | static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd) |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 297 | { |
| 298 | u32 opmode; |
| 299 | int dat; |
| 300 | int i; |
Mike Frysinger | 8eed6ca | 2008-11-05 06:36:15 -0500 | [diff] [blame] | 301 | debug("Eth_init: ......\n"); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 302 | |
| 303 | txIdx = 0; |
| 304 | rxIdx = 0; |
| 305 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 306 | /* Initialize System Register */ |
| 307 | if (bfin_miiphy_init(dev, &dat) < 0) |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 308 | return -1; |
| 309 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 310 | /* Initialize EMAC address */ |
Mike Frysinger | 4324dc7 | 2010-04-27 14:15:28 -0400 | [diff] [blame] | 311 | bfin_EMAC_setup_addr(dev); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 312 | |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 313 | /* Initialize TX and RX buffer */ |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 314 | for (i = 0; i < PKTBUFSRX; i++) { |
| 315 | rxbuf[i] = SetupRxBuffer(i); |
| 316 | if (i > 0) { |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 317 | rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 318 | if (i == (PKTBUFSRX - 1)) |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 319 | rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | for (i = 0; i < TX_BUF_CNT; i++) { |
| 323 | txbuf[i] = SetupTxBuffer(i); |
| 324 | if (i > 0) { |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 325 | txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 326 | if (i == (TX_BUF_CNT - 1)) |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 327 | txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | /* Set RX DMA */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 332 | bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma); |
| 333 | bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 334 | |
| 335 | /* Wait MII done */ |
Mike Frysinger | ac45af4 | 2008-10-14 04:52:00 -0400 | [diff] [blame] | 336 | bfin_miiphy_wait(); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 337 | |
| 338 | /* We enable only RX here */ |
| 339 | /* ASTP : Enable Automatic Pad Stripping |
| 340 | PR : Promiscuous Mode for test |
| 341 | PSF : Receive frames with total length less than 64 bytes. |
| 342 | FDMODE : Full Duplex Mode |
| 343 | LB : Internal Loopback for test |
| 344 | RE : Receiver Enable */ |
| 345 | if (dat == FDMODE) |
| 346 | opmode = ASTP | FDMODE | PSF; |
| 347 | else |
| 348 | opmode = ASTP | PSF; |
| 349 | opmode |= RE; |
Mike Frysinger | 092d248 | 2008-12-09 17:46:21 -0500 | [diff] [blame] | 350 | #ifdef CONFIG_RMII |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 351 | opmode |= TE | RMII; |
| 352 | #endif |
| 353 | /* Turn on the EMAC */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 354 | bfin_write_EMAC_OPMODE(opmode); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static void bfin_EMAC_halt(struct eth_device *dev) |
| 359 | { |
Mike Frysinger | 8eed6ca | 2008-11-05 06:36:15 -0500 | [diff] [blame] | 360 | debug("Eth_halt: ......\n"); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 361 | /* Turn off the EMAC */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 362 | bfin_write_EMAC_OPMODE(0); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 363 | /* Turn off the EMAC RX DMA */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 364 | bfin_write_DMA1_CONFIG(0); |
| 365 | bfin_write_DMA2_CONFIG(0); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 368 | ADI_ETHER_BUFFER *SetupRxBuffer(int no) |
| 369 | { |
| 370 | ADI_ETHER_FRAME_BUFFER *frmbuf; |
| 371 | ADI_ETHER_BUFFER *buf; |
| 372 | int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */ |
| 373 | int total_size = nobytes_buffer + RECV_BUFSIZE; |
| 374 | |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 375 | buf = (void *) (RXBUF_BASE_ADDR + no * total_size); |
| 376 | frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 377 | |
| 378 | memset(buf, 0x00, nobytes_buffer); |
| 379 | buf->FrmData = frmbuf; |
| 380 | memset(frmbuf, 0xfe, RECV_BUFSIZE); |
| 381 | |
| 382 | /* set up first desc to point to receive frame buffer */ |
| 383 | buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]); |
| 384 | buf->Dma[0].START_ADDR = (u32) buf->FrmData; |
| 385 | buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */ |
| 386 | buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */ |
| 387 | buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */ |
| 388 | buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */ |
| 389 | buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */ |
| 390 | |
| 391 | /* set up second desc to point to status word */ |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 392 | buf->Dma[1].NEXT_DESC_PTR = buf->Dma; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 393 | buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum; |
| 394 | buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */ |
| 395 | buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */ |
| 396 | buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */ |
| 397 | buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */ |
| 398 | buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */ |
| 399 | buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */ |
| 400 | |
| 401 | return buf; |
| 402 | } |
| 403 | |
| 404 | ADI_ETHER_BUFFER *SetupTxBuffer(int no) |
| 405 | { |
| 406 | ADI_ETHER_FRAME_BUFFER *frmbuf; |
| 407 | ADI_ETHER_BUFFER *buf; |
| 408 | int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */ |
| 409 | int total_size = nobytes_buffer + RECV_BUFSIZE; |
| 410 | |
Mike Frysinger | 6d7d480 | 2009-01-08 11:57:57 -0500 | [diff] [blame] | 411 | buf = (void *) (TXBUF_BASE_ADDR + no * total_size); |
| 412 | frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 413 | |
| 414 | memset(buf, 0x00, nobytes_buffer); |
| 415 | buf->FrmData = frmbuf; |
| 416 | memset(frmbuf, 0x00, RECV_BUFSIZE); |
| 417 | |
| 418 | /* set up first desc to point to receive frame buffer */ |
| 419 | buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]); |
| 420 | buf->Dma[0].START_ADDR = (u32) buf->FrmData; |
| 421 | buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */ |
| 422 | buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */ |
| 423 | buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */ |
| 424 | buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */ |
| 425 | buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */ |
| 426 | |
| 427 | /* set up second desc to point to status word */ |
| 428 | buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]); |
| 429 | buf->Dma[1].START_ADDR = (u32) & buf->StatusWord; |
| 430 | buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */ |
| 431 | buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */ |
| 432 | buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */ |
| 433 | buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */ |
| 434 | buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */ |
| 435 | buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */ |
| 436 | |
| 437 | return buf; |
| 438 | } |
| 439 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 440 | #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER) |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 441 | int ether_post_test(int flags) |
| 442 | { |
| 443 | uchar buf[64]; |
| 444 | int i, value = 0; |
| 445 | int length; |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 446 | uint addr; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 447 | |
| 448 | printf("\n--------"); |
| 449 | bfin_EMAC_init(NULL, NULL); |
| 450 | /* construct the package */ |
Mike Frysinger | 0c71481 | 2010-07-25 16:38:12 -0400 | [diff] [blame] | 451 | addr = bfin_read_EMAC_ADDRLO(); |
| 452 | buf[0] = buf[6] = addr; |
| 453 | buf[1] = buf[7] = addr >> 8; |
| 454 | buf[2] = buf[8] = addr >> 16; |
| 455 | buf[3] = buf[9] = addr >> 24; |
| 456 | addr = bfin_read_EMAC_ADDRHI(); |
| 457 | buf[4] = buf[10] = addr; |
| 458 | buf[5] = buf[11] = addr >> 8; |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 459 | buf[12] = 0x08; /* Type: ARP */ |
| 460 | buf[13] = 0x06; |
| 461 | buf[14] = 0x00; /* Hardware type: Ethernet */ |
| 462 | buf[15] = 0x01; |
| 463 | buf[16] = 0x08; /* Protocal type: IP */ |
| 464 | buf[17] = 0x00; |
| 465 | buf[18] = 0x06; /* Hardware size */ |
| 466 | buf[19] = 0x04; /* Protocol size */ |
| 467 | buf[20] = 0x00; /* Opcode: request */ |
| 468 | buf[21] = 0x01; |
| 469 | |
| 470 | for (i = 0; i < 42; i++) |
| 471 | buf[i + 22] = i; |
| 472 | printf("--------Send 64 bytes......\n"); |
Joe Hershberger | 10cbe3b | 2012-05-22 18:36:19 +0000 | [diff] [blame] | 473 | bfin_EMAC_send(NULL, buf, 64); |
Aubrey Li | 26bf7de | 2007-03-19 01:24:52 +0800 | [diff] [blame] | 474 | for (i = 0; i < 100; i++) { |
| 475 | udelay(10000); |
| 476 | if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) { |
| 477 | value = 1; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | if (value == 0) { |
| 482 | printf("--------EMAC can't receive any data\n"); |
| 483 | eth_halt(); |
| 484 | return -1; |
| 485 | } |
| 486 | length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4; |
| 487 | for (i = 0; i < length; i++) { |
| 488 | if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) { |
| 489 | printf("--------EMAC receive error data!\n"); |
| 490 | eth_halt(); |
| 491 | return -1; |
| 492 | } |
| 493 | } |
| 494 | printf("--------receive %d bytes, matched\n", length); |
| 495 | bfin_EMAC_halt(NULL); |
| 496 | return 0; |
| 497 | } |
| 498 | #endif |