wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * This file is based on mpc4200fec.c, |
| 6 | * (C) Copyright Motorola, Inc., 2000 |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <mpc5xxx.h> |
| 11 | #include <malloc.h> |
| 12 | #include <net.h> |
| 13 | #include <miiphy.h> |
| 14 | #include "sdma.h" |
| 15 | #include "fec.h" |
| 16 | |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 17 | /* #define DEBUG 0x28 */ |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 18 | |
| 19 | #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \ |
| 20 | defined(CONFIG_MPC5XXX_FEC) |
| 21 | |
| 22 | #if (DEBUG & 0x60) |
| 23 | static void tfifo_print(mpc5xxx_fec_priv *fec); |
| 24 | static void rfifo_print(mpc5xxx_fec_priv *fec); |
| 25 | #endif /* DEBUG */ |
| 26 | |
| 27 | #if (DEBUG & 0x40) |
| 28 | static uint32 local_crc32(char *string, unsigned int crc_value, int len); |
| 29 | #endif |
| 30 | |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 31 | typedef struct { |
| 32 | uint8 data[1500]; /* actual data */ |
| 33 | int length; /* actual length */ |
| 34 | int used; /* buffer in use or not */ |
| 35 | uint8 head[16]; /* MAC header(6 + 6 + 2) + 2(aligned) */ |
| 36 | } NBUF; |
| 37 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 38 | /********************************************************************/ |
| 39 | static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec) |
| 40 | { |
| 41 | int ix; |
| 42 | char *data; |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 43 | static int once = 0; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 44 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 45 | for (ix = 0; ix < FEC_RBD_NUM; ix++) { |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 46 | if (!once) { |
| 47 | data = (char *)malloc(FEC_MAX_PKT_SIZE); |
| 48 | if (data == NULL) { |
| 49 | printf ("RBD INIT FAILED\n"); |
| 50 | return -1; |
| 51 | } |
| 52 | fec->rbdBase[ix].dataPointer = (uint32)data; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 53 | } |
| 54 | fec->rbdBase[ix].status = FEC_RBD_EMPTY; |
| 55 | fec->rbdBase[ix].dataLength = 0; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 56 | } |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 57 | once ++; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * have the last RBD to close the ring |
| 61 | */ |
| 62 | fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP; |
| 63 | fec->rbdIndex = 0; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /********************************************************************/ |
| 69 | static void mpc5xxx_fec_tbd_init(mpc5xxx_fec_priv *fec) |
| 70 | { |
| 71 | int ix; |
| 72 | |
| 73 | for (ix = 0; ix < FEC_TBD_NUM; ix++) { |
| 74 | fec->tbdBase[ix].status = 0; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Have the last TBD to close the ring |
| 79 | */ |
| 80 | fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP; |
| 81 | |
| 82 | /* |
| 83 | * Initialize some indices |
| 84 | */ |
| 85 | fec->tbdIndex = 0; |
| 86 | fec->usedTbdIndex = 0; |
| 87 | fec->cleanTbdNum = FEC_TBD_NUM; |
| 88 | } |
| 89 | |
| 90 | /********************************************************************/ |
| 91 | static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, FEC_RBD * pRbd) |
| 92 | { |
| 93 | /* |
| 94 | * Reset buffer descriptor as empty |
| 95 | */ |
| 96 | if ((fec->rbdIndex) == (FEC_RBD_NUM - 1)) |
| 97 | pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY); |
| 98 | else |
| 99 | pRbd->status = FEC_RBD_EMPTY; |
| 100 | |
| 101 | pRbd->dataLength = 0; |
| 102 | |
| 103 | /* |
| 104 | * Now, we have an empty RxBD, restart the SmartDMA receive task |
| 105 | */ |
| 106 | SDMA_TASK_ENABLE(FEC_RECV_TASK_NO); |
| 107 | |
| 108 | /* |
| 109 | * Increment BD count |
| 110 | */ |
| 111 | fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM; |
| 112 | } |
| 113 | |
| 114 | /********************************************************************/ |
| 115 | static void mpc5xxx_fec_tbd_scrub(mpc5xxx_fec_priv *fec) |
| 116 | { |
| 117 | FEC_TBD *pUsedTbd; |
| 118 | |
| 119 | #if (DEBUG & 0x1) |
| 120 | printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n", |
| 121 | fec->cleanTbdNum, fec->usedTbdIndex); |
| 122 | #endif |
| 123 | |
| 124 | /* |
| 125 | * process all the consumed TBDs |
| 126 | */ |
| 127 | while (fec->cleanTbdNum < FEC_TBD_NUM) { |
| 128 | pUsedTbd = &fec->tbdBase[fec->usedTbdIndex]; |
| 129 | if (pUsedTbd->status & FEC_TBD_READY) { |
| 130 | #if (DEBUG & 0x20) |
| 131 | printf("Cannot clean TBD %d, in use\n", fec->cleanTbdNum); |
| 132 | #endif |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * clean this buffer descriptor |
| 138 | */ |
| 139 | if (fec->usedTbdIndex == (FEC_TBD_NUM - 1)) |
| 140 | pUsedTbd->status = FEC_TBD_WRAP; |
| 141 | else |
| 142 | pUsedTbd->status = 0; |
| 143 | |
| 144 | /* |
| 145 | * update some indeces for a correct handling of the TBD ring |
| 146 | */ |
| 147 | fec->cleanTbdNum++; |
| 148 | fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /********************************************************************/ |
| 153 | static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac) |
| 154 | { |
| 155 | uint8 currByte; /* byte for which to compute the CRC */ |
| 156 | int byte; /* loop - counter */ |
| 157 | int bit; /* loop - counter */ |
| 158 | uint32 crc = 0xffffffff; /* initial value */ |
| 159 | |
| 160 | /* |
| 161 | * The algorithm used is the following: |
| 162 | * we loop on each of the six bytes of the provided address, |
| 163 | * and we compute the CRC by left-shifting the previous |
| 164 | * value by one position, so that each bit in the current |
| 165 | * byte of the address may contribute the calculation. If |
| 166 | * the latter and the MSB in the CRC are different, then |
| 167 | * the CRC value so computed is also ex-ored with the |
| 168 | * "polynomium generator". The current byte of the address |
| 169 | * is also shifted right by one bit at each iteration. |
| 170 | * This is because the CRC generatore in hardware is implemented |
| 171 | * as a shift-register with as many ex-ores as the radixes |
| 172 | * in the polynomium. This suggests that we represent the |
| 173 | * polynomiumm itself as a 32-bit constant. |
| 174 | */ |
| 175 | for (byte = 0; byte < 6; byte++) { |
| 176 | currByte = mac[byte]; |
| 177 | for (bit = 0; bit < 8; bit++) { |
| 178 | if ((currByte & 0x01) ^ (crc & 0x01)) { |
| 179 | crc >>= 1; |
| 180 | crc = crc ^ 0xedb88320; |
| 181 | } else { |
| 182 | crc >>= 1; |
| 183 | } |
| 184 | currByte >>= 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | crc = crc >> 26; |
| 189 | |
| 190 | /* |
| 191 | * Set individual hash table register |
| 192 | */ |
| 193 | if (crc >= 32) { |
| 194 | fec->eth->iaddr1 = (1 << (crc - 32)); |
| 195 | fec->eth->iaddr2 = 0; |
| 196 | } else { |
| 197 | fec->eth->iaddr1 = 0; |
| 198 | fec->eth->iaddr2 = (1 << crc); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Set physical address |
| 203 | */ |
| 204 | fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3]; |
| 205 | fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808; |
| 206 | } |
| 207 | |
| 208 | /********************************************************************/ |
| 209 | static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis) |
| 210 | { |
wdenk | eb20ad3 | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 211 | DECLARE_GLOBAL_DATA_PTR; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 212 | mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv; |
| 213 | struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA; |
| 214 | const uint8 phyAddr = 0; /* Only one PHY */ |
| 215 | |
| 216 | #if (DEBUG & 0x1) |
| 217 | printf ("mpc5xxx_fec_init... Begin\n"); |
| 218 | #endif |
| 219 | |
| 220 | /* |
| 221 | * Initialize RxBD/TxBD rings |
| 222 | */ |
| 223 | mpc5xxx_fec_rbd_init(fec); |
| 224 | mpc5xxx_fec_tbd_init(fec); |
| 225 | |
| 226 | /* |
| 227 | * Initialize GPIO pins |
| 228 | */ |
| 229 | if (fec->xcv_type == SEVENWIRE) { |
| 230 | /* 10MBit with 7-wire operation */ |
| 231 | *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000; |
| 232 | } else { |
| 233 | /* 100MBit with MD operation */ |
| 234 | *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Clear FEC-Lite interrupt event register(IEVENT) |
| 239 | */ |
| 240 | fec->eth->ievent = 0xffffffff; |
| 241 | |
| 242 | /* |
| 243 | * Set interrupt mask register |
| 244 | */ |
| 245 | fec->eth->imask = 0x00000000; |
| 246 | |
| 247 | /* |
| 248 | * Set FEC-Lite receive control register(R_CNTRL): |
| 249 | */ |
| 250 | if (fec->xcv_type == SEVENWIRE) { |
| 251 | /* |
| 252 | * Frame length=1518; 7-wire mode |
| 253 | */ |
| 254 | fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */ |
| 255 | } else { |
| 256 | /* |
| 257 | * Frame length=1518; MII mode; |
| 258 | */ |
| 259 | fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */ |
| 260 | } |
| 261 | |
| 262 | if (fec->xcv_type == SEVENWIRE) { |
| 263 | /* |
| 264 | * Set FEC-Lite transmit control register(X_CNTRL): |
| 265 | */ |
| 266 | /*fec->eth->x_cntrl = 0x00000002; */ /* half-duplex, heartbeat */ |
| 267 | fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */ |
| 268 | } else { |
| 269 | /*fec->eth->x_cntrl = 0x00000006; */ /* full-duplex, heartbeat */ |
| 270 | fec->eth->x_cntrl = 0x00000004; /* full-duplex, heartbeat disabled */ |
| 271 | |
| 272 | /* |
wdenk | eb20ad3 | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 273 | * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 274 | * and do not drop the Preamble. |
| 275 | */ |
wdenk | eb20ad3 | 2003-09-05 23:19:14 +0000 | [diff] [blame] | 276 | fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */ |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Set Opcode/Pause Duration Register |
| 281 | */ |
| 282 | fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */ |
| 283 | |
| 284 | /* |
| 285 | * Set Rx FIFO alarm and granularity value |
| 286 | */ |
| 287 | fec->eth->rfifo_cntrl = 0x0c000000; |
| 288 | fec->eth->rfifo_alarm = 0x0000030c; |
| 289 | #if (DEBUG & 0x22) |
| 290 | if (fec->eth->rfifo_status & 0x00700000 ) { |
| 291 | printf("mpc5xxx_fec_init() RFIFO error\n"); |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | /* |
| 296 | * Set Tx FIFO granularity value |
| 297 | */ |
| 298 | fec->eth->tfifo_cntrl = 0x0c000000; |
| 299 | #if (DEBUG & 0x2) |
| 300 | printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status); |
| 301 | printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm); |
| 302 | #endif |
| 303 | |
| 304 | /* |
| 305 | * Set transmit fifo watermark register(X_WMRK), default = 64 |
| 306 | */ |
| 307 | fec->eth->tfifo_alarm = 0x00000080; |
| 308 | fec->eth->x_wmrk = 0x2; |
| 309 | |
| 310 | /* |
| 311 | * Set individual address filter for unicast address |
| 312 | * and set physical address registers. |
| 313 | */ |
| 314 | mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr); |
| 315 | |
| 316 | /* |
| 317 | * Set multicast address filter |
| 318 | */ |
| 319 | fec->eth->gaddr1 = 0x00000000; |
| 320 | fec->eth->gaddr2 = 0x00000000; |
| 321 | |
| 322 | /* |
| 323 | * Turn ON cheater FSM: ???? |
| 324 | */ |
| 325 | fec->eth->xmit_fsm = 0x03000000; |
| 326 | |
| 327 | #if defined(CONFIG_MPC5200) |
| 328 | /* |
| 329 | * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't |
| 330 | * work w/ the current receive task. |
| 331 | */ |
| 332 | sdma->PtdCntrl |= 0x00000001; |
| 333 | #endif |
| 334 | |
| 335 | /* |
| 336 | * Set priority of different initiators |
| 337 | */ |
| 338 | sdma->IPR0 = 7; /* always */ |
| 339 | sdma->IPR3 = 6; /* Eth RX */ |
| 340 | sdma->IPR4 = 5; /* Eth Tx */ |
| 341 | |
| 342 | /* |
| 343 | * Clear SmartDMA task interrupt pending bits |
| 344 | */ |
| 345 | SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO); |
| 346 | |
| 347 | /* |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 348 | * Initialize SmartDMA parameters stored in SRAM |
| 349 | */ |
| 350 | *(int *)FEC_TBD_BASE = (int)fec->tbdBase; |
| 351 | *(int *)FEC_RBD_BASE = (int)fec->rbdBase; |
| 352 | *(int *)FEC_TBD_NEXT = (int)fec->tbdBase; |
| 353 | *(int *)FEC_RBD_NEXT = (int)fec->rbdBase; |
| 354 | |
| 355 | if (fec->xcv_type != SEVENWIRE) { |
| 356 | /* |
| 357 | * Initialize PHY(LXT971A): |
| 358 | * |
| 359 | * Generally, on power up, the LXT971A reads its configuration |
| 360 | * pins to check for forced operation, If not cofigured for |
| 361 | * forced operation, it uses auto-negotiation/parallel detection |
| 362 | * to automatically determine line operating conditions. |
| 363 | * If the PHY device on the other side of the link supports |
| 364 | * auto-negotiation, the LXT971A auto-negotiates with it |
| 365 | * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not |
| 366 | * support auto-negotiation, the LXT971A automatically detects |
| 367 | * the presence of either link pulses(10Mbps PHY) or Idle |
| 368 | * symbols(100Mbps) and sets its operating conditions accordingly. |
| 369 | * |
| 370 | * When auto-negotiation is controlled by software, the following |
| 371 | * steps are recommended. |
| 372 | * |
| 373 | * Note: |
| 374 | * The physical address is dependent on hardware configuration. |
| 375 | * |
| 376 | */ |
| 377 | int timeout = 1; |
| 378 | uint16 phyStatus; |
| 379 | |
| 380 | /* |
| 381 | * Reset PHY, then delay 300ns |
| 382 | */ |
| 383 | miiphy_write(phyAddr, 0x0, 0x8000); |
| 384 | udelay(1000); |
| 385 | |
| 386 | if (fec->xcv_type == MII10) { |
| 387 | /* |
| 388 | * Force 10Base-T, FDX operation |
| 389 | */ |
wdenk | 56bb535 | 2003-09-16 17:29:31 +0000 | [diff] [blame^] | 390 | #if (DEBUG & 0x2) |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 391 | printf("Forcing 10 Mbps ethernet link... "); |
wdenk | 56bb535 | 2003-09-16 17:29:31 +0000 | [diff] [blame^] | 392 | #endif |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 393 | miiphy_read(phyAddr, 0x1, &phyStatus); |
| 394 | /* |
| 395 | miiphy_write(fec, phyAddr, 0x0, 0x0100); |
| 396 | */ |
| 397 | miiphy_write(phyAddr, 0x0, 0x0180); |
| 398 | |
| 399 | timeout = 20; |
| 400 | do { /* wait for link status to go down */ |
| 401 | udelay(10000); |
| 402 | if ((timeout--) == 0) { |
| 403 | #if (DEBUG & 0x2) |
| 404 | printf("hmmm, should not have waited..."); |
| 405 | #endif |
| 406 | break; |
| 407 | } |
| 408 | miiphy_read(phyAddr, 0x1, &phyStatus); |
| 409 | #if (DEBUG & 0x2) |
| 410 | printf("="); |
| 411 | #endif |
| 412 | } while ((phyStatus & 0x0004)); /* !link up */ |
| 413 | |
| 414 | timeout = 1000; |
| 415 | do { /* wait for link status to come back up */ |
| 416 | udelay(10000); |
| 417 | if ((timeout--) == 0) { |
| 418 | printf("failed. Link is down.\n"); |
| 419 | break; |
| 420 | } |
| 421 | miiphy_read(phyAddr, 0x1, &phyStatus); |
| 422 | #if (DEBUG & 0x2) |
| 423 | printf("+"); |
| 424 | #endif |
| 425 | } while (!(phyStatus & 0x0004)); /* !link up */ |
| 426 | |
| 427 | printf ("done.\n"); |
| 428 | } else { /* MII100 */ |
| 429 | /* |
| 430 | * Set the auto-negotiation advertisement register bits |
| 431 | */ |
| 432 | miiphy_write(phyAddr, 0x4, 0x01e1); |
| 433 | |
| 434 | /* |
| 435 | * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation |
| 436 | */ |
| 437 | miiphy_write(phyAddr, 0x0, 0x1200); |
| 438 | |
| 439 | /* |
| 440 | * Wait for AN completion |
| 441 | */ |
| 442 | timeout = 5000; |
| 443 | do { |
| 444 | udelay(1000); |
| 445 | |
| 446 | if ((timeout--) == 0) { |
| 447 | #if (DEBUG & 0x2) |
| 448 | printf("PHY auto neg 0 failed...\n"); |
| 449 | #endif |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) { |
| 454 | #if (DEBUG & 0x2) |
| 455 | printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus); |
| 456 | #endif |
| 457 | return -1; |
| 458 | } |
| 459 | } while ((phyStatus & 0x0020) != 0x0020); |
| 460 | |
| 461 | #if (DEBUG & 0x2) |
| 462 | printf("PHY auto neg complete! \n"); |
| 463 | #endif |
| 464 | } |
| 465 | |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Enable FEC-Lite controller |
| 470 | */ |
| 471 | fec->eth->ecntrl |= 0x00000006; |
| 472 | |
| 473 | if (fec->xcv_type != SEVENWIRE) { |
| 474 | #if (DEBUG & 0x2) |
| 475 | uint16 phyStatus, i; |
| 476 | uint8 phyAddr = 0; |
| 477 | |
| 478 | for (i = 0; i < 9; i++) { |
| 479 | miiphy_read(phyAddr, i, &phyStatus); |
| 480 | printf("Mii reg %d: 0x%04x\n", i, phyStatus); |
| 481 | } |
| 482 | for (i = 16; i < 21; i++) { |
| 483 | miiphy_read(phyAddr, i, &phyStatus); |
| 484 | printf("Mii reg %d: 0x%04x\n", i, phyStatus); |
| 485 | } |
| 486 | #endif |
| 487 | } |
| 488 | /* |
| 489 | * Enable SmartDMA receive task |
| 490 | */ |
| 491 | SDMA_TASK_ENABLE(FEC_RECV_TASK_NO); |
| 492 | |
| 493 | #if (DEBUG & 0x1) |
| 494 | printf("mpc5xxx_fec_init... Done \n"); |
| 495 | #endif |
| 496 | |
wdenk | 3f08e20 | 2003-08-07 14:52:18 +0000 | [diff] [blame] | 497 | return 1; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /********************************************************************/ |
| 501 | static void mpc5xxx_fec_halt(struct eth_device *dev) |
| 502 | { |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 503 | #if defined(CONFIG_MPC5200) |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 504 | struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA; |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 505 | #endif |
| 506 | mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 507 | int counter = 0xffff; |
| 508 | |
| 509 | #if (DEBUG & 0x2) |
| 510 | if (fec->xcv_type != SEVENWIRE) { |
| 511 | uint16 phyStatus, i; |
| 512 | uint8 phyAddr = 0; |
| 513 | |
| 514 | for (i = 0; i < 9; i++) { |
| 515 | miiphy_read(phyAddr, i, &phyStatus); |
| 516 | printf("Mii reg %d: 0x%04x\n", i, phyStatus); |
| 517 | } |
| 518 | for (i = 16; i < 21; i++) { |
| 519 | miiphy_read(phyAddr, i, &phyStatus); |
| 520 | printf ("Mii reg %d: 0x%04x\n", i, phyStatus); |
| 521 | } |
| 522 | } |
| 523 | #endif |
| 524 | |
| 525 | |
| 526 | /* |
| 527 | * mask FEC chip interrupts |
| 528 | */ |
| 529 | fec->eth->imask = 0; |
| 530 | |
| 531 | /* |
| 532 | * issue graceful stop command to the FEC transmitter if necessary |
| 533 | */ |
| 534 | fec->eth->x_cntrl |= 0x00000001; |
| 535 | |
| 536 | /* |
| 537 | * wait for graceful stop to register |
| 538 | */ |
| 539 | while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ; |
| 540 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 541 | /* |
| 542 | * Disable SmartDMA tasks |
| 543 | */ |
| 544 | SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO); |
| 545 | SDMA_TASK_DISABLE (FEC_RECV_TASK_NO); |
| 546 | |
| 547 | #if defined(CONFIG_MPC5200) |
| 548 | /* |
| 549 | * Turn on COMM bus prefetch in the MGT5200 BestComm after we're |
| 550 | * done. It doesn't work w/ the current receive task. |
| 551 | */ |
| 552 | sdma->PtdCntrl &= ~0x00000001; |
| 553 | #endif |
| 554 | |
| 555 | /* |
| 556 | * Disable the Ethernet Controller |
| 557 | */ |
| 558 | fec->eth->ecntrl &= 0xfffffffd; |
| 559 | |
| 560 | /* |
| 561 | * Clear FIFO status registers |
| 562 | */ |
| 563 | fec->eth->rfifo_status &= 0x00700000; |
| 564 | fec->eth->tfifo_status &= 0x00700000; |
| 565 | |
| 566 | fec->eth->reset_cntrl = 0x01000000; |
| 567 | |
| 568 | /* |
| 569 | * Issue a reset command to the FEC chip |
| 570 | */ |
| 571 | fec->eth->ecntrl |= 0x1; |
| 572 | |
| 573 | /* |
| 574 | * wait at least 16 clock cycles |
| 575 | */ |
| 576 | udelay(10); |
| 577 | |
| 578 | #if (DEBUG & 0x3) |
| 579 | printf("Ethernet task stopped\n"); |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | #if (DEBUG & 0x60) |
| 584 | /********************************************************************/ |
| 585 | |
| 586 | static void tfifo_print(mpc5xxx_fec_priv *fec) |
| 587 | { |
| 588 | uint16 phyAddr = 0; |
| 589 | uint16 phyStatus; |
| 590 | |
| 591 | if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr) |
| 592 | || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) { |
| 593 | |
| 594 | miiphy_read(phyAddr, 0x1, &phyStatus); |
| 595 | printf("\nphyStatus: 0x%04x\n", phyStatus); |
| 596 | printf("ecntrl: 0x%08x\n", fec->eth->ecntrl); |
| 597 | printf("ievent: 0x%08x\n", fec->eth->ievent); |
| 598 | printf("x_status: 0x%08x\n", fec->eth->x_status); |
| 599 | printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status); |
| 600 | |
| 601 | printf(" control 0x%08x\n", fec->eth->tfifo_cntrl); |
| 602 | printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr); |
| 603 | printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr); |
| 604 | printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm); |
| 605 | printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr); |
| 606 | printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static void rfifo_print(mpc5xxx_fec_priv *fec) |
| 611 | { |
| 612 | uint16 phyAddr = 0; |
| 613 | uint16 phyStatus; |
| 614 | |
| 615 | if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr) |
| 616 | || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) { |
| 617 | |
| 618 | miiphy_read(phyAddr, 0x1, &phyStatus); |
| 619 | printf("\nphyStatus: 0x%04x\n", phyStatus); |
| 620 | printf("ecntrl: 0x%08x\n", fec->eth->ecntrl); |
| 621 | printf("ievent: 0x%08x\n", fec->eth->ievent); |
| 622 | printf("x_status: 0x%08x\n", fec->eth->x_status); |
| 623 | printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status); |
| 624 | |
| 625 | printf(" control 0x%08x\n", fec->eth->rfifo_cntrl); |
| 626 | printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr); |
| 627 | printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr); |
| 628 | printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm); |
| 629 | printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr); |
| 630 | printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr); |
| 631 | } |
| 632 | } |
| 633 | #endif /* DEBUG */ |
| 634 | |
| 635 | /********************************************************************/ |
| 636 | |
| 637 | static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data, |
| 638 | int data_length) |
| 639 | { |
| 640 | /* |
| 641 | * This routine transmits one frame. This routine only accepts |
| 642 | * 6-byte Ethernet addresses. |
| 643 | */ |
| 644 | mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv; |
| 645 | FEC_TBD *pTbd; |
| 646 | |
| 647 | #if (DEBUG & 0x20) |
| 648 | printf("tbd status: 0x%04x\n", fec->tbdBase[0].status); |
| 649 | tfifo_print(fec); |
| 650 | #endif |
| 651 | |
| 652 | /* |
| 653 | * Clear Tx BD ring at first |
| 654 | */ |
| 655 | mpc5xxx_fec_tbd_scrub(fec); |
| 656 | |
| 657 | /* |
| 658 | * Check for valid length of data. |
| 659 | */ |
| 660 | if ((data_length > 1500) || (data_length <= 0)) { |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Check the number of vacant TxBDs. |
| 666 | */ |
| 667 | if (fec->cleanTbdNum < 1) { |
| 668 | #if (DEBUG & 0x20) |
| 669 | printf("No available TxBDs ...\n"); |
| 670 | #endif |
| 671 | return -1; |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * Get the first TxBD to send the mac header |
| 676 | */ |
| 677 | pTbd = &fec->tbdBase[fec->tbdIndex]; |
| 678 | pTbd->dataLength = data_length; |
| 679 | pTbd->dataPointer = (uint32)eth_data; |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 680 | pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 681 | fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM; |
| 682 | |
| 683 | #if (DEBUG & 0x100) |
| 684 | printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex); |
| 685 | #endif |
| 686 | |
| 687 | /* |
| 688 | * Kick the MII i/f |
| 689 | */ |
| 690 | if (fec->xcv_type != SEVENWIRE) { |
| 691 | uint16 phyStatus; |
| 692 | miiphy_read(0, 0x1, &phyStatus); |
| 693 | } |
| 694 | |
| 695 | /* |
| 696 | * Enable SmartDMA transmit task |
| 697 | */ |
| 698 | |
| 699 | #if (DEBUG & 0x20) |
| 700 | tfifo_print(fec); |
| 701 | #endif |
| 702 | SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO); |
| 703 | #if (DEBUG & 0x20) |
| 704 | tfifo_print(fec); |
| 705 | #endif |
| 706 | #if (DEBUG & 0x8) |
| 707 | printf( "+" ); |
| 708 | #endif |
| 709 | |
| 710 | fec->cleanTbdNum -= 1; |
| 711 | |
| 712 | #if (DEBUG & 0x129) && (DEBUG & 0x80000000) |
| 713 | printf ("smartDMA ethernet Tx task enabled\n"); |
| 714 | #endif |
| 715 | /* |
| 716 | * wait until frame is sent . |
| 717 | */ |
| 718 | while (pTbd->status & FEC_TBD_READY) { |
| 719 | udelay(10); |
| 720 | #if (DEBUG & 0x8) |
| 721 | printf ("TDB status = %04x\n", pTbd->status); |
| 722 | #endif |
| 723 | } |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /********************************************************************/ |
| 730 | static int mpc5xxx_fec_recv(struct eth_device *dev) |
| 731 | { |
| 732 | /* |
| 733 | * This command pulls one frame from the card |
| 734 | */ |
| 735 | mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv; |
| 736 | FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex]; |
| 737 | unsigned long ievent; |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 738 | int frame_length, len = 0; |
| 739 | NBUF *frame; |
| 740 | char buff[FEC_MAX_PKT_SIZE]; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 741 | |
| 742 | #if (DEBUG & 0x1) |
| 743 | printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex); |
| 744 | #endif |
| 745 | #if (DEBUG & 0x8) |
| 746 | printf( "-" ); |
| 747 | #endif |
| 748 | |
| 749 | /* |
| 750 | * Check if any critical events have happened |
| 751 | */ |
| 752 | ievent = fec->eth->ievent; |
| 753 | fec->eth->ievent = ievent; |
| 754 | if (ievent & 0x20060000) { |
| 755 | /* BABT, Rx/Tx FIFO errors */ |
| 756 | mpc5xxx_fec_halt(dev); |
| 757 | mpc5xxx_fec_init(dev, NULL); |
| 758 | return 0; |
| 759 | } |
| 760 | if (ievent & 0x80000000) { |
| 761 | /* Heartbeat error */ |
| 762 | fec->eth->x_cntrl |= 0x00000001; |
| 763 | } |
| 764 | if (ievent & 0x10000000) { |
| 765 | /* Graceful stop complete */ |
| 766 | if (fec->eth->x_cntrl & 0x00000001) { |
| 767 | mpc5xxx_fec_halt(dev); |
| 768 | fec->eth->x_cntrl &= ~0x00000001; |
| 769 | mpc5xxx_fec_init(dev, NULL); |
| 770 | } |
| 771 | } |
| 772 | |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 773 | if (!(pRbd->status & FEC_RBD_EMPTY)) { |
| 774 | if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) && |
| 775 | ((pRbd->dataLength - 4) > 14)) { |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 776 | |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 777 | /* |
| 778 | * Get buffer address and size |
| 779 | */ |
| 780 | frame = (NBUF *)pRbd->dataPointer; |
| 781 | frame_length = pRbd->dataLength - 4; |
| 782 | |
| 783 | #if (DEBUG & 0x20) |
| 784 | { |
| 785 | int i; |
| 786 | printf("recv data hdr:"); |
| 787 | for (i = 0; i < 14; i++) |
| 788 | printf("%x ", *(frame->head + i)); |
| 789 | printf("\n"); |
| 790 | } |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 791 | #endif |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 792 | /* |
| 793 | * Fill the buffer and pass it to upper layers |
| 794 | */ |
| 795 | memcpy(buff, frame->head, 14); |
| 796 | memcpy(buff + 14, frame->data, frame_length); |
| 797 | NetReceive(buff, frame_length); |
| 798 | len = frame_length; |
| 799 | } |
| 800 | /* |
| 801 | * Reset buffer descriptor as empty |
| 802 | */ |
| 803 | mpc5xxx_fec_rbd_clean(fec, pRbd); |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 804 | } |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 805 | SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO); |
| 806 | return len; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | |
| 810 | /********************************************************************/ |
| 811 | int mpc5xxx_fec_initialize(bd_t * bis) |
| 812 | { |
| 813 | mpc5xxx_fec_priv *fec; |
| 814 | struct eth_device *dev; |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 815 | char *tmp, *end; |
| 816 | char env_enetaddr[6]; |
| 817 | int i; |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 818 | |
| 819 | fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec)); |
| 820 | dev = (struct eth_device *)malloc(sizeof(*dev)); |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 821 | memset(dev, 0, sizeof *dev); |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 822 | |
| 823 | fec->eth = (ethernet_regs *)MPC5XXX_FEC; |
| 824 | fec->tbdBase = (FEC_TBD *)FEC_BD_BASE; |
| 825 | fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD)); |
| 826 | #ifdef CONFIG_ICECUBE |
wdenk | 56bb535 | 2003-09-16 17:29:31 +0000 | [diff] [blame^] | 827 | #ifndef CONFIG_FEC_10MBIT |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 828 | fec->xcv_type = MII100; |
wdenk | 56bb535 | 2003-09-16 17:29:31 +0000 | [diff] [blame^] | 829 | #else |
| 830 | fec->xcv_type = MII10; |
| 831 | #endif |
| 832 | #else |
| 833 | #error fec->xcv_type not initialized. |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 834 | #endif |
| 835 | |
| 836 | dev->priv = (void *)fec; |
| 837 | dev->iobase = MPC5XXX_FEC; |
| 838 | dev->init = mpc5xxx_fec_init; |
| 839 | dev->halt = mpc5xxx_fec_halt; |
| 840 | dev->send = mpc5xxx_fec_send; |
| 841 | dev->recv = mpc5xxx_fec_recv; |
| 842 | |
wdenk | b846356 | 2003-07-26 08:08:08 +0000 | [diff] [blame] | 843 | sprintf(dev->name, "FEC ETHERNET"); |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 844 | eth_register(dev); |
| 845 | |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 846 | /* |
| 847 | * Try to set the mac address now. The fec mac address is |
| 848 | * a garbage after reset. When not using fec for booting |
| 849 | * the Linux fec driver will try to work with this garbage. |
| 850 | */ |
| 851 | tmp = getenv("ethaddr"); |
| 852 | if (tmp) { |
| 853 | for (i=0; i<6; i++) { |
| 854 | env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 855 | if (tmp) |
| 856 | tmp = (*end) ? end+1 : end; |
| 857 | } |
| 858 | mpc5xxx_fec_set_hwaddr(fec, env_enetaddr); |
| 859 | } |
| 860 | |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 861 | return 1; |
| 862 | } |
| 863 | |
| 864 | /* MII-interface related functions */ |
| 865 | /********************************************************************/ |
| 866 | int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal) |
| 867 | { |
| 868 | ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC; |
| 869 | uint32 reg; /* convenient holder for the PHY register */ |
| 870 | uint32 phy; /* convenient holder for the PHY */ |
| 871 | int timeout = 0xffff; |
| 872 | |
| 873 | /* |
| 874 | * reading from any PHY's register is done by properly |
| 875 | * programming the FEC's MII data register. |
| 876 | */ |
| 877 | reg = regAddr << FEC_MII_DATA_RA_SHIFT; |
| 878 | phy = phyAddr << FEC_MII_DATA_PA_SHIFT; |
| 879 | |
| 880 | eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg); |
| 881 | |
| 882 | /* |
| 883 | * wait for the related interrupt |
| 884 | */ |
| 885 | while ((timeout--) && (!(eth->ievent & 0x00800000))) ; |
| 886 | |
| 887 | if (timeout == 0) { |
| 888 | #if (DEBUG & 0x2) |
| 889 | printf ("Read MDIO failed...\n"); |
| 890 | #endif |
| 891 | return -1; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * clear mii interrupt bit |
| 896 | */ |
| 897 | eth->ievent = 0x00800000; |
| 898 | |
| 899 | /* |
| 900 | * it's now safe to read the PHY's register |
| 901 | */ |
| 902 | *retVal = (uint16) eth->mii_data; |
| 903 | |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | /********************************************************************/ |
| 908 | int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data) |
| 909 | { |
| 910 | ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC; |
| 911 | uint32 reg; /* convenient holder for the PHY register */ |
| 912 | uint32 phy; /* convenient holder for the PHY */ |
| 913 | int timeout = 0xffff; |
| 914 | |
| 915 | reg = regAddr << FEC_MII_DATA_RA_SHIFT; |
| 916 | phy = phyAddr << FEC_MII_DATA_PA_SHIFT; |
| 917 | |
| 918 | eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR | |
| 919 | FEC_MII_DATA_TA | phy | reg | data); |
| 920 | |
| 921 | /* |
| 922 | * wait for the MII interrupt |
| 923 | */ |
| 924 | while ((timeout--) && (!(eth->ievent & 0x00800000))) ; |
| 925 | |
| 926 | if (timeout == 0) { |
| 927 | #if (DEBUG & 0x2) |
| 928 | printf ("Write MDIO failed...\n"); |
| 929 | #endif |
| 930 | return -1; |
| 931 | } |
| 932 | |
| 933 | /* |
| 934 | * clear MII interrupt bit |
| 935 | */ |
| 936 | eth->ievent = 0x00800000; |
| 937 | |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | #if (DEBUG & 0x40) |
| 942 | static uint32 local_crc32(char *string, unsigned int crc_value, int len) |
| 943 | { |
| 944 | int i; |
| 945 | char c; |
| 946 | unsigned int crc, count; |
| 947 | |
| 948 | /* |
| 949 | * crc32 algorithm |
| 950 | */ |
| 951 | /* |
| 952 | * crc = 0xffffffff; * The initialized value should be 0xffffffff |
| 953 | */ |
| 954 | crc = crc_value; |
| 955 | |
| 956 | for (i = len; --i >= 0;) { |
| 957 | c = *string++; |
| 958 | for (count = 0; count < 8; count++) { |
| 959 | if ((c & 0x01) ^ (crc & 0x01)) { |
| 960 | crc >>= 1; |
| 961 | crc = crc ^ 0xedb88320; |
| 962 | } else { |
| 963 | crc >>= 1; |
| 964 | } |
| 965 | c >>= 1; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * In big endian system, do byte swaping for crc value |
| 971 | */ |
| 972 | /**/ return crc; |
| 973 | } |
| 974 | #endif /* DEBUG */ |
| 975 | |
| 976 | #endif /* CONFIG_MPC5XXX_FEC */ |