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