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