Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 2 | * (C) Copyright 2003-2009 |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * Derived from the MPC8xx FEC driver. |
| 6 | * Adapted for MPC512x by Grzegorz Bernacki <gjb@semihalf.com> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <net.h> |
Ben Warren | b664dea | 2008-08-31 10:36:38 -0700 | [diff] [blame] | 12 | #include <netdev.h> |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 13 | #include <miiphy.h> |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Ben Warren | 604b6fb | 2008-08-28 23:58:30 -0700 | [diff] [blame] | 15 | #include "mpc512x_fec.h" |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | #define DEBUG 0 |
| 20 | |
Wolfgang Denk | 56cbd02 | 2007-08-12 14:27:39 +0200 | [diff] [blame] | 21 | #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 22 | defined(CONFIG_MPC512x_FEC) |
| 23 | |
Wolfgang Denk | 56cbd02 | 2007-08-12 14:27:39 +0200 | [diff] [blame] | 24 | #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 25 | #error "CONFIG_MII has to be defined!" |
| 26 | #endif |
| 27 | |
| 28 | #if (DEBUG & 0x40) |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 29 | static u32 local_crc32(char *string, unsigned int crc_value, int len); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 32 | int fec512x_miiphy_read(char *devname, u8 phyAddr, u8 regAddr, u16 * retVal); |
| 33 | int fec512x_miiphy_write(char *devname, u8 phyAddr, u8 regAddr, u16 data); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 34 | int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis); |
| 35 | |
Grzegorz Bernacki | 9425b96 | 2007-09-10 17:39:08 +0200 | [diff] [blame] | 36 | static uchar rx_buff[FEC_BUFFER_SIZE]; |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 37 | static int rx_buff_idx = 0; |
| 38 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 39 | /********************************************************************/ |
| 40 | #if (DEBUG & 0x2) |
| 41 | static void mpc512x_fec_phydump (char *devname) |
| 42 | { |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 43 | u16 phyStatus, i; |
| 44 | u8 phyAddr = CONFIG_PHY_ADDR; |
| 45 | u8 reg_mask[] = { |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 46 | /* regs to print: 0...8, 21,27,31 */ |
| 47 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, |
| 48 | 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, |
| 49 | }; |
| 50 | |
| 51 | for (i = 0; i < 32; i++) { |
| 52 | if (reg_mask[i]) { |
| 53 | miiphy_read (devname, phyAddr, i, &phyStatus); |
| 54 | printf ("Mii reg %d: 0x%04x\n", i, phyStatus); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | /********************************************************************/ |
| 61 | static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec) |
| 62 | { |
| 63 | int ix; |
| 64 | |
| 65 | /* |
| 66 | * Receive BDs init |
| 67 | */ |
| 68 | for (ix = 0; ix < FEC_RBD_NUM; ix++) { |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 69 | fec->bdBase->rbd[ix].dataPointer = |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 70 | (u32)&fec->bdBase->recv_frames[ix]; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 71 | fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY; |
| 72 | fec->bdBase->rbd[ix].dataLength = 0; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * have the last RBD to close the ring |
| 77 | */ |
| 78 | fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP; |
| 79 | fec->rbdIndex = 0; |
| 80 | |
| 81 | /* |
| 82 | * Trasmit BDs init |
| 83 | */ |
| 84 | for (ix = 0; ix < FEC_TBD_NUM; ix++) { |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 85 | fec->bdBase->tbd[ix].status = 0; |
| 86 | } |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 87 | |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Have the last TBD to close the ring |
| 90 | */ |
| 91 | fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 92 | |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 93 | /* |
| 94 | * Initialize some indices |
| 95 | */ |
| 96 | fec->tbdIndex = 0; |
| 97 | fec->usedTbdIndex = 0; |
| 98 | fec->cleanTbdNum = FEC_TBD_NUM; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /********************************************************************/ |
| 104 | static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd) |
| 105 | { |
| 106 | /* |
| 107 | * Reset buffer descriptor as empty |
| 108 | */ |
| 109 | if ((fec->rbdIndex) == (FEC_RBD_NUM - 1)) |
| 110 | pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY); |
| 111 | else |
| 112 | pRbd->status = FEC_RBD_EMPTY; |
| 113 | |
| 114 | pRbd->dataLength = 0; |
| 115 | |
| 116 | /* |
| 117 | * Increment BD count |
| 118 | */ |
| 119 | fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM; |
| 120 | |
| 121 | /* |
| 122 | * Now, we have an empty RxBD, notify FEC |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 123 | * Set Descriptor polling active |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 124 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 125 | out_be32(&fec->eth->r_des_active, 0x01000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /********************************************************************/ |
| 129 | static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec) |
| 130 | { |
| 131 | volatile FEC_TBD *pUsedTbd; |
| 132 | |
| 133 | #if (DEBUG & 0x1) |
| 134 | printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n", |
| 135 | fec->cleanTbdNum, fec->usedTbdIndex); |
| 136 | #endif |
| 137 | |
| 138 | /* |
| 139 | * process all the consumed TBDs |
| 140 | */ |
| 141 | while (fec->cleanTbdNum < FEC_TBD_NUM) { |
| 142 | pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex]; |
| 143 | if (pUsedTbd->status & FEC_TBD_READY) { |
| 144 | #if (DEBUG & 0x20) |
| 145 | printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex); |
| 146 | #endif |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * clean this buffer descriptor |
| 152 | */ |
| 153 | if (fec->usedTbdIndex == (FEC_TBD_NUM - 1)) |
| 154 | pUsedTbd->status = FEC_TBD_WRAP; |
| 155 | else |
| 156 | pUsedTbd->status = 0; |
| 157 | |
| 158 | /* |
| 159 | * update some indeces for a correct handling of the TBD ring |
| 160 | */ |
| 161 | fec->cleanTbdNum++; |
| 162 | fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /********************************************************************/ |
| 167 | static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac) |
| 168 | { |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 169 | u8 currByte; /* byte for which to compute the CRC */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 170 | int byte; /* loop - counter */ |
| 171 | int bit; /* loop - counter */ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 172 | u32 crc = 0xffffffff; /* initial value */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * The algorithm used is the following: |
| 176 | * we loop on each of the six bytes of the provided address, |
| 177 | * and we compute the CRC by left-shifting the previous |
| 178 | * value by one position, so that each bit in the current |
| 179 | * byte of the address may contribute the calculation. If |
| 180 | * the latter and the MSB in the CRC are different, then |
| 181 | * the CRC value so computed is also ex-ored with the |
| 182 | * "polynomium generator". The current byte of the address |
| 183 | * is also shifted right by one bit at each iteration. |
| 184 | * This is because the CRC generatore in hardware is implemented |
| 185 | * as a shift-register with as many ex-ores as the radixes |
| 186 | * in the polynomium. This suggests that we represent the |
| 187 | * polynomiumm itself as a 32-bit constant. |
| 188 | */ |
| 189 | for (byte = 0; byte < 6; byte++) { |
| 190 | currByte = mac[byte]; |
| 191 | for (bit = 0; bit < 8; bit++) { |
| 192 | if ((currByte & 0x01) ^ (crc & 0x01)) { |
| 193 | crc >>= 1; |
| 194 | crc = crc ^ 0xedb88320; |
| 195 | } else { |
| 196 | crc >>= 1; |
| 197 | } |
| 198 | currByte >>= 1; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | crc = crc >> 26; |
| 203 | |
| 204 | /* |
| 205 | * Set individual hash table register |
| 206 | */ |
| 207 | if (crc >= 32) { |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 208 | out_be32(&fec->eth->iaddr1, (1 << (crc - 32))); |
| 209 | out_be32(&fec->eth->iaddr2, 0); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 210 | } else { |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 211 | out_be32(&fec->eth->iaddr1, 0); |
| 212 | out_be32(&fec->eth->iaddr2, (1 << crc)); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Set physical address |
| 217 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 218 | out_be32(&fec->eth->paddr1, (mac[0] << 24) + (mac[1] << 16) + |
| 219 | (mac[2] << 8) + mac[3]); |
| 220 | out_be32(&fec->eth->paddr2, (mac[4] << 24) + (mac[5] << 16) + |
| 221 | 0x8808); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /********************************************************************/ |
| 225 | static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis) |
| 226 | { |
| 227 | mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv; |
| 228 | |
| 229 | #if (DEBUG & 0x1) |
| 230 | printf ("mpc512x_fec_init... Begin\n"); |
| 231 | #endif |
| 232 | |
| 233 | /* Set interrupt mask register */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 234 | out_be32(&fec->eth->imask, 0x00000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 235 | |
| 236 | /* Clear FEC-Lite interrupt event register(IEVENT) */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 237 | out_be32(&fec->eth->ievent, 0xffffffff); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 238 | |
| 239 | /* Set transmit fifo watermark register(X_WMRK), default = 64 */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 240 | out_be32(&fec->eth->x_wmrk, 0x0); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 241 | |
| 242 | /* Set Opcode/Pause Duration Register */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 243 | out_be32(&fec->eth->op_pause, 0x00010020); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 244 | |
Grzegorz Bernacki | 9425b96 | 2007-09-10 17:39:08 +0200 | [diff] [blame] | 245 | /* Frame length=1522; MII mode */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 246 | out_be32(&fec->eth->r_cntrl, (FEC_MAX_FRAME_LEN << 16) | 0x24); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 247 | |
| 248 | /* Half-duplex, heartbeat disabled */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 249 | out_be32(&fec->eth->x_cntrl, 0x00000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 250 | |
| 251 | /* Enable MIB counters */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 252 | out_be32(&fec->eth->mib_control, 0x0); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 253 | |
| 254 | /* Setup recv fifo start and buff size */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 255 | out_be32(&fec->eth->r_fstart, 0x500); |
| 256 | out_be32(&fec->eth->r_buff_size, FEC_BUFFER_SIZE); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 257 | |
| 258 | /* Setup BD base addresses */ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 259 | out_be32(&fec->eth->r_des_start, (u32)fec->bdBase->rbd); |
| 260 | out_be32(&fec->eth->x_des_start, (u32)fec->bdBase->tbd); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 261 | |
| 262 | /* DMA Control */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 263 | out_be32(&fec->eth->dma_control, 0xc0000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 264 | |
| 265 | /* Enable FEC */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 266 | setbits_be32(&fec->eth->ecntrl, 0x00000006); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 267 | |
| 268 | /* Initilize addresses and status words of BDs */ |
| 269 | mpc512x_fec_bd_init (fec); |
| 270 | |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 271 | /* Descriptor polling active */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 272 | out_be32(&fec->eth->r_des_active, 0x01000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 273 | |
| 274 | #if (DEBUG & 0x1) |
| 275 | printf("mpc512x_fec_init... Done \n"); |
| 276 | #endif |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | /********************************************************************/ |
| 281 | int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis) |
| 282 | { |
| 283 | mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv; |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 284 | const u8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 285 | int timeout = 1; |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 286 | u16 phyStatus; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 287 | |
| 288 | #if (DEBUG & 0x1) |
| 289 | printf ("mpc512x_fec_init_phy... Begin\n"); |
| 290 | #endif |
| 291 | |
| 292 | /* |
| 293 | * Clear FEC-Lite interrupt event register(IEVENT) |
| 294 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 295 | out_be32(&fec->eth->ievent, 0xffffffff); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 296 | |
| 297 | /* |
| 298 | * Set interrupt mask register |
| 299 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 300 | out_be32(&fec->eth->imask, 0x00000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 301 | |
| 302 | if (fec->xcv_type != SEVENWIRE) { |
| 303 | /* |
| 304 | * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock |
| 305 | * and do not drop the Preamble. |
| 306 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 307 | out_be32(&fec->eth->mii_speed, |
| 308 | (((gd->ips_clk / 1000000) / 5) + 1) << 1); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 309 | |
| 310 | /* |
| 311 | * Reset PHY, then delay 300ns |
| 312 | */ |
| 313 | miiphy_write (dev->name, phyAddr, 0x0, 0x8000); |
| 314 | udelay (1000); |
| 315 | |
| 316 | if (fec->xcv_type == MII10) { |
| 317 | /* |
| 318 | * Force 10Base-T, FDX operation |
| 319 | */ |
| 320 | #if (DEBUG & 0x2) |
| 321 | printf ("Forcing 10 Mbps ethernet link... "); |
| 322 | #endif |
| 323 | miiphy_read (dev->name, phyAddr, 0x1, &phyStatus); |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 324 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 325 | miiphy_write (dev->name, phyAddr, 0x0, 0x0180); |
| 326 | |
| 327 | timeout = 20; |
| 328 | do { /* wait for link status to go down */ |
| 329 | udelay (10000); |
| 330 | if ((timeout--) == 0) { |
| 331 | #if (DEBUG & 0x2) |
| 332 | printf ("hmmm, should not have waited..."); |
| 333 | #endif |
| 334 | break; |
| 335 | } |
| 336 | miiphy_read (dev->name, phyAddr, 0x1, &phyStatus); |
| 337 | #if (DEBUG & 0x2) |
| 338 | printf ("="); |
| 339 | #endif |
| 340 | } while ((phyStatus & 0x0004)); /* !link up */ |
| 341 | |
| 342 | timeout = 1000; |
| 343 | do { /* wait for link status to come back up */ |
| 344 | udelay (10000); |
| 345 | if ((timeout--) == 0) { |
| 346 | printf ("failed. Link is down.\n"); |
| 347 | break; |
| 348 | } |
| 349 | miiphy_read (dev->name, phyAddr, 0x1, &phyStatus); |
| 350 | #if (DEBUG & 0x2) |
| 351 | printf ("+"); |
| 352 | #endif |
| 353 | } while (!(phyStatus & 0x0004)); /* !link up */ |
| 354 | |
| 355 | #if (DEBUG & 0x2) |
| 356 | printf ("done.\n"); |
| 357 | #endif |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 358 | } else { /* MII100 */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 359 | /* |
| 360 | * Set the auto-negotiation advertisement register bits |
| 361 | */ |
| 362 | miiphy_write (dev->name, phyAddr, 0x4, 0x01e1); |
| 363 | |
| 364 | /* |
| 365 | * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation |
| 366 | */ |
| 367 | miiphy_write (dev->name, phyAddr, 0x0, 0x1200); |
| 368 | |
| 369 | /* |
| 370 | * Wait for AN completion |
| 371 | */ |
Wolfgang Denk | 818968c | 2008-09-12 13:52:21 +0200 | [diff] [blame] | 372 | timeout = 2500; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 373 | do { |
| 374 | udelay (1000); |
| 375 | |
| 376 | if ((timeout--) == 0) { |
| 377 | #if (DEBUG & 0x2) |
| 378 | printf ("PHY auto neg 0 failed...\n"); |
| 379 | #endif |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) { |
| 384 | #if (DEBUG & 0x2) |
| 385 | printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus); |
| 386 | #endif |
| 387 | return -1; |
| 388 | } |
| 389 | } while (!(phyStatus & 0x0004)); |
| 390 | |
| 391 | #if (DEBUG & 0x2) |
| 392 | printf ("PHY auto neg complete! \n"); |
| 393 | #endif |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | #if (DEBUG & 0x2) |
| 398 | if (fec->xcv_type != SEVENWIRE) |
| 399 | mpc512x_fec_phydump (dev->name); |
| 400 | #endif |
| 401 | |
| 402 | #if (DEBUG & 0x1) |
| 403 | printf ("mpc512x_fec_init_phy... Done \n"); |
| 404 | #endif |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | /********************************************************************/ |
| 409 | static void mpc512x_fec_halt (struct eth_device *dev) |
| 410 | { |
| 411 | mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv; |
| 412 | int counter = 0xffff; |
| 413 | |
| 414 | #if (DEBUG & 0x2) |
| 415 | if (fec->xcv_type != SEVENWIRE) |
| 416 | mpc512x_fec_phydump (dev->name); |
| 417 | #endif |
| 418 | |
| 419 | /* |
| 420 | * mask FEC chip interrupts |
| 421 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 422 | out_be32(&fec->eth->imask, 0); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 423 | |
| 424 | /* |
| 425 | * issue graceful stop command to the FEC transmitter if necessary |
| 426 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 427 | setbits_be32(&fec->eth->x_cntrl, 0x00000001); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * wait for graceful stop to register |
| 431 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 432 | while ((counter--) && (!(in_be32(&fec->eth->ievent) & 0x10000000))) |
| 433 | ; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 434 | |
| 435 | /* |
| 436 | * Disable the Ethernet Controller |
| 437 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 438 | clrbits_be32(&fec->eth->ecntrl, 0x00000002); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 439 | |
| 440 | /* |
| 441 | * Issue a reset command to the FEC chip |
| 442 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 443 | setbits_be32(&fec->eth->ecntrl, 0x1); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * wait at least 16 clock cycles |
| 447 | */ |
| 448 | udelay (10); |
| 449 | #if (DEBUG & 0x3) |
| 450 | printf ("Ethernet task stopped\n"); |
| 451 | #endif |
| 452 | } |
| 453 | |
| 454 | /********************************************************************/ |
| 455 | |
| 456 | static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data, |
| 457 | int data_length) |
| 458 | { |
| 459 | /* |
| 460 | * This routine transmits one frame. This routine only accepts |
| 461 | * 6-byte Ethernet addresses. |
| 462 | */ |
| 463 | mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv; |
| 464 | volatile FEC_TBD *pTbd; |
| 465 | |
| 466 | #if (DEBUG & 0x20) |
| 467 | printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status); |
| 468 | #endif |
| 469 | |
| 470 | /* |
| 471 | * Clear Tx BD ring at first |
| 472 | */ |
| 473 | mpc512x_fec_tbd_scrub (fec); |
| 474 | |
| 475 | /* |
| 476 | * Check for valid length of data. |
| 477 | */ |
| 478 | if ((data_length > 1500) || (data_length <= 0)) { |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Check the number of vacant TxBDs. |
| 484 | */ |
| 485 | if (fec->cleanTbdNum < 1) { |
| 486 | #if (DEBUG & 0x20) |
| 487 | printf ("No available TxBDs ...\n"); |
| 488 | #endif |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Get the first TxBD to send the mac header |
| 494 | */ |
| 495 | pTbd = &fec->bdBase->tbd[fec->tbdIndex]; |
| 496 | pTbd->dataLength = data_length; |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 497 | pTbd->dataPointer = (u32)eth_data; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 498 | pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY; |
| 499 | fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM; |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 500 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 501 | /* Activate transmit Buffer Descriptor polling */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 502 | out_be32(&fec->eth->x_des_active, 0x01000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 503 | |
| 504 | #if (DEBUG & 0x8) |
| 505 | printf ( "+" ); |
| 506 | #endif |
| 507 | |
| 508 | fec->cleanTbdNum -= 1; |
| 509 | |
| 510 | /* |
| 511 | * wait until frame is sent . |
| 512 | */ |
| 513 | while (pTbd->status & FEC_TBD_READY) { |
| 514 | udelay (10); |
| 515 | #if (DEBUG & 0x8) |
| 516 | printf ("TDB status = %04x\n", pTbd->status); |
| 517 | #endif |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /********************************************************************/ |
| 525 | static int mpc512x_fec_recv (struct eth_device *dev) |
| 526 | { |
| 527 | /* |
| 528 | * This command pulls one frame from the card |
| 529 | */ |
| 530 | mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv; |
| 531 | volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex]; |
| 532 | unsigned long ievent; |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 533 | int frame_length = 0; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 534 | |
| 535 | #if (DEBUG & 0x1) |
| 536 | printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex); |
| 537 | #endif |
| 538 | #if (DEBUG & 0x8) |
| 539 | printf( "-" ); |
| 540 | #endif |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 541 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 542 | /* |
| 543 | * Check if any critical events have happened |
| 544 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 545 | ievent = in_be32(&fec->eth->ievent); |
| 546 | out_be32(&fec->eth->ievent, ievent); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 547 | if (ievent & 0x20060000) { |
| 548 | /* BABT, Rx/Tx FIFO errors */ |
| 549 | mpc512x_fec_halt (dev); |
| 550 | mpc512x_fec_init (dev, NULL); |
| 551 | return 0; |
| 552 | } |
| 553 | if (ievent & 0x80000000) { |
| 554 | /* Heartbeat error */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 555 | setbits_be32(&fec->eth->x_cntrl, 0x00000001); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 556 | } |
| 557 | if (ievent & 0x10000000) { |
| 558 | /* Graceful stop complete */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 559 | if (in_be32(&fec->eth->x_cntrl) & 0x00000001) { |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 560 | mpc512x_fec_halt (dev); |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 561 | clrbits_be32(&fec->eth->x_cntrl, 0x00000001);; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 562 | mpc512x_fec_init (dev, NULL); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (!(pRbd->status & FEC_RBD_EMPTY)) { |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 567 | if (!(pRbd->status & FEC_RBD_ERR) && |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 568 | ((pRbd->dataLength - 4) > 14)) { |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 569 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 570 | /* |
| 571 | * Get buffer size |
| 572 | */ |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 573 | if (pRbd->status & FEC_RBD_LAST) |
| 574 | frame_length = pRbd->dataLength - 4; |
| 575 | else |
| 576 | frame_length = pRbd->dataLength; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 577 | #if (DEBUG & 0x20) |
| 578 | { |
| 579 | int i; |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 580 | printf ("recv data length 0x%08x data hdr: ", |
| 581 | pRbd->dataLength); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 582 | for (i = 0; i < 14; i++) |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 583 | printf ("%x ", *((u8*)pRbd->dataPointer + i)); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 584 | printf("\n"); |
| 585 | } |
| 586 | #endif |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 587 | /* |
| 588 | * Fill the buffer and pass it to upper layers |
| 589 | */ |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 590 | memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer, |
| 591 | frame_length - rx_buff_idx); |
| 592 | rx_buff_idx = frame_length; |
| 593 | |
| 594 | if (pRbd->status & FEC_RBD_LAST) { |
| 595 | NetReceive ((uchar*)rx_buff, frame_length); |
| 596 | rx_buff_idx = 0; |
| 597 | } |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Reset buffer descriptor as empty |
| 602 | */ |
| 603 | mpc512x_fec_rbd_clean (fec, pRbd); |
| 604 | } |
| 605 | |
| 606 | /* Try to fill Buffer Descriptors */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 607 | out_be32(&fec->eth->r_des_active, 0x01000000); |
| 608 | |
Grzegorz Bernacki | 7b654b9 | 2007-09-07 17:09:21 +0200 | [diff] [blame] | 609 | return frame_length; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /********************************************************************/ |
| 613 | int mpc512x_fec_initialize (bd_t * bis) |
| 614 | { |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 615 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 616 | mpc512x_fec_priv *fec; |
| 617 | struct eth_device *dev; |
| 618 | int i; |
| 619 | char *tmp, *end, env_enetaddr[6]; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 620 | void * bd; |
| 621 | |
| 622 | fec = (mpc512x_fec_priv *) malloc (sizeof(*fec)); |
| 623 | dev = (struct eth_device *) malloc (sizeof(*dev)); |
| 624 | memset (dev, 0, sizeof *dev); |
| 625 | |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 626 | fec->eth = &im->fec; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 627 | |
| 628 | # ifndef CONFIG_FEC_10MBIT |
| 629 | fec->xcv_type = MII100; |
| 630 | # else |
| 631 | fec->xcv_type = MII10; |
| 632 | # endif |
| 633 | dev->priv = (void *)fec; |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 634 | dev->iobase = (int)&im->fec; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 635 | dev->init = mpc512x_fec_init; |
| 636 | dev->halt = mpc512x_fec_halt; |
| 637 | dev->send = mpc512x_fec_send; |
| 638 | dev->recv = mpc512x_fec_recv; |
| 639 | |
| 640 | sprintf (dev->name, "FEC ETHERNET"); |
| 641 | eth_register (dev); |
| 642 | |
Wolfgang Denk | 56cbd02 | 2007-08-12 14:27:39 +0200 | [diff] [blame] | 643 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 644 | miiphy_register (dev->name, |
| 645 | fec512x_miiphy_read, fec512x_miiphy_write); |
| 646 | #endif |
| 647 | |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 648 | /* Clean up space FEC's MIB and FIFO RAM ...*/ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 649 | memset ((void *)&im->fec.mib, 0x00, sizeof(im->fec.mib)); |
| 650 | memset ((void *)&im->fec.fifo, 0x00, sizeof(im->fec.fifo)); |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 651 | |
| 652 | /* |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 653 | * Malloc space for BDs (must be quad word-aligned) |
Wolfgang Denk | 530181f | 2007-08-02 21:27:46 +0200 | [diff] [blame] | 654 | * this pointer is lost, so cannot be freed |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 655 | */ |
| 656 | bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f); |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 657 | fec->bdBase = (mpc512x_buff_descs*)((u32)bd & 0xfffffff0); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 658 | memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f); |
| 659 | |
| 660 | /* |
| 661 | * Set interrupt mask register |
| 662 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 663 | out_be32(&fec->eth->imask, 0x00000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Clear FEC-Lite interrupt event register(IEVENT) |
| 667 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 668 | out_be32(&fec->eth->ievent, 0xffffffff); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 669 | |
| 670 | /* |
| 671 | * Try to set the mac address now. The fec mac address is |
| 672 | * a garbage after reset. When not using fec for booting |
| 673 | * the Linux fec driver will try to work with this garbage. |
| 674 | */ |
| 675 | tmp = getenv ("ethaddr"); |
| 676 | if (tmp) { |
| 677 | for (i=0; i<6; i++) { |
| 678 | env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0; |
| 679 | if (tmp) |
| 680 | tmp = (*end) ? end+1 : end; |
| 681 | } |
| 682 | mpc512x_fec_set_hwaddr (fec, env_enetaddr); |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 683 | out_be32(&fec->eth->gaddr1, 0x00000000); |
| 684 | out_be32(&fec->eth->gaddr2, 0x00000000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | mpc512x_fec_init_phy (dev, bis); |
| 688 | |
| 689 | return 1; |
| 690 | } |
| 691 | |
| 692 | /* MII-interface related functions */ |
| 693 | /********************************************************************/ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 694 | int fec512x_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal) |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 695 | { |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 696 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
| 697 | volatile fec512x_t *eth = &im->fec; |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 698 | u32 reg; /* convenient holder for the PHY register */ |
| 699 | u32 phy; /* convenient holder for the PHY */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 700 | int timeout = 0xffff; |
| 701 | |
| 702 | /* |
| 703 | * reading from any PHY's register is done by properly |
| 704 | * programming the FEC's MII data register. |
| 705 | */ |
| 706 | reg = regAddr << FEC_MII_DATA_RA_SHIFT; |
| 707 | phy = phyAddr << FEC_MII_DATA_PA_SHIFT; |
| 708 | |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 709 | out_be32(ð->mii_data, FEC_MII_DATA_ST | |
| 710 | FEC_MII_DATA_OP_RD | |
| 711 | FEC_MII_DATA_TA | |
| 712 | phy | reg); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * wait for the related interrupt |
| 716 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 717 | while ((timeout--) && (!(in_be32(ð->ievent) & 0x00800000))) |
| 718 | ; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 719 | |
| 720 | if (timeout == 0) { |
| 721 | #if (DEBUG & 0x2) |
| 722 | printf ("Read MDIO failed...\n"); |
| 723 | #endif |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * clear mii interrupt bit |
| 729 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 730 | out_be32(ð->ievent, 0x00800000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 731 | |
| 732 | /* |
| 733 | * it's now safe to read the PHY's register |
| 734 | */ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 735 | *retVal = (u16) in_be32(ð->mii_data); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /********************************************************************/ |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 741 | int fec512x_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data) |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 742 | { |
Wolfgang Denk | ef5f37d | 2009-05-16 10:47:44 +0200 | [diff] [blame] | 743 | volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR; |
| 744 | volatile fec512x_t *eth = &im->fec; |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 745 | u32 reg; /* convenient holder for the PHY register */ |
| 746 | u32 phy; /* convenient holder for the PHY */ |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 747 | int timeout = 0xffff; |
| 748 | |
| 749 | reg = regAddr << FEC_MII_DATA_RA_SHIFT; |
| 750 | phy = phyAddr << FEC_MII_DATA_PA_SHIFT; |
| 751 | |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 752 | out_be32(ð->mii_data, FEC_MII_DATA_ST | |
| 753 | FEC_MII_DATA_OP_WR | |
| 754 | FEC_MII_DATA_TA | |
| 755 | phy | reg | data); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * wait for the MII interrupt |
| 759 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 760 | while ((timeout--) && (!(in_be32(ð->ievent) & 0x00800000))) |
| 761 | ; |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 762 | |
| 763 | if (timeout == 0) { |
| 764 | #if (DEBUG & 0x2) |
| 765 | printf ("Write MDIO failed...\n"); |
| 766 | #endif |
| 767 | return -1; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * clear MII interrupt bit |
| 772 | */ |
Wolfgang Denk | b853995 | 2009-05-16 10:47:43 +0200 | [diff] [blame] | 773 | out_be32(ð->ievent, 0x00800000); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | #if (DEBUG & 0x40) |
Wolfgang Denk | f342f86 | 2009-05-16 10:47:45 +0200 | [diff] [blame] | 779 | static u32 local_crc32 (char *string, unsigned int crc_value, int len) |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 780 | { |
| 781 | int i; |
| 782 | char c; |
| 783 | unsigned int crc, count; |
| 784 | |
| 785 | /* |
| 786 | * crc32 algorithm |
| 787 | */ |
| 788 | /* |
| 789 | * crc = 0xffffffff; * The initialized value should be 0xffffffff |
| 790 | */ |
| 791 | crc = crc_value; |
| 792 | |
| 793 | for (i = len; --i >= 0;) { |
| 794 | c = *string++; |
| 795 | for (count = 0; count < 8; count++) { |
| 796 | if ((c & 0x01) ^ (crc & 0x01)) { |
| 797 | crc >>= 1; |
| 798 | crc = crc ^ 0xedb88320; |
| 799 | } else { |
| 800 | crc >>= 1; |
| 801 | } |
| 802 | c >>= 1; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * In big endian system, do byte swaping for crc value |
| 808 | */ |
| 809 | /**/ return crc; |
| 810 | } |
| 811 | #endif /* DEBUG */ |
| 812 | |
| 813 | #endif /* CONFIG_MPC512x_FEC */ |