wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <malloc.h> |
| 10 | #include <commproc.h> |
| 11 | #include <net.h> |
| 12 | #include <command.h> |
| 13 | |
Wolfgang Denk | d87080b | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 16 | #undef ET_DEBUG |
| 17 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 18 | #if defined(CONFIG_CMD_NET) && \ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 19 | (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2)) |
| 20 | |
| 21 | /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */ |
| 22 | #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2) |
| 23 | #define CONFIG_ETHER_ON_FEC1 1 |
| 24 | #endif |
| 25 | |
| 26 | /* define WANT_MII when MII support is required */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 27 | #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 28 | #define WANT_MII |
| 29 | #else |
| 30 | #undef WANT_MII |
| 31 | #endif |
| 32 | |
| 33 | #if defined(WANT_MII) |
| 34 | #include <miiphy.h> |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 35 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 36 | #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 37 | #error "CONFIG_MII has to be defined!" |
| 38 | #endif |
| 39 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | #if defined(CONFIG_RMII) && !defined(WANT_MII) |
| 43 | #error RMII support is unusable without a working PHY. |
| 44 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | #ifdef CONFIG_SYS_DISCOVER_PHY |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 47 | static int mii_discover_phy(struct eth_device *dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 50 | int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg); |
| 51 | int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 52 | u16 value); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 53 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 54 | static struct ether_fcc_info_s |
| 55 | { |
| 56 | int ether_index; |
| 57 | int fecp_offset; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 58 | int phy_addr; |
| 59 | int actual_phy_addr; |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 60 | int initialized; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 61 | } |
| 62 | ether_fcc_info[] = { |
| 63 | #if defined(CONFIG_ETHER_ON_FEC1) |
| 64 | { |
| 65 | 0, |
| 66 | offsetof(immap_t, im_cpm.cp_fec1), |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 67 | #if defined(CONFIG_FEC1_PHY) |
| 68 | CONFIG_FEC1_PHY, |
| 69 | #else |
| 70 | -1, /* discover */ |
| 71 | #endif |
| 72 | -1, |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 73 | 0, |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 74 | |
| 75 | }, |
| 76 | #endif |
| 77 | #if defined(CONFIG_ETHER_ON_FEC2) |
| 78 | { |
| 79 | 1, |
| 80 | offsetof(immap_t, im_cpm.cp_fec2), |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 81 | #if defined(CONFIG_FEC2_PHY) |
| 82 | CONFIG_FEC2_PHY, |
| 83 | #else |
| 84 | -1, |
| 85 | #endif |
| 86 | -1, |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 87 | 0, |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 88 | }, |
| 89 | #endif |
| 90 | }; |
| 91 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | /* Ethernet Transmit and Receive Buffers */ |
| 93 | #define DBUF_LENGTH 1520 |
| 94 | |
| 95 | #define TX_BUF_CNT 2 |
| 96 | |
| 97 | #define TOUT_LOOP 100 |
| 98 | |
| 99 | #define PKT_MAXBUF_SIZE 1518 |
| 100 | #define PKT_MINBUF_SIZE 64 |
| 101 | #define PKT_MAXBLR_SIZE 1520 |
| 102 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 103 | #ifdef __GNUC__ |
| 104 | static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8))); |
| 105 | #else |
| 106 | #error txbuf must be aligned. |
| 107 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | |
| 109 | static uint rxIdx; /* index of the current RX buffer */ |
| 110 | static uint txIdx; /* index of the current TX buffer */ |
| 111 | |
| 112 | /* |
| 113 | * FEC Ethernet Tx and Rx buffer descriptors allocated at the |
| 114 | * immr->udata_bd address on Dual-Port RAM |
| 115 | * Provide for Double Buffering |
| 116 | */ |
| 117 | |
| 118 | typedef volatile struct CommonBufferDescriptor { |
| 119 | cbd_t rxbd[PKTBUFSRX]; /* Rx BD */ |
| 120 | cbd_t txbd[TX_BUF_CNT]; /* Tx BD */ |
| 121 | } RTXBD; |
| 122 | |
| 123 | static RTXBD *rtx = NULL; |
| 124 | |
Wolfgang Denk | 1b10d07 | 2012-05-20 21:14:54 +0000 | [diff] [blame] | 125 | static int fec_send(struct eth_device *dev, void *packet, int length); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | static int fec_recv(struct eth_device* dev); |
| 127 | static int fec_init(struct eth_device* dev, bd_t * bd); |
| 128 | static void fec_halt(struct eth_device* dev); |
Wolfgang Denk | 08e99e1 | 2008-01-13 02:19:13 +0100 | [diff] [blame] | 129 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 130 | static void __mii_init(void); |
Wolfgang Denk | 08e99e1 | 2008-01-13 02:19:13 +0100 | [diff] [blame] | 131 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 132 | |
| 133 | int fec_initialize(bd_t *bis) |
| 134 | { |
| 135 | struct eth_device* dev; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 136 | struct ether_fcc_info_s *efis; |
| 137 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 138 | |
Robert P. J. Day | b7707b0 | 2016-05-23 06:49:21 -0400 | [diff] [blame] | 139 | for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 141 | dev = malloc(sizeof(*dev)); |
| 142 | if (dev == NULL) |
| 143 | hang(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 144 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 145 | memset(dev, 0, sizeof(*dev)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 146 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 147 | /* for FEC1 make sure that the name of the interface is the same |
| 148 | as the old one for compatibility reasons */ |
| 149 | if (i == 0) { |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 150 | strcpy(dev->name, "FEC"); |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 151 | } else { |
Heiko Schocher | 48690d8 | 2010-07-20 17:45:02 +0200 | [diff] [blame] | 152 | sprintf (dev->name, "FEC%d", |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 153 | ether_fcc_info[i].ether_index + 1); |
| 154 | } |
| 155 | |
| 156 | efis = ðer_fcc_info[i]; |
| 157 | |
| 158 | /* |
| 159 | * reset actual phy addr |
| 160 | */ |
| 161 | efis->actual_phy_addr = -1; |
| 162 | |
| 163 | dev->priv = efis; |
| 164 | dev->init = fec_init; |
| 165 | dev->halt = fec_halt; |
| 166 | dev->send = fec_send; |
| 167 | dev->recv = fec_recv; |
| 168 | |
| 169 | eth_register(dev); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 170 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 171 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 172 | int retval; |
| 173 | struct mii_dev *mdiodev = mdio_alloc(); |
| 174 | if (!mdiodev) |
| 175 | return -ENOMEM; |
| 176 | strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
| 177 | mdiodev->read = fec8xx_miiphy_read; |
| 178 | mdiodev->write = fec8xx_miiphy_write; |
| 179 | |
| 180 | retval = mdio_register(mdiodev); |
| 181 | if (retval < 0) |
| 182 | return retval; |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 183 | #endif |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 184 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 185 | return 1; |
| 186 | } |
| 187 | |
Wolfgang Denk | 1b10d07 | 2012-05-20 21:14:54 +0000 | [diff] [blame] | 188 | static int fec_send(struct eth_device *dev, void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 189 | { |
| 190 | int j, rc; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 191 | struct ether_fcc_info_s *efis = dev->priv; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 192 | volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 193 | |
| 194 | /* section 16.9.23.3 |
| 195 | * Wait for ready |
| 196 | */ |
| 197 | j = 0; |
| 198 | while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) { |
| 199 | udelay(1); |
| 200 | j++; |
| 201 | } |
| 202 | if (j>=TOUT_LOOP) { |
| 203 | printf("TX not ready\n"); |
| 204 | } |
| 205 | |
| 206 | rtx->txbd[txIdx].cbd_bufaddr = (uint)packet; |
| 207 | rtx->txbd[txIdx].cbd_datlen = length; |
| 208 | rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST; |
| 209 | __asm__ ("eieio"); |
| 210 | |
| 211 | /* Activate transmit Buffer Descriptor polling */ |
| 212 | fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */ |
| 213 | |
| 214 | j = 0; |
| 215 | while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 216 | udelay(1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 217 | j++; |
| 218 | } |
| 219 | if (j>=TOUT_LOOP) { |
| 220 | printf("TX timeout\n"); |
| 221 | } |
| 222 | #ifdef ET_DEBUG |
| 223 | printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n", |
| 224 | __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc, |
| 225 | (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2); |
| 226 | #endif |
| 227 | /* return only status bits */; |
| 228 | rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS); |
| 229 | |
| 230 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 231 | |
| 232 | return rc; |
| 233 | } |
| 234 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 235 | static int fec_recv (struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 236 | { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 237 | struct ether_fcc_info_s *efis = dev->priv; |
| 238 | volatile fec_t *fecp = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 239 | (volatile fec_t *) (CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 240 | int length; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 242 | for (;;) { |
| 243 | /* section 16.9.23.2 */ |
| 244 | if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) { |
| 245 | length = -1; |
| 246 | break; /* nothing received - leave for() loop */ |
| 247 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 248 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 249 | length = rtx->rxbd[rxIdx].cbd_datlen; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 250 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 251 | if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 252 | #ifdef ET_DEBUG |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 253 | printf ("%s[%d] err: %x\n", |
| 254 | __FUNCTION__, __LINE__, |
| 255 | rtx->rxbd[rxIdx].cbd_sc); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 256 | #endif |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 257 | } else { |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 258 | uchar *rx = net_rx_packets[rxIdx]; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 259 | |
| 260 | length -= 4; |
| 261 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 262 | #if defined(CONFIG_CMD_CDP) |
Joe Hershberger | 0adb5b7 | 2015-04-08 01:41:04 -0500 | [diff] [blame] | 263 | if ((rx[0] & 1) != 0 && |
| 264 | memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 && |
| 265 | !is_cdp_packet((uchar *)rx)) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 266 | rx = NULL; |
| 267 | #endif |
| 268 | /* |
| 269 | * Pass the packet up to the protocol layers. |
| 270 | */ |
| 271 | if (rx != NULL) |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 272 | net_process_received_packet(rx, length); |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Give the buffer back to the FEC. */ |
| 276 | rtx->rxbd[rxIdx].cbd_datlen = 0; |
| 277 | |
| 278 | /* wrap around buffer index when necessary */ |
| 279 | if ((rxIdx + 1) >= PKTBUFSRX) { |
| 280 | rtx->rxbd[PKTBUFSRX - 1].cbd_sc = |
| 281 | (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); |
| 282 | rxIdx = 0; |
| 283 | } else { |
| 284 | rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY; |
| 285 | rxIdx++; |
| 286 | } |
| 287 | |
| 288 | __asm__ ("eieio"); |
| 289 | |
| 290 | /* Try to fill Buffer Descriptors */ |
| 291 | fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 292 | } |
| 293 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 294 | return length; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /************************************************************** |
| 298 | * |
| 299 | * FEC Ethernet Initialization Routine |
| 300 | * |
| 301 | *************************************************************/ |
| 302 | |
| 303 | #define FEC_ECNTRL_PINMUX 0x00000004 |
| 304 | #define FEC_ECNTRL_ETHER_EN 0x00000002 |
| 305 | #define FEC_ECNTRL_RESET 0x00000001 |
| 306 | |
| 307 | #define FEC_RCNTRL_BC_REJ 0x00000010 |
| 308 | #define FEC_RCNTRL_PROM 0x00000008 |
| 309 | #define FEC_RCNTRL_MII_MODE 0x00000004 |
| 310 | #define FEC_RCNTRL_DRT 0x00000002 |
| 311 | #define FEC_RCNTRL_LOOP 0x00000001 |
| 312 | |
| 313 | #define FEC_TCNTRL_FDEN 0x00000004 |
| 314 | #define FEC_TCNTRL_HBC 0x00000002 |
| 315 | #define FEC_TCNTRL_GTS 0x00000001 |
| 316 | |
| 317 | #define FEC_RESET_DELAY 50 |
| 318 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 319 | #if defined(CONFIG_RMII) |
| 320 | |
| 321 | static inline void fec_10Mbps(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 323 | struct ether_fcc_info_s *efis = dev->priv; |
| 324 | int fecidx = efis->ether_index; |
| 325 | uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 326 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 327 | if ((unsigned int)fecidx >= 2) |
| 328 | hang(); |
| 329 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 330 | ((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_cptr |= mask; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static inline void fec_100Mbps(struct eth_device *dev) |
| 334 | { |
| 335 | struct ether_fcc_info_s *efis = dev->priv; |
| 336 | int fecidx = efis->ether_index; |
| 337 | uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; |
| 338 | |
| 339 | if ((unsigned int)fecidx >= 2) |
| 340 | hang(); |
| 341 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 342 | ((volatile immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_cptr &= ~mask; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | #endif |
| 346 | |
| 347 | static inline void fec_full_duplex(struct eth_device *dev) |
| 348 | { |
| 349 | struct ether_fcc_info_s *efis = dev->priv; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 350 | volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 351 | |
| 352 | fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT; |
| 353 | fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */ |
| 354 | } |
| 355 | |
| 356 | static inline void fec_half_duplex(struct eth_device *dev) |
| 357 | { |
| 358 | struct ether_fcc_info_s *efis = dev->priv; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 359 | volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 360 | |
| 361 | fecp->fec_r_cntrl |= FEC_RCNTRL_DRT; |
| 362 | fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */ |
| 363 | } |
| 364 | |
| 365 | static void fec_pin_init(int fecidx) |
| 366 | { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 367 | bd_t *bd = gd->bd; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 368 | volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * Set MII speed to 2.5 MHz or slightly below. |
Wolfgang Denk | 90357f1 | 2011-11-04 15:55:21 +0000 | [diff] [blame] | 372 | * |
| 373 | * According to the MPC860T (Rev. D) Fast ethernet controller user |
| 374 | * manual (6.2.14), |
| 375 | * the MII management interface clock must be less than or equal |
| 376 | * to 2.5 MHz. |
| 377 | * This MDC frequency is equal to system clock / (2 * MII_SPEED). |
| 378 | * Then MII_SPEED = system_clock / 2 * 2,5 MHz. |
Markus Klotzbuecher | d6cc73e | 2006-07-12 09:08:36 +0200 | [diff] [blame] | 379 | * |
| 380 | * All MII configuration is done via FEC1 registers: |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 381 | */ |
Markus Klotzbuecher | d6cc73e | 2006-07-12 09:08:36 +0200 | [diff] [blame] | 382 | immr->im_cpm.cp_fec1.fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 383 | |
wdenk | 1114257 | 2004-06-06 21:35:06 +0000 | [diff] [blame] | 384 | #if defined(CONFIG_MPC885_FAMILY) && defined(WANT_MII) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 385 | /* use MDC for MII */ |
| 386 | immr->im_ioport.iop_pdpar |= 0x0080; |
| 387 | immr->im_ioport.iop_pddir &= ~0x0080; |
| 388 | #endif |
| 389 | |
| 390 | if (fecidx == 0) { |
| 391 | #if defined(CONFIG_ETHER_ON_FEC1) |
| 392 | |
wdenk | 1114257 | 2004-06-06 21:35:06 +0000 | [diff] [blame] | 393 | #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 394 | |
| 395 | #if !defined(CONFIG_RMII) |
| 396 | |
| 397 | immr->im_ioport.iop_papar |= 0xf830; |
| 398 | immr->im_ioport.iop_padir |= 0x0830; |
| 399 | immr->im_ioport.iop_padir &= ~0xf000; |
| 400 | |
| 401 | immr->im_cpm.cp_pbpar |= 0x00001001; |
| 402 | immr->im_cpm.cp_pbdir &= ~0x00001001; |
| 403 | |
| 404 | immr->im_ioport.iop_pcpar |= 0x000c; |
| 405 | immr->im_ioport.iop_pcdir &= ~0x000c; |
| 406 | |
| 407 | immr->im_cpm.cp_pepar |= 0x00000003; |
| 408 | immr->im_cpm.cp_pedir |= 0x00000003; |
| 409 | immr->im_cpm.cp_peso &= ~0x00000003; |
| 410 | |
| 411 | immr->im_cpm.cp_cptr &= ~0x00000100; |
| 412 | |
| 413 | #else |
| 414 | |
| 415 | #if !defined(CONFIG_FEC1_PHY_NORXERR) |
| 416 | immr->im_ioport.iop_papar |= 0x1000; |
| 417 | immr->im_ioport.iop_padir &= ~0x1000; |
| 418 | #endif |
| 419 | immr->im_ioport.iop_papar |= 0xe810; |
| 420 | immr->im_ioport.iop_padir |= 0x0810; |
| 421 | immr->im_ioport.iop_padir &= ~0xe000; |
| 422 | |
| 423 | immr->im_cpm.cp_pbpar |= 0x00000001; |
| 424 | immr->im_cpm.cp_pbdir &= ~0x00000001; |
| 425 | |
| 426 | immr->im_cpm.cp_cptr |= 0x00000100; |
| 427 | immr->im_cpm.cp_cptr &= ~0x00000050; |
| 428 | |
| 429 | #endif /* !CONFIG_RMII */ |
| 430 | |
Marek Vasut | 4af5f0f | 2014-10-22 21:34:46 +0200 | [diff] [blame] | 431 | #else |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 432 | /* |
| 433 | * Configure all of port D for MII. |
| 434 | */ |
| 435 | immr->im_ioport.iop_pdpar = 0x1fff; |
| 436 | |
| 437 | /* |
| 438 | * Bits moved from Rev. D onward |
| 439 | */ |
| 440 | if ((get_immr(0) & 0xffff) < 0x0501) |
| 441 | immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */ |
| 442 | else |
| 443 | immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 444 | #endif |
| 445 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 446 | #endif /* CONFIG_ETHER_ON_FEC1 */ |
| 447 | } else if (fecidx == 1) { |
| 448 | |
| 449 | #if defined(CONFIG_ETHER_ON_FEC2) |
| 450 | |
wdenk | 1114257 | 2004-06-06 21:35:06 +0000 | [diff] [blame] | 451 | #if defined(CONFIG_MPC885_FAMILY) /* MPC87x/88x have got 2 FECs and different pinout */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 452 | |
| 453 | #if !defined(CONFIG_RMII) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 454 | immr->im_cpm.cp_pepar |= 0x0003fffc; |
| 455 | immr->im_cpm.cp_pedir |= 0x0003fffc; |
| 456 | immr->im_cpm.cp_peso &= ~0x000087fc; |
| 457 | immr->im_cpm.cp_peso |= 0x00037800; |
| 458 | |
| 459 | immr->im_cpm.cp_cptr &= ~0x00000080; |
| 460 | #else |
| 461 | |
| 462 | #if !defined(CONFIG_FEC2_PHY_NORXERR) |
| 463 | immr->im_cpm.cp_pepar |= 0x00000010; |
| 464 | immr->im_cpm.cp_pedir |= 0x00000010; |
| 465 | immr->im_cpm.cp_peso &= ~0x00000010; |
| 466 | #endif |
| 467 | immr->im_cpm.cp_pepar |= 0x00039620; |
| 468 | immr->im_cpm.cp_pedir |= 0x00039620; |
| 469 | immr->im_cpm.cp_peso |= 0x00031000; |
| 470 | immr->im_cpm.cp_peso &= ~0x00008620; |
| 471 | |
| 472 | immr->im_cpm.cp_cptr |= 0x00000080; |
| 473 | immr->im_cpm.cp_cptr &= ~0x00000028; |
| 474 | #endif /* CONFIG_RMII */ |
| 475 | |
wdenk | 1114257 | 2004-06-06 21:35:06 +0000 | [diff] [blame] | 476 | #endif /* CONFIG_MPC885_FAMILY */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 477 | |
| 478 | #endif /* CONFIG_ETHER_ON_FEC2 */ |
| 479 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 480 | } |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 483 | static int fec_reset(volatile fec_t *fecp) |
| 484 | { |
| 485 | int i; |
| 486 | |
| 487 | /* Whack a reset. |
| 488 | * A delay is required between a reset of the FEC block and |
| 489 | * initialization of other FEC registers because the reset takes |
| 490 | * some time to complete. If you don't delay, subsequent writes |
| 491 | * to FEC registers might get killed by the reset routine which is |
| 492 | * still in progress. |
| 493 | */ |
| 494 | |
| 495 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET; |
| 496 | for (i = 0; |
| 497 | (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY); |
| 498 | ++i) { |
| 499 | udelay (1); |
| 500 | } |
| 501 | if (i == FEC_RESET_DELAY) |
| 502 | return -1; |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 507 | static int fec_init (struct eth_device *dev, bd_t * bd) |
| 508 | { |
| 509 | struct ether_fcc_info_s *efis = dev->priv; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 510 | volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 511 | volatile fec_t *fecp = |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 512 | (volatile fec_t *) (CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 513 | int i; |
| 514 | |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 515 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 516 | /* the MII interface is connected to FEC1 |
| 517 | * so for the miiphy_xxx function to work we must |
| 518 | * call mii_init since fec_halt messes the thing up |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 519 | */ |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 520 | if (efis->ether_index != 0) |
| 521 | __mii_init(); |
| 522 | #endif |
| 523 | |
| 524 | if (fec_reset(fecp) < 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 525 | printf ("FEC_RESET_DELAY timeout\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 526 | |
| 527 | /* We use strictly polling mode only |
| 528 | */ |
| 529 | fecp->fec_imask = 0; |
| 530 | |
| 531 | /* Clear any pending interrupt |
| 532 | */ |
| 533 | fecp->fec_ievent = 0xffc0; |
| 534 | |
| 535 | /* No need to set the IVEC register */ |
| 536 | |
| 537 | /* Set station address |
| 538 | */ |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 539 | #define ea dev->enetaddr |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 540 | fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]); |
| 541 | fecp->fec_addr_high = (ea[4] << 8) | (ea[5]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 542 | #undef ea |
| 543 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 544 | #if defined(CONFIG_CMD_CDP) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 545 | /* |
| 546 | * Turn on multicast address hash table |
| 547 | */ |
| 548 | fecp->fec_hash_table_high = 0xffffffff; |
| 549 | fecp->fec_hash_table_low = 0xffffffff; |
| 550 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 551 | /* Clear multicast address hash table |
| 552 | */ |
| 553 | fecp->fec_hash_table_high = 0; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 554 | fecp->fec_hash_table_low = 0; |
| 555 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 556 | |
| 557 | /* Set maximum receive buffer size. |
| 558 | */ |
| 559 | fecp->fec_r_buff_size = PKT_MAXBLR_SIZE; |
| 560 | |
| 561 | /* Set maximum frame length |
| 562 | */ |
| 563 | fecp->fec_r_hash = PKT_MAXBUF_SIZE; |
| 564 | |
| 565 | /* |
| 566 | * Setup Buffers and Buffer Desriptors |
| 567 | */ |
| 568 | rxIdx = 0; |
| 569 | txIdx = 0; |
| 570 | |
| 571 | if (!rtx) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 572 | #ifdef CONFIG_SYS_ALLOC_DPRAM |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 573 | rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + |
| 574 | dpram_alloc_align (sizeof (RTXBD), 8)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 575 | #else |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 576 | rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 577 | #endif |
| 578 | } |
| 579 | /* |
| 580 | * Setup Receiver Buffer Descriptors (13.14.24.18) |
| 581 | * Settings: |
| 582 | * Empty, Wrap |
| 583 | */ |
| 584 | for (i = 0; i < PKTBUFSRX; i++) { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 585 | rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY; |
| 586 | rtx->rxbd[i].cbd_datlen = 0; /* Reset */ |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 587 | rtx->rxbd[i].cbd_bufaddr = (uint) net_rx_packets[i]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 588 | } |
| 589 | rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP; |
| 590 | |
| 591 | /* |
| 592 | * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19) |
| 593 | * Settings: |
| 594 | * Last, Tx CRC |
| 595 | */ |
| 596 | for (i = 0; i < TX_BUF_CNT; i++) { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 597 | rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC; |
| 598 | rtx->txbd[i].cbd_datlen = 0; /* Reset */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 599 | rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]); |
| 600 | } |
| 601 | rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP; |
| 602 | |
| 603 | /* Set receive and transmit descriptor base |
| 604 | */ |
| 605 | fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]); |
| 606 | fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]); |
| 607 | |
| 608 | /* Enable MII mode |
| 609 | */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 610 | #if 0 /* Full duplex mode */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 611 | fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE; |
| 612 | fecp->fec_x_cntrl = FEC_TCNTRL_FDEN; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 613 | #else /* Half duplex mode */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 614 | fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT; |
| 615 | fecp->fec_x_cntrl = 0; |
| 616 | #endif |
| 617 | |
| 618 | /* Enable big endian and don't care about SDMA FC. |
| 619 | */ |
| 620 | fecp->fec_fun_code = 0x78000000; |
| 621 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 622 | /* |
| 623 | * Setup the pin configuration of the FEC |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 624 | */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 625 | fec_pin_init (efis->ether_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 626 | |
| 627 | rxIdx = 0; |
| 628 | txIdx = 0; |
| 629 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 630 | /* |
| 631 | * Now enable the transmit and receive processing |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 632 | */ |
| 633 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN; |
| 634 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 635 | if (efis->phy_addr == -1) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 636 | #ifdef CONFIG_SYS_DISCOVER_PHY |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 637 | /* |
| 638 | * wait for the PHY to wake up after reset |
| 639 | */ |
| 640 | efis->actual_phy_addr = mii_discover_phy (dev); |
wdenk | 62b4ac9 | 2004-05-05 08:31:53 +0000 | [diff] [blame] | 641 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 642 | if (efis->actual_phy_addr == -1) { |
| 643 | printf ("Unable to discover phy!\n"); |
Ben Warren | 422b1a0 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 644 | return -1; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 645 | } |
wdenk | 62b4ac9 | 2004-05-05 08:31:53 +0000 | [diff] [blame] | 646 | #else |
| 647 | efis->actual_phy_addr = -1; |
| 648 | #endif |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 649 | } else { |
| 650 | efis->actual_phy_addr = efis->phy_addr; |
| 651 | } |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 652 | |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 653 | #if defined(CONFIG_MII) && defined(CONFIG_RMII) |
| 654 | /* |
| 655 | * adapt the RMII speed to the speed of the phy |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 656 | */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 657 | if (miiphy_speed (dev->name, efis->actual_phy_addr) == _100BASET) { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 658 | fec_100Mbps (dev); |
| 659 | } else { |
| 660 | fec_10Mbps (dev); |
| 661 | } |
| 662 | #endif |
| 663 | |
| 664 | #if defined(CONFIG_MII) |
| 665 | /* |
| 666 | * adapt to the half/full speed settings |
| 667 | */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 668 | if (miiphy_duplex (dev->name, efis->actual_phy_addr) == FULL) { |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 669 | fec_full_duplex (dev); |
| 670 | } else { |
| 671 | fec_half_duplex (dev); |
| 672 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 673 | #endif |
| 674 | |
| 675 | /* And last, try to fill Rx Buffer Descriptors */ |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 676 | fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 677 | |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 678 | efis->initialized = 1; |
| 679 | |
Ben Warren | 422b1a0 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 680 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 684 | static void fec_halt(struct eth_device* dev) |
| 685 | { |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 686 | struct ether_fcc_info_s *efis = dev->priv; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 687 | volatile fec_t *fecp = (volatile fec_t *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 688 | int i; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 689 | |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 690 | /* avoid halt if initialized; mii gets stuck otherwise */ |
| 691 | if (!efis->initialized) |
| 692 | return; |
| 693 | |
| 694 | /* Whack a reset. |
| 695 | * A delay is required between a reset of the FEC block and |
| 696 | * initialization of other FEC registers because the reset takes |
| 697 | * some time to complete. If you don't delay, subsequent writes |
| 698 | * to FEC registers might get killed by the reset routine which is |
| 699 | * still in progress. |
| 700 | */ |
| 701 | |
| 702 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET; |
| 703 | for (i = 0; |
| 704 | (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY); |
| 705 | ++i) { |
| 706 | udelay (1); |
| 707 | } |
| 708 | if (i == FEC_RESET_DELAY) { |
| 709 | printf ("FEC_RESET_DELAY timeout\n"); |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | efis->initialized = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 714 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 715 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 716 | #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 717 | |
| 718 | /* Make MII read/write commands for the FEC. |
| 719 | */ |
| 720 | |
| 721 | #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \ |
| 722 | (REG & 0x1f) << 18)) |
| 723 | |
| 724 | #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \ |
| 725 | (REG & 0x1f) << 18) | \ |
| 726 | (VAL & 0xffff)) |
| 727 | |
| 728 | /* Interrupt events/masks. |
| 729 | */ |
| 730 | #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */ |
| 731 | #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */ |
| 732 | #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */ |
| 733 | #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */ |
| 734 | #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */ |
| 735 | #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */ |
| 736 | #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */ |
| 737 | #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */ |
| 738 | #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */ |
| 739 | #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */ |
| 740 | |
| 741 | /* PHY identification |
| 742 | */ |
| 743 | #define PHY_ID_LXT970 0x78100000 /* LXT970 */ |
| 744 | #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */ |
| 745 | #define PHY_ID_82555 0x02a80150 /* Intel 82555 */ |
| 746 | #define PHY_ID_QS6612 0x01814400 /* QS6612 */ |
| 747 | #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */ |
| 748 | #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */ |
| 749 | #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */ |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame] | 750 | #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */ |
Markus Klotzbuecher | b02d017 | 2006-07-12 08:48:24 +0200 | [diff] [blame] | 751 | #define PHY_ID_KSM8995M 0x00221450 /* MICREL KS8995MA */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 752 | |
| 753 | /* send command to phy using mii, wait for result */ |
| 754 | static uint |
| 755 | mii_send(uint mii_cmd) |
| 756 | { |
| 757 | uint mii_reply; |
| 758 | volatile fec_t *ep; |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 759 | int cnt; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 760 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 761 | ep = &(((immap_t *)CONFIG_SYS_IMMR)->im_cpm.cp_fec); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 762 | |
| 763 | ep->fec_mii_data = mii_cmd; /* command to phy */ |
| 764 | |
| 765 | /* wait for mii complete */ |
wdenk | c26e454 | 2004-04-18 10:13:26 +0000 | [diff] [blame] | 766 | cnt = 0; |
| 767 | while (!(ep->fec_ievent & FEC_ENET_MII)) { |
| 768 | if (++cnt > 1000) { |
| 769 | printf("mii_send STUCK!\n"); |
| 770 | break; |
| 771 | } |
| 772 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 773 | mii_reply = ep->fec_mii_data; /* result from phy */ |
| 774 | ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */ |
| 775 | #if 0 |
| 776 | printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n", |
| 777 | __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply); |
| 778 | #endif |
| 779 | return (mii_reply & 0xffff); /* data read from phy */ |
| 780 | } |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 781 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 782 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 783 | #if defined(CONFIG_SYS_DISCOVER_PHY) |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 784 | static int mii_discover_phy(struct eth_device *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 785 | { |
| 786 | #define MAX_PHY_PASSES 11 |
| 787 | uint phyno; |
| 788 | int pass; |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 789 | uint phytype; |
| 790 | int phyaddr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 791 | |
| 792 | phyaddr = -1; /* didn't find a PHY yet */ |
| 793 | for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) { |
| 794 | if (pass > 1) { |
| 795 | /* PHY may need more time to recover from reset. |
| 796 | * The LXT970 needs 50ms typical, no maximum is |
| 797 | * specified, so wait 10ms before try again. |
| 798 | * With 11 passes this gives it 100ms to wake up. |
| 799 | */ |
| 800 | udelay(10000); /* wait 10ms */ |
| 801 | } |
| 802 | for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) { |
Mike Frysinger | 8ef583a | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 803 | phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 804 | #ifdef ET_DEBUG |
| 805 | printf("PHY type 0x%x pass %d type ", phytype, pass); |
| 806 | #endif |
| 807 | if (phytype != 0xffff) { |
| 808 | phyaddr = phyno; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 809 | phytype |= mii_send(mk_mii_read(phyno, |
Mike Frysinger | 8ef583a | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 810 | MII_PHYSID1)) << 16; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 811 | |
| 812 | #ifdef ET_DEBUG |
| 813 | printf("PHY @ 0x%x pass %d type ",phyno,pass); |
| 814 | switch (phytype & 0xfffffff0) { |
| 815 | case PHY_ID_LXT970: |
| 816 | printf("LXT970\n"); |
| 817 | break; |
| 818 | case PHY_ID_LXT971: |
| 819 | printf("LXT971\n"); |
| 820 | break; |
| 821 | case PHY_ID_82555: |
| 822 | printf("82555\n"); |
| 823 | break; |
| 824 | case PHY_ID_QS6612: |
| 825 | printf("QS6612\n"); |
| 826 | break; |
| 827 | case PHY_ID_AMD79C784: |
| 828 | printf("AMD79C784\n"); |
| 829 | break; |
| 830 | case PHY_ID_LSI80225B: |
| 831 | printf("LSI L80225/B\n"); |
| 832 | break; |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame] | 833 | case PHY_ID_DM9161: |
| 834 | printf("Davicom DM9161\n"); |
| 835 | break; |
Markus Klotzbuecher | b02d017 | 2006-07-12 08:48:24 +0200 | [diff] [blame] | 836 | case PHY_ID_KSM8995M: |
| 837 | printf("MICREL KS8995M\n"); |
| 838 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 839 | default: |
| 840 | printf("0x%08x\n", phytype); |
| 841 | break; |
| 842 | } |
| 843 | #endif |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | if (phyaddr < 0) { |
| 848 | printf("No PHY device found.\n"); |
| 849 | } |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 850 | return phyaddr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 851 | } |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 852 | #endif /* CONFIG_SYS_DISCOVER_PHY */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 853 | |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 854 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 855 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 856 | /**************************************************************************** |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 857 | * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 858 | * This function is a subset of eth_init |
| 859 | **************************************************************************** |
| 860 | */ |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 861 | static void __mii_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 862 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 863 | volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 864 | volatile fec_t *fecp = &(immr->im_cpm.cp_fec); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 865 | |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 866 | if (fec_reset(fecp) < 0) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 867 | printf ("FEC_RESET_DELAY timeout\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 868 | |
| 869 | /* We use strictly polling mode only |
| 870 | */ |
| 871 | fecp->fec_imask = 0; |
| 872 | |
| 873 | /* Clear any pending interrupt |
| 874 | */ |
| 875 | fecp->fec_ievent = 0xffc0; |
| 876 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 877 | /* Now enable the transmit and receive processing |
| 878 | */ |
| 879 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN; |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | void mii_init (void) |
| 883 | { |
| 884 | int i; |
| 885 | |
| 886 | __mii_init(); |
| 887 | |
| 888 | /* Setup the pin configuration of the FEC(s) |
| 889 | */ |
Robert P. J. Day | b7707b0 | 2016-05-23 06:49:21 -0400 | [diff] [blame] | 890 | for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) |
Guennadi Liakhovetski | d197ffd | 2007-11-29 21:15:56 +0100 | [diff] [blame] | 891 | fec_pin_init(ether_fcc_info[i].ether_index); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 892 | } |
wdenk | a6ab4bf | 2004-04-15 21:31:56 +0000 | [diff] [blame] | 893 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 894 | /***************************************************************************** |
| 895 | * Read and write a MII PHY register, routines used by MII Utilities |
| 896 | * |
| 897 | * FIXME: These routines are expected to return 0 on success, but mii_send |
| 898 | * does _not_ return an error code. Maybe 0xFFFF means error, i.e. |
| 899 | * no PHY connected... |
| 900 | * For now always return 0. |
| 901 | * FIXME: These routines only work after calling eth_init() at least once! |
| 902 | * Otherwise they hang in mii_send() !!! Sorry! |
| 903 | *****************************************************************************/ |
| 904 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 905 | int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 906 | { |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 907 | unsigned short value = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 908 | short rdreg; /* register working value */ |
| 909 | |
| 910 | #ifdef MII_DEBUG |
| 911 | printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr); |
| 912 | #endif |
| 913 | rdreg = mii_send(mk_mii_read(addr, reg)); |
| 914 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 915 | value = rdreg; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 916 | #ifdef MII_DEBUG |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 917 | printf ("0x%04x\n", value); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 918 | #endif |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 919 | return value; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Joe Hershberger | 5a49f17 | 2016-08-08 11:28:38 -0500 | [diff] [blame^] | 922 | int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 923 | u16 value) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 924 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 925 | #ifdef MII_DEBUG |
| 926 | printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr); |
| 927 | #endif |
Wolfgang Denk | 90357f1 | 2011-11-04 15:55:21 +0000 | [diff] [blame] | 928 | (void)mii_send(mk_mii_write(addr, reg, value)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 929 | |
| 930 | #ifdef MII_DEBUG |
| 931 | printf ("0x%04x\n", value); |
| 932 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 933 | return 0; |
| 934 | } |
Jon Loeliger | 4431283 | 2007-07-09 19:06:00 -0500 | [diff] [blame] | 935 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 936 | |
Jon Loeliger | 068b60a | 2007-07-10 10:27:39 -0500 | [diff] [blame] | 937 | #endif |