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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <malloc.h> |
| 26 | #include <commproc.h> |
| 27 | #include <net.h> |
| 28 | #include <command.h> |
| 29 | |
| 30 | #undef ET_DEBUG |
| 31 | |
| 32 | #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET) |
| 33 | |
| 34 | #ifdef CFG_DISCOVER_PHY |
| 35 | #include <miiphy.h> |
| 36 | static void mii_discover_phy(void); |
| 37 | #endif |
| 38 | |
| 39 | /* Ethernet Transmit and Receive Buffers */ |
| 40 | #define DBUF_LENGTH 1520 |
| 41 | |
| 42 | #define TX_BUF_CNT 2 |
| 43 | |
| 44 | #define TOUT_LOOP 100 |
| 45 | |
| 46 | #define PKT_MAXBUF_SIZE 1518 |
| 47 | #define PKT_MINBUF_SIZE 64 |
| 48 | #define PKT_MAXBLR_SIZE 1520 |
| 49 | |
| 50 | |
| 51 | static char txbuf[DBUF_LENGTH]; |
| 52 | |
| 53 | static uint rxIdx; /* index of the current RX buffer */ |
| 54 | static uint txIdx; /* index of the current TX buffer */ |
| 55 | |
| 56 | /* |
| 57 | * FEC Ethernet Tx and Rx buffer descriptors allocated at the |
| 58 | * immr->udata_bd address on Dual-Port RAM |
| 59 | * Provide for Double Buffering |
| 60 | */ |
| 61 | |
| 62 | typedef volatile struct CommonBufferDescriptor { |
| 63 | cbd_t rxbd[PKTBUFSRX]; /* Rx BD */ |
| 64 | cbd_t txbd[TX_BUF_CNT]; /* Tx BD */ |
| 65 | } RTXBD; |
| 66 | |
| 67 | static RTXBD *rtx = NULL; |
| 68 | |
| 69 | static int fec_send(struct eth_device* dev, volatile void *packet, int length); |
| 70 | static int fec_recv(struct eth_device* dev); |
| 71 | static int fec_init(struct eth_device* dev, bd_t * bd); |
| 72 | static void fec_halt(struct eth_device* dev); |
| 73 | |
| 74 | int fec_initialize(bd_t *bis) |
| 75 | { |
| 76 | struct eth_device* dev; |
| 77 | |
| 78 | dev = (struct eth_device*) malloc(sizeof *dev); |
wdenk | 7f6c2cb | 2002-11-10 22:06:23 +0000 | [diff] [blame] | 79 | memset(dev, 0, sizeof *dev); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | |
| 81 | sprintf(dev->name, "FEC ETHERNET"); |
| 82 | dev->iobase = 0; |
| 83 | dev->priv = 0; |
| 84 | dev->init = fec_init; |
| 85 | dev->halt = fec_halt; |
| 86 | dev->send = fec_send; |
| 87 | dev->recv = fec_recv; |
| 88 | |
| 89 | eth_register(dev); |
| 90 | |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | static int fec_send(struct eth_device* dev, volatile void *packet, int length) |
| 95 | { |
| 96 | int j, rc; |
| 97 | volatile immap_t *immr = (immap_t *) CFG_IMMR; |
| 98 | volatile fec_t *fecp = &(immr->im_cpm.cp_fec); |
| 99 | |
| 100 | /* section 16.9.23.3 |
| 101 | * Wait for ready |
| 102 | */ |
| 103 | j = 0; |
| 104 | while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) { |
| 105 | udelay(1); |
| 106 | j++; |
| 107 | } |
| 108 | if (j>=TOUT_LOOP) { |
| 109 | printf("TX not ready\n"); |
| 110 | } |
| 111 | |
| 112 | rtx->txbd[txIdx].cbd_bufaddr = (uint)packet; |
| 113 | rtx->txbd[txIdx].cbd_datlen = length; |
| 114 | rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST; |
| 115 | __asm__ ("eieio"); |
| 116 | |
| 117 | /* Activate transmit Buffer Descriptor polling */ |
| 118 | fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */ |
| 119 | |
| 120 | j = 0; |
| 121 | while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) { |
| 122 | #if defined(CONFIG_ICU862) |
| 123 | udelay(10); |
| 124 | #else |
| 125 | udelay(1); |
| 126 | #endif |
| 127 | j++; |
| 128 | } |
| 129 | if (j>=TOUT_LOOP) { |
| 130 | printf("TX timeout\n"); |
| 131 | } |
| 132 | #ifdef ET_DEBUG |
| 133 | printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n", |
| 134 | __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc, |
| 135 | (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2); |
| 136 | #endif |
| 137 | /* return only status bits */; |
| 138 | rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS); |
| 139 | |
| 140 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 141 | |
| 142 | return rc; |
| 143 | } |
| 144 | |
| 145 | static int fec_recv(struct eth_device* dev) |
| 146 | { |
| 147 | int length; |
| 148 | volatile immap_t *immr = (immap_t *) CFG_IMMR; |
| 149 | volatile fec_t *fecp = &(immr->im_cpm.cp_fec); |
| 150 | |
| 151 | for (;;) { |
| 152 | /* section 16.9.23.2 */ |
| 153 | if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) { |
| 154 | length = -1; |
| 155 | break; /* nothing received - leave for() loop */ |
| 156 | } |
| 157 | |
| 158 | length = rtx->rxbd[rxIdx].cbd_datlen; |
| 159 | |
| 160 | if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) { |
| 161 | #ifdef ET_DEBUG |
| 162 | printf("%s[%d] err: %x\n", |
| 163 | __FUNCTION__,__LINE__,rtx->rxbd[rxIdx].cbd_sc); |
| 164 | #endif |
| 165 | } else { |
| 166 | /* Pass the packet up to the protocol layers. */ |
| 167 | NetReceive(NetRxPackets[rxIdx], length - 4); |
| 168 | } |
| 169 | |
| 170 | /* Give the buffer back to the FEC. */ |
| 171 | rtx->rxbd[rxIdx].cbd_datlen = 0; |
| 172 | |
| 173 | /* wrap around buffer index when necessary */ |
| 174 | if ((rxIdx + 1) >= PKTBUFSRX) { |
| 175 | rtx->rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); |
| 176 | rxIdx = 0; |
| 177 | } else { |
| 178 | rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY; |
| 179 | rxIdx++; |
| 180 | } |
| 181 | |
| 182 | __asm__ ("eieio"); |
| 183 | |
| 184 | /* Try to fill Buffer Descriptors */ |
| 185 | fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */ |
| 186 | } |
| 187 | |
| 188 | return length; |
| 189 | } |
| 190 | |
| 191 | /************************************************************** |
| 192 | * |
| 193 | * FEC Ethernet Initialization Routine |
| 194 | * |
| 195 | *************************************************************/ |
| 196 | |
| 197 | #define FEC_ECNTRL_PINMUX 0x00000004 |
| 198 | #define FEC_ECNTRL_ETHER_EN 0x00000002 |
| 199 | #define FEC_ECNTRL_RESET 0x00000001 |
| 200 | |
| 201 | #define FEC_RCNTRL_BC_REJ 0x00000010 |
| 202 | #define FEC_RCNTRL_PROM 0x00000008 |
| 203 | #define FEC_RCNTRL_MII_MODE 0x00000004 |
| 204 | #define FEC_RCNTRL_DRT 0x00000002 |
| 205 | #define FEC_RCNTRL_LOOP 0x00000001 |
| 206 | |
| 207 | #define FEC_TCNTRL_FDEN 0x00000004 |
| 208 | #define FEC_TCNTRL_HBC 0x00000002 |
| 209 | #define FEC_TCNTRL_GTS 0x00000001 |
| 210 | |
| 211 | #define FEC_RESET_DELAY 50 |
| 212 | |
| 213 | static int fec_init(struct eth_device* dev, bd_t * bd) |
| 214 | { |
| 215 | |
| 216 | int i; |
| 217 | volatile immap_t *immr = (immap_t *) CFG_IMMR; |
| 218 | volatile fec_t *fecp = &(immr->im_cpm.cp_fec); |
| 219 | |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 220 | #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */ |
| 221 | #if defined(CONFIG_DUET_ADS) |
| 222 | *(vu_char *)BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST); |
| 223 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 224 | /* configure FADS for fast (FEC) ethernet, half-duplex */ |
| 225 | /* The LXT970 needs about 50ms to recover from reset, so |
| 226 | * wait for it by discovering the PHY before leaving eth_init(). |
| 227 | */ |
| 228 | { |
| 229 | volatile uint *bcsr4 = (volatile uint *) BCSR4; |
| 230 | *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1)) |
| 231 | | (BCSR4_FETHCFG0 | BCSR4_FETHFDE | BCSR4_FETHRST); |
| 232 | |
| 233 | /* reset the LXT970 PHY */ |
| 234 | *bcsr4 &= ~BCSR4_FETHRST; |
| 235 | udelay (10); |
| 236 | *bcsr4 |= BCSR4_FETHRST; |
| 237 | udelay (10); |
| 238 | } |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 239 | #endif /* CONFIG_DUET_ADS */ |
| 240 | #endif /* CONFIG_FADS */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 241 | /* Whack a reset. |
| 242 | * A delay is required between a reset of the FEC block and |
| 243 | * initialization of other FEC registers because the reset takes |
| 244 | * some time to complete. If you don't delay, subsequent writes |
| 245 | * to FEC registers might get killed by the reset routine which is |
| 246 | * still in progress. |
| 247 | */ |
| 248 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET; |
| 249 | for (i = 0; |
| 250 | (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY); |
| 251 | ++i) { |
| 252 | udelay (1); |
| 253 | } |
| 254 | if (i == FEC_RESET_DELAY) { |
| 255 | printf ("FEC_RESET_DELAY timeout\n"); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* We use strictly polling mode only |
| 260 | */ |
| 261 | fecp->fec_imask = 0; |
| 262 | |
| 263 | /* Clear any pending interrupt |
| 264 | */ |
| 265 | fecp->fec_ievent = 0xffc0; |
| 266 | |
| 267 | /* No need to set the IVEC register */ |
| 268 | |
| 269 | /* Set station address |
| 270 | */ |
| 271 | #define ea eth_get_dev()->enetaddr |
| 272 | fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | |
| 273 | (ea[2] << 8) | (ea[3] ) ; |
| 274 | fecp->fec_addr_high = (ea[4] << 8) | (ea[5] ) ; |
| 275 | #undef ea |
| 276 | |
| 277 | /* Clear multicast address hash table |
| 278 | */ |
| 279 | fecp->fec_hash_table_high = 0; |
| 280 | fecp->fec_hash_table_low = 0; |
| 281 | |
| 282 | /* Set maximum receive buffer size. |
| 283 | */ |
| 284 | fecp->fec_r_buff_size = PKT_MAXBLR_SIZE; |
| 285 | |
| 286 | /* Set maximum frame length |
| 287 | */ |
| 288 | fecp->fec_r_hash = PKT_MAXBUF_SIZE; |
| 289 | |
| 290 | /* |
| 291 | * Setup Buffers and Buffer Desriptors |
| 292 | */ |
| 293 | rxIdx = 0; |
| 294 | txIdx = 0; |
| 295 | |
| 296 | if (!rtx) { |
| 297 | #ifdef CFG_ALLOC_DPRAM |
| 298 | rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + dpram_alloc_align(sizeof(RTXBD),8)); |
| 299 | #else |
| 300 | rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE); |
| 301 | #endif |
| 302 | } |
| 303 | /* |
| 304 | * Setup Receiver Buffer Descriptors (13.14.24.18) |
| 305 | * Settings: |
| 306 | * Empty, Wrap |
| 307 | */ |
| 308 | for (i = 0; i < PKTBUFSRX; i++) { |
| 309 | rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY; |
| 310 | rtx->rxbd[i].cbd_datlen = 0; /* Reset */ |
| 311 | rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i]; |
| 312 | } |
| 313 | rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP; |
| 314 | |
| 315 | /* |
| 316 | * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19) |
| 317 | * Settings: |
| 318 | * Last, Tx CRC |
| 319 | */ |
| 320 | for (i = 0; i < TX_BUF_CNT; i++) { |
| 321 | rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC; |
| 322 | rtx->txbd[i].cbd_datlen = 0; /* Reset */ |
| 323 | rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]); |
| 324 | } |
| 325 | rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP; |
| 326 | |
| 327 | /* Set receive and transmit descriptor base |
| 328 | */ |
| 329 | fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]); |
| 330 | fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]); |
| 331 | |
| 332 | /* Enable MII mode |
| 333 | */ |
| 334 | #if 0 /* Full duplex mode */ |
| 335 | fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE; |
| 336 | fecp->fec_x_cntrl = FEC_TCNTRL_FDEN; |
| 337 | #else /* Half duplex mode */ |
| 338 | fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT; |
| 339 | fecp->fec_x_cntrl = 0; |
| 340 | #endif |
| 341 | |
| 342 | /* Enable big endian and don't care about SDMA FC. |
| 343 | */ |
| 344 | fecp->fec_fun_code = 0x78000000; |
| 345 | |
| 346 | /* Set MII speed to 2.5 MHz or slightly below. |
| 347 | * According to the MPC860T (Rev. D) Fast ethernet controller user |
| 348 | * manual (6.2.14), |
| 349 | * the MII management interface clock must be less than or equal |
| 350 | * to 2.5 MHz. |
| 351 | * This MDC frequency is equal to system clock / (2 * MII_SPEED). |
| 352 | * Then MII_SPEED = system_clock / 2 * 2,5 Mhz. |
| 353 | */ |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 354 | fecp->fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 355 | |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 356 | #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */ |
| 357 | immr->im_ioport.iop_papar |= 0xf830; |
| 358 | immr->im_ioport.iop_padir |= 0x0830; |
| 359 | immr->im_ioport.iop_padir &= ~0xf000; |
| 360 | immr->im_cpm.cp_pbpar |= 0x00001001; |
| 361 | immr->im_cpm.cp_pbdir &= ~0x00001001; |
| 362 | immr->im_ioport.iop_pcpar |= 0x000c; |
| 363 | immr->im_ioport.iop_pcdir &= ~0x000c; |
| 364 | immr->im_ioport.iop_pdpar |= 0x0080; |
| 365 | immr->im_ioport.iop_pddir &= ~0x0080; |
| 366 | immr->im_cpm.cp_pepar |= 0x00000003; |
| 367 | immr->im_cpm.cp_pedir |= 0x00000003; |
| 368 | immr->im_cpm.cp_peso &= ~0x00000003; |
| 369 | #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 370 | /* Configure all of port D for MII. |
| 371 | */ |
| 372 | immr->im_ioport.iop_pdpar = 0x1fff; |
| 373 | |
| 374 | /* Bits moved from Rev. D onward */ |
| 375 | if ((get_immr (0) & 0xffff) < 0x0501) { |
| 376 | immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */ |
| 377 | } else { |
| 378 | immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */ |
| 379 | } |
| 380 | #else |
| 381 | /* Configure port A for MII. |
| 382 | */ |
| 383 | |
| 384 | #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY) |
| 385 | |
| 386 | /* On the ICU862 board the MII-MDC pin is routed to PD8 pin |
| 387 | * of CPU, so for this board we need to configure Utopia and |
| 388 | * enable PD8 to MII-MDC function */ |
| 389 | immr->im_ioport.iop_pdpar |= 0x4080; |
| 390 | #endif |
| 391 | |
| 392 | /* Has Utopia been configured? */ |
| 393 | if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) { |
| 394 | /* |
| 395 | * YES - Use MUXED mode for UTOPIA bus. |
| 396 | * This frees Port A for use by MII (see 862UM table 41-6). |
| 397 | */ |
| 398 | immr->im_ioport.utmode &= ~0x80; |
| 399 | } else { |
| 400 | /* |
| 401 | * NO - set SPLIT mode for UTOPIA bus. |
| 402 | * |
| 403 | * This doesn't really effect UTOPIA (which isn't |
| 404 | * enabled anyway) but just tells the 862 |
| 405 | * to use port A for MII (see 862UM table 41-6). |
| 406 | */ |
| 407 | immr->im_ioport.utmode |= 0x80; |
| 408 | } |
| 409 | #endif /* !defined(CONFIG_ICU862) */ |
| 410 | |
| 411 | rxIdx = 0; |
| 412 | txIdx = 0; |
| 413 | |
| 414 | /* Now enable the transmit and receive processing |
| 415 | */ |
| 416 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN; |
| 417 | |
| 418 | #ifdef CFG_DISCOVER_PHY |
| 419 | /* wait for the PHY to wake up after reset |
| 420 | */ |
| 421 | mii_discover_phy(); |
| 422 | #endif |
| 423 | |
| 424 | /* And last, try to fill Rx Buffer Descriptors */ |
| 425 | fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */ |
| 426 | |
| 427 | return 1; |
| 428 | } |
| 429 | |
| 430 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 431 | static void fec_halt(struct eth_device* dev) |
| 432 | { |
| 433 | #if 0 |
| 434 | volatile immap_t *immr = (immap_t *)CFG_IMMR; |
| 435 | immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT); |
| 436 | #endif |
| 437 | } |
| 438 | |
| 439 | #if 0 |
| 440 | void restart(void) |
| 441 | { |
| 442 | volatile immap_t *immr = (immap_t *)CFG_IMMR; |
| 443 | immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT); |
| 444 | } |
| 445 | #endif |
| 446 | |
| 447 | #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII) |
| 448 | |
| 449 | static int phyaddr = -1; /* didn't find a PHY yet */ |
| 450 | static uint phytype; |
| 451 | |
| 452 | /* Make MII read/write commands for the FEC. |
| 453 | */ |
| 454 | |
| 455 | #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \ |
| 456 | (REG & 0x1f) << 18)) |
| 457 | |
| 458 | #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \ |
| 459 | (REG & 0x1f) << 18) | \ |
| 460 | (VAL & 0xffff)) |
| 461 | |
| 462 | /* Interrupt events/masks. |
| 463 | */ |
| 464 | #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */ |
| 465 | #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */ |
| 466 | #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */ |
| 467 | #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */ |
| 468 | #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */ |
| 469 | #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */ |
| 470 | #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */ |
| 471 | #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */ |
| 472 | #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */ |
| 473 | #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */ |
| 474 | |
| 475 | /* PHY identification |
| 476 | */ |
| 477 | #define PHY_ID_LXT970 0x78100000 /* LXT970 */ |
| 478 | #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */ |
| 479 | #define PHY_ID_82555 0x02a80150 /* Intel 82555 */ |
| 480 | #define PHY_ID_QS6612 0x01814400 /* QS6612 */ |
| 481 | #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */ |
| 482 | #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */ |
| 483 | #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */ |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 484 | #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 485 | |
| 486 | /* send command to phy using mii, wait for result */ |
| 487 | static uint |
| 488 | mii_send(uint mii_cmd) |
| 489 | { |
| 490 | uint mii_reply; |
| 491 | volatile fec_t *ep; |
| 492 | |
| 493 | ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec); |
| 494 | |
| 495 | ep->fec_mii_data = mii_cmd; /* command to phy */ |
| 496 | |
| 497 | /* wait for mii complete */ |
| 498 | while (!(ep->fec_ievent & FEC_ENET_MII)) |
| 499 | ; /* spin until done */ |
| 500 | mii_reply = ep->fec_mii_data; /* result from phy */ |
| 501 | ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */ |
| 502 | #if 0 |
| 503 | printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n", |
| 504 | __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply); |
| 505 | #endif |
| 506 | return (mii_reply & 0xffff); /* data read from phy */ |
| 507 | } |
| 508 | #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */ |
| 509 | |
| 510 | #if defined(CFG_DISCOVER_PHY) |
| 511 | static void |
| 512 | mii_discover_phy(void) |
| 513 | { |
| 514 | #define MAX_PHY_PASSES 11 |
| 515 | uint phyno; |
| 516 | int pass; |
| 517 | |
| 518 | phyaddr = -1; /* didn't find a PHY yet */ |
| 519 | for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) { |
| 520 | if (pass > 1) { |
| 521 | /* PHY may need more time to recover from reset. |
| 522 | * The LXT970 needs 50ms typical, no maximum is |
| 523 | * specified, so wait 10ms before try again. |
| 524 | * With 11 passes this gives it 100ms to wake up. |
| 525 | */ |
| 526 | udelay(10000); /* wait 10ms */ |
| 527 | } |
| 528 | for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) { |
| 529 | phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1)); |
| 530 | #ifdef ET_DEBUG |
| 531 | printf("PHY type 0x%x pass %d type ", phytype, pass); |
| 532 | #endif |
| 533 | if (phytype != 0xffff) { |
| 534 | phyaddr = phyno; |
| 535 | phytype <<= 16; |
| 536 | phytype |= mii_send(mk_mii_read(phyno, |
| 537 | PHY_PHYIDR2)); |
| 538 | |
| 539 | #ifdef ET_DEBUG |
| 540 | printf("PHY @ 0x%x pass %d type ",phyno,pass); |
| 541 | switch (phytype & 0xfffffff0) { |
| 542 | case PHY_ID_LXT970: |
| 543 | printf("LXT970\n"); |
| 544 | break; |
| 545 | case PHY_ID_LXT971: |
| 546 | printf("LXT971\n"); |
| 547 | break; |
| 548 | case PHY_ID_82555: |
| 549 | printf("82555\n"); |
| 550 | break; |
| 551 | case PHY_ID_QS6612: |
| 552 | printf("QS6612\n"); |
| 553 | break; |
| 554 | case PHY_ID_AMD79C784: |
| 555 | printf("AMD79C784\n"); |
| 556 | break; |
| 557 | case PHY_ID_LSI80225B: |
| 558 | printf("LSI L80225/B\n"); |
| 559 | break; |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 560 | case PHY_ID_DM9161: |
| 561 | printf("Davicom DM9161\n"); |
| 562 | break; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 563 | default: |
| 564 | printf("0x%08x\n", phytype); |
| 565 | break; |
| 566 | } |
| 567 | #endif |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | if (phyaddr < 0) { |
| 572 | printf("No PHY device found.\n"); |
| 573 | } |
| 574 | } |
| 575 | #endif /* CFG_DISCOVER_PHY */ |
| 576 | |
| 577 | #if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII) |
| 578 | |
| 579 | static int mii_init_done = 0; |
| 580 | |
| 581 | /**************************************************************************** |
| 582 | * mii_init -- Initialize the MII for MII command without ethernet |
| 583 | * This function is a subset of eth_init |
| 584 | **************************************************************************** |
| 585 | */ |
| 586 | void mii_init (void) |
| 587 | { |
| 588 | DECLARE_GLOBAL_DATA_PTR; |
| 589 | bd_t *bd = gd->bd; |
| 590 | |
| 591 | volatile immap_t *immr = (immap_t *) CFG_IMMR; |
| 592 | volatile fec_t *fecp = &(immr->im_cpm.cp_fec); |
| 593 | int i; |
| 594 | |
| 595 | if (mii_init_done != 0) { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | /* Whack a reset. |
| 600 | * A delay is required between a reset of the FEC block and |
| 601 | * initialization of other FEC registers because the reset takes |
| 602 | * some time to complete. If you don't delay, subsequent writes |
| 603 | * to FEC registers might get killed by the reset routine which is |
| 604 | * still in progress. |
| 605 | */ |
| 606 | |
| 607 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET; |
| 608 | for (i = 0; |
| 609 | (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY); |
| 610 | ++i) { |
| 611 | udelay (1); |
| 612 | } |
| 613 | if (i == FEC_RESET_DELAY) { |
| 614 | printf ("FEC_RESET_DELAY timeout\n"); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | /* We use strictly polling mode only |
| 619 | */ |
| 620 | fecp->fec_imask = 0; |
| 621 | |
| 622 | /* Clear any pending interrupt |
| 623 | */ |
| 624 | fecp->fec_ievent = 0xffc0; |
| 625 | |
| 626 | /* Set MII speed to 2.5 MHz or slightly below. |
| 627 | * According to the MPC860T (Rev. D) Fast ethernet controller user |
| 628 | * manual (6.2.14), |
| 629 | * the MII management interface clock must be less than or equal |
| 630 | * to 2.5 MHz. |
| 631 | * This MDC frequency is equal to system clock / (2 * MII_SPEED). |
| 632 | * Then MII_SPEED = system_clock / 2 * 2,5 Mhz. |
| 633 | */ |
wdenk | ef1464c | 2003-10-08 22:14:02 +0000 | [diff] [blame] | 634 | fecp->fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 635 | |
wdenk | 180d3f7 | 2004-01-04 16:28:35 +0000 | [diff] [blame^] | 636 | #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */ |
| 637 | immr->im_ioport.iop_papar |= 0xf830; |
| 638 | immr->im_ioport.iop_padir |= 0x0830; |
| 639 | immr->im_ioport.iop_padir &= ~0xf000; |
| 640 | immr->im_cpm.cp_pbpar |= 0x00001001; |
| 641 | immr->im_cpm.cp_pbdir &= ~0x00001001; |
| 642 | immr->im_ioport.iop_pcpar |= 0x000c; |
| 643 | immr->im_ioport.iop_pcdir &= ~0x000c; |
| 644 | immr->im_ioport.iop_pdpar |= 0x0080; |
| 645 | immr->im_ioport.iop_pddir &= ~0x0080; |
| 646 | immr->im_cpm.cp_pepar |= 0x00000003; |
| 647 | immr->im_cpm.cp_pedir |= 0x00000003; |
| 648 | immr->im_cpm.cp_peso &= ~0x00000003; |
| 649 | #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 650 | /* Configure all of port D for MII. |
| 651 | */ |
| 652 | immr->im_ioport.iop_pdpar = 0x1fff; |
| 653 | |
| 654 | /* Bits moved from Rev. D onward */ |
| 655 | if ((get_immr (0) & 0xffff) < 0x0501) { |
| 656 | immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */ |
| 657 | } else { |
| 658 | immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */ |
| 659 | } |
| 660 | #else |
| 661 | /* Configure port A for MII. |
| 662 | */ |
| 663 | |
| 664 | #if defined(CONFIG_ICU862) |
| 665 | |
| 666 | /* On the ICU862 board the MII-MDC pin is routed to PD8 pin |
| 667 | * of CPU, so for this board we need to configure Utopia and |
| 668 | * enable PD8 to MII-MDC function */ |
| 669 | immr->im_ioport.iop_pdpar |= 0x4080; |
| 670 | #endif |
| 671 | |
| 672 | /* Has Utopia been configured? */ |
| 673 | if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) { |
| 674 | /* |
| 675 | * YES - Use MUXED mode for UTOPIA bus. |
| 676 | * This frees Port A for use by MII (see 862UM table 41-6). |
| 677 | */ |
| 678 | immr->im_ioport.utmode &= ~0x80; |
| 679 | } else { |
| 680 | /* |
| 681 | * NO - set SPLIT mode for UTOPIA bus. |
| 682 | * |
| 683 | * This doesn't really effect UTOPIA (which isn't |
| 684 | * enabled anyway) but just tells the 862 |
| 685 | * to use port A for MII (see 862UM table 41-6). |
| 686 | */ |
| 687 | immr->im_ioport.utmode |= 0x80; |
| 688 | } |
| 689 | #endif /* !defined(CONFIG_ICU862) */ |
| 690 | /* Now enable the transmit and receive processing |
| 691 | */ |
| 692 | fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN; |
| 693 | |
| 694 | mii_init_done = 1; |
| 695 | } |
| 696 | /***************************************************************************** |
| 697 | * Read and write a MII PHY register, routines used by MII Utilities |
| 698 | * |
| 699 | * FIXME: These routines are expected to return 0 on success, but mii_send |
| 700 | * does _not_ return an error code. Maybe 0xFFFF means error, i.e. |
| 701 | * no PHY connected... |
| 702 | * For now always return 0. |
| 703 | * FIXME: These routines only work after calling eth_init() at least once! |
| 704 | * Otherwise they hang in mii_send() !!! Sorry! |
| 705 | *****************************************************************************/ |
| 706 | |
| 707 | int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value) |
| 708 | { |
| 709 | short rdreg; /* register working value */ |
| 710 | |
| 711 | #ifdef MII_DEBUG |
| 712 | printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr); |
| 713 | #endif |
| 714 | rdreg = mii_send(mk_mii_read(addr, reg)); |
| 715 | |
| 716 | *value = rdreg; |
| 717 | |
| 718 | #ifdef MII_DEBUG |
| 719 | printf ("0x%04x\n", *value); |
| 720 | #endif |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value) |
| 726 | { |
| 727 | short rdreg; /* register working value */ |
| 728 | |
| 729 | #ifdef MII_DEBUG |
| 730 | printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr); |
| 731 | #endif |
| 732 | |
| 733 | rdreg = mii_send(mk_mii_write(addr, reg, value)); |
| 734 | |
| 735 | #ifdef MII_DEBUG |
| 736 | printf ("0x%04x\n", value); |
| 737 | #endif |
| 738 | |
| 739 | return 0; |
| 740 | } |
| 741 | #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/ |
| 742 | |
| 743 | #endif /* CFG_CMD_NET, FEC_ENET */ |