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