wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 1 | /* |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 2 | * Freescale Three Speed Ethernet Controller driver |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 3 | * |
| 4 | * This software may be used and distributed according to the |
| 5 | * terms of the GNU Public License, Version 2, incorporated |
| 6 | * herein by reference. |
| 7 | * |
Mingkai Hu | a65e610 | 2011-01-27 12:52:45 +0800 | [diff] [blame] | 8 | * Copyright 2004-2011 Freescale Semiconductor, Inc. |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 9 | * (C) Copyright 2003, Motorola, Inc. |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 10 | * author Andy Fleming |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <config.h> |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 15 | #include <common.h> |
| 16 | #include <malloc.h> |
| 17 | #include <net.h> |
| 18 | #include <command.h> |
Andy Fleming | c067fc1 | 2008-08-31 16:33:25 -0500 | [diff] [blame] | 19 | #include <tsec.h> |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 20 | #include <fsl_mdio.h> |
Kim Phillips | ae4dd97 | 2009-08-24 14:32:26 -0500 | [diff] [blame] | 21 | #include <asm/errno.h> |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 22 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 25 | #define TX_BUF_CNT 2 |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 26 | |
Jon Loeliger | b7ced08 | 2006-10-10 17:03:43 -0500 | [diff] [blame] | 27 | static uint rxIdx; /* index of the current RX buffer */ |
| 28 | static uint txIdx; /* index of the current TX buffer */ |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 29 | |
| 30 | typedef volatile struct rtxbd { |
| 31 | txbd8_t txbd[TX_BUF_CNT]; |
| 32 | rxbd8_t rxbd[PKTBUFSRX]; |
Jon Loeliger | b7ced08 | 2006-10-10 17:03:43 -0500 | [diff] [blame] | 33 | } RTXBD; |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 34 | |
Andy Fleming | fecff2b | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 35 | #define MAXCONTROLLERS (8) |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 36 | |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 37 | static struct tsec_private *privlist[MAXCONTROLLERS]; |
Andy Fleming | fecff2b | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 38 | static int num_tsecs = 0; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 39 | |
wdenk | 9c53f40 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 40 | #ifdef __GNUC__ |
| 41 | static RTXBD rtx __attribute__ ((aligned(8))); |
| 42 | #else |
| 43 | #error "rtx must be 64-bit aligned" |
| 44 | #endif |
| 45 | |
Andy Fleming | fecff2b | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 46 | /* Default initializations for TSEC controllers. */ |
| 47 | |
| 48 | static struct tsec_info_struct tsec_info[] = { |
| 49 | #ifdef CONFIG_TSEC1 |
| 50 | STD_TSEC_INFO(1), /* TSEC1 */ |
| 51 | #endif |
| 52 | #ifdef CONFIG_TSEC2 |
| 53 | STD_TSEC_INFO(2), /* TSEC2 */ |
| 54 | #endif |
| 55 | #ifdef CONFIG_MPC85XX_FEC |
| 56 | { |
| 57 | .regs = (tsec_t *)(TSEC_BASE_ADDR + 0x2000), |
Andy Fleming | fecff2b | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 58 | .devname = CONFIG_MPC85XX_FEC_NAME, |
| 59 | .phyaddr = FEC_PHY_ADDR, |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 60 | .flags = FEC_FLAGS, |
| 61 | .mii_devname = DEFAULT_MII_NAME |
Andy Fleming | fecff2b | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 62 | }, /* FEC */ |
| 63 | #endif |
| 64 | #ifdef CONFIG_TSEC3 |
| 65 | STD_TSEC_INFO(3), /* TSEC3 */ |
| 66 | #endif |
| 67 | #ifdef CONFIG_TSEC4 |
| 68 | STD_TSEC_INFO(4), /* TSEC4 */ |
| 69 | #endif |
| 70 | }; |
| 71 | |
Andy Fleming | ac65e07 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 72 | #define TBIANA_SETTINGS ( \ |
| 73 | TBIANA_ASYMMETRIC_PAUSE \ |
| 74 | | TBIANA_SYMMETRIC_PAUSE \ |
| 75 | | TBIANA_FULL_DUPLEX \ |
| 76 | ) |
| 77 | |
Felix Radensky | 27f98e0 | 2010-06-28 01:57:39 +0300 | [diff] [blame] | 78 | /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */ |
| 79 | #ifndef CONFIG_TSEC_TBICR_SETTINGS |
Kumar Gala | c1457f9 | 2010-12-01 22:55:54 -0600 | [diff] [blame] | 80 | #define CONFIG_TSEC_TBICR_SETTINGS ( \ |
Andy Fleming | ac65e07 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 81 | TBICR_PHY_RESET \ |
Kumar Gala | c1457f9 | 2010-12-01 22:55:54 -0600 | [diff] [blame] | 82 | | TBICR_ANEG_ENABLE \ |
Andy Fleming | ac65e07 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 83 | | TBICR_FULL_DUPLEX \ |
| 84 | | TBICR_SPEED1_SET \ |
| 85 | ) |
Felix Radensky | 27f98e0 | 2010-06-28 01:57:39 +0300 | [diff] [blame] | 86 | #endif /* CONFIG_TSEC_TBICR_SETTINGS */ |
Peter Tyser | 583c1f4 | 2009-11-03 17:52:07 -0600 | [diff] [blame] | 87 | |
Andy Fleming | ac65e07 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 88 | /* Configure the TBI for SGMII operation */ |
| 89 | static void tsec_configure_serdes(struct tsec_private *priv) |
| 90 | { |
Peter Tyser | 4ef03c0 | 2009-11-09 13:09:46 -0600 | [diff] [blame] | 91 | /* Access TBI PHY registers at given TSEC register offset as opposed |
| 92 | * to the register offset used for external PHY accesses */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 93 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
| 94 | 0, TBI_ANA, TBIANA_SETTINGS); |
| 95 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
| 96 | 0, TBI_TBICON, TBICON_CLK_SELECT); |
| 97 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
| 98 | 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS); |
David Updegraff | 0451b01 | 2007-04-20 14:34:48 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 101 | #ifdef CONFIG_MCAST_TFTP |
| 102 | |
| 103 | /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */ |
| 104 | |
| 105 | /* Set the appropriate hash bit for the given addr */ |
| 106 | |
| 107 | /* The algorithm works like so: |
| 108 | * 1) Take the Destination Address (ie the multicast address), and |
| 109 | * do a CRC on it (little endian), and reverse the bits of the |
| 110 | * result. |
| 111 | * 2) Use the 8 most significant bits as a hash into a 256-entry |
| 112 | * table. The table is controlled through 8 32-bit registers: |
| 113 | * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is |
| 114 | * gaddr7. This means that the 3 most significant bits in the |
| 115 | * hash index which gaddr register to use, and the 5 other bits |
| 116 | * indicate which bit (assuming an IBM numbering scheme, which |
| 117 | * for PowerPC (tm) is usually the case) in the tregister holds |
| 118 | * the entry. */ |
| 119 | static int |
| 120 | tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set) |
| 121 | { |
| 122 | struct tsec_private *priv = privlist[1]; |
| 123 | volatile tsec_t *regs = priv->regs; |
| 124 | volatile u32 *reg_array, value; |
| 125 | u8 result, whichbit, whichreg; |
| 126 | |
| 127 | result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff); |
| 128 | whichbit = result & 0x1f; /* the 5 LSB = which bit to set */ |
| 129 | whichreg = result >> 5; /* the 3 MSB = which reg to set it in */ |
| 130 | value = (1 << (31-whichbit)); |
| 131 | |
| 132 | reg_array = &(regs->hash.gaddr0); |
| 133 | |
| 134 | if (set) { |
| 135 | reg_array[whichreg] |= value; |
| 136 | } else { |
| 137 | reg_array[whichreg] &= ~value; |
| 138 | } |
| 139 | return 0; |
| 140 | } |
| 141 | #endif /* Multicast TFTP ? */ |
| 142 | |
| 143 | /* Initialized required registers to appropriate values, zeroing |
| 144 | * those we don't care about (unless zero is bad, in which case, |
| 145 | * choose a more appropriate value) |
| 146 | */ |
| 147 | static void init_registers(tsec_t *regs) |
| 148 | { |
| 149 | /* Clear IEVENT */ |
| 150 | out_be32(®s->ievent, IEVENT_INIT_CLEAR); |
| 151 | |
| 152 | out_be32(®s->imask, IMASK_INIT_CLEAR); |
| 153 | |
| 154 | out_be32(®s->hash.iaddr0, 0); |
| 155 | out_be32(®s->hash.iaddr1, 0); |
| 156 | out_be32(®s->hash.iaddr2, 0); |
| 157 | out_be32(®s->hash.iaddr3, 0); |
| 158 | out_be32(®s->hash.iaddr4, 0); |
| 159 | out_be32(®s->hash.iaddr5, 0); |
| 160 | out_be32(®s->hash.iaddr6, 0); |
| 161 | out_be32(®s->hash.iaddr7, 0); |
| 162 | |
| 163 | out_be32(®s->hash.gaddr0, 0); |
| 164 | out_be32(®s->hash.gaddr1, 0); |
| 165 | out_be32(®s->hash.gaddr2, 0); |
| 166 | out_be32(®s->hash.gaddr3, 0); |
| 167 | out_be32(®s->hash.gaddr4, 0); |
| 168 | out_be32(®s->hash.gaddr5, 0); |
| 169 | out_be32(®s->hash.gaddr6, 0); |
| 170 | out_be32(®s->hash.gaddr7, 0); |
| 171 | |
| 172 | out_be32(®s->rctrl, 0x00000000); |
| 173 | |
| 174 | /* Init RMON mib registers */ |
| 175 | memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t)); |
| 176 | |
| 177 | out_be32(®s->rmon.cam1, 0xffffffff); |
| 178 | out_be32(®s->rmon.cam2, 0xffffffff); |
| 179 | |
| 180 | out_be32(®s->mrblr, MRBLR_INIT_SETTINGS); |
| 181 | |
| 182 | out_be32(®s->minflr, MINFLR_INIT_SETTINGS); |
| 183 | |
| 184 | out_be32(®s->attr, ATTR_INIT_SETTINGS); |
| 185 | out_be32(®s->attreli, ATTRELI_INIT_SETTINGS); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | /* Configure maccfg2 based on negotiated speed and duplex |
| 190 | * reported by PHY handling code |
| 191 | */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 192 | static void adjust_link(struct tsec_private *priv, struct phy_device *phydev) |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 193 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 194 | tsec_t *regs = priv->regs; |
| 195 | u32 ecntrl, maccfg2; |
| 196 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 197 | if (!phydev->link) { |
| 198 | printf("%s: No link.\n", phydev->dev->name); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
| 202 | /* clear all bits relative with interface mode */ |
| 203 | ecntrl = in_be32(®s->ecntrl); |
| 204 | ecntrl &= ~ECNTRL_R100; |
| 205 | |
| 206 | maccfg2 = in_be32(®s->maccfg2); |
| 207 | maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX); |
| 208 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 209 | if (phydev->duplex) |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 210 | maccfg2 |= MACCFG2_FULL_DUPLEX; |
| 211 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 212 | switch (phydev->speed) { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 213 | case 1000: |
| 214 | maccfg2 |= MACCFG2_GMII; |
| 215 | break; |
| 216 | case 100: |
| 217 | case 10: |
| 218 | maccfg2 |= MACCFG2_MII; |
| 219 | |
| 220 | /* Set R100 bit in all modes although |
| 221 | * it is only used in RGMII mode |
| 222 | */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 223 | if (phydev->speed == 100) |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 224 | ecntrl |= ECNTRL_R100; |
| 225 | break; |
| 226 | default: |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 227 | printf("%s: Speed was bad\n", phydev->dev->name); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 228 | break; |
| 229 | } |
| 230 | |
| 231 | out_be32(®s->ecntrl, ecntrl); |
| 232 | out_be32(®s->maccfg2, maccfg2); |
wdenk | f41ff3b | 2005-04-04 23:43:44 +0000 | [diff] [blame] | 233 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 234 | printf("Speed: %d, %s duplex%s\n", phydev->speed, |
| 235 | (phydev->duplex) ? "full" : "half", |
| 236 | (phydev->port == PORT_FIBRE) ? ", fiber mode" : ""); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 237 | } |
wdenk | bfad55d | 2005-03-14 23:56:42 +0000 | [diff] [blame] | 238 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 239 | /* Set up the buffers and their descriptors, and bring up the |
| 240 | * interface |
Jon Loeliger | b7ced08 | 2006-10-10 17:03:43 -0500 | [diff] [blame] | 241 | */ |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 242 | static void startup_tsec(struct eth_device *dev) |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 243 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 244 | int i; |
| 245 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 246 | tsec_t *regs = priv->regs; |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 247 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 248 | /* reset the indices to zero */ |
| 249 | rxIdx = 0; |
| 250 | txIdx = 0; |
| 251 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 252 | /* Point to the buffer descriptors */ |
| 253 | out_be32(®s->tbase, (unsigned int)(&rtx.txbd[txIdx])); |
| 254 | out_be32(®s->rbase, (unsigned int)(&rtx.rxbd[rxIdx])); |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 255 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 256 | /* Initialize the Rx Buffer descriptors */ |
| 257 | for (i = 0; i < PKTBUFSRX; i++) { |
| 258 | rtx.rxbd[i].status = RXBD_EMPTY; |
| 259 | rtx.rxbd[i].length = 0; |
| 260 | rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i]; |
| 261 | } |
| 262 | rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP; |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 263 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 264 | /* Initialize the TX Buffer Descriptors */ |
| 265 | for (i = 0; i < TX_BUF_CNT; i++) { |
| 266 | rtx.txbd[i].status = 0; |
| 267 | rtx.txbd[i].length = 0; |
| 268 | rtx.txbd[i].bufPtr = 0; |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 269 | } |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 270 | rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP; |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 271 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 272 | /* Enable Transmit and Receive */ |
| 273 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN); |
| 274 | |
| 275 | /* Tell the DMA it is clear to go */ |
| 276 | setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS); |
| 277 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); |
| 278 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 279 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 282 | /* This returns the status bits of the device. The return value |
| 283 | * is never checked, and this is what the 8260 driver did, so we |
| 284 | * do the same. Presumably, this would be zero if there were no |
| 285 | * errors |
| 286 | */ |
| 287 | static int tsec_send(struct eth_device *dev, volatile void *packet, int length) |
| 288 | { |
| 289 | int i; |
| 290 | int result = 0; |
| 291 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 292 | tsec_t *regs = priv->regs; |
Wolfgang Denk | f0c4e46 | 2006-03-12 22:50:55 +0100 | [diff] [blame] | 293 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 294 | /* Find an empty buffer descriptor */ |
| 295 | for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) { |
| 296 | if (i >= TOUT_LOOP) { |
| 297 | debug("%s: tsec: tx buffers full\n", dev->name); |
| 298 | return result; |
| 299 | } |
| 300 | } |
Dave Liu | a304a28 | 2008-01-11 18:45:28 +0800 | [diff] [blame] | 301 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 302 | rtx.txbd[txIdx].bufPtr = (uint) packet; |
| 303 | rtx.txbd[txIdx].length = length; |
| 304 | rtx.txbd[txIdx].status |= |
| 305 | (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT); |
Li Yang | 25e38bd | 2011-01-27 19:02:50 +0800 | [diff] [blame] | 306 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 307 | /* Tell the DMA to go */ |
| 308 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 309 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 310 | /* Wait for buffer to be transmitted */ |
| 311 | for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) { |
| 312 | if (i >= TOUT_LOOP) { |
| 313 | debug("%s: tsec: tx error\n", dev->name); |
| 314 | return result; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 319 | result = rtx.txbd[txIdx].status & TXBD_STATS; |
| 320 | |
| 321 | return result; |
| 322 | } |
| 323 | |
| 324 | static int tsec_recv(struct eth_device *dev) |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 325 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 326 | int length; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 327 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 328 | tsec_t *regs = priv->regs; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 329 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 330 | while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) { |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 331 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 332 | length = rtx.rxbd[rxIdx].length; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 333 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 334 | /* Send the packet up if there were no errors */ |
| 335 | if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) { |
| 336 | NetReceive(NetRxPackets[rxIdx], length - 4); |
| 337 | } else { |
| 338 | printf("Got error %x\n", |
| 339 | (rtx.rxbd[rxIdx].status & RXBD_STATS)); |
Andy Fleming | b2d14f4 | 2007-05-09 00:54:20 -0500 | [diff] [blame] | 340 | } |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 341 | |
| 342 | rtx.rxbd[rxIdx].length = 0; |
| 343 | |
| 344 | /* Set the wrap bit if this is the last element in the list */ |
| 345 | rtx.rxbd[rxIdx].status = |
| 346 | RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0); |
| 347 | |
| 348 | rxIdx = (rxIdx + 1) % PKTBUFSRX; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 351 | if (in_be32(®s->ievent) & IEVENT_BSY) { |
| 352 | out_be32(®s->ievent, IEVENT_BSY); |
| 353 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 356 | return -1; |
| 357 | |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 360 | /* Stop the interface */ |
| 361 | static void tsec_halt(struct eth_device *dev) |
| 362 | { |
| 363 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 364 | tsec_t *regs = priv->regs; |
| 365 | |
| 366 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 367 | setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 368 | |
| 369 | while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC)) |
| 370 | != (IEVENT_GRSC | IEVENT_GTSC)) |
| 371 | ; |
| 372 | |
| 373 | clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN); |
| 374 | |
| 375 | /* Shut down the PHY, as needed */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 376 | phy_shutdown(priv->phydev); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Initializes data structures and registers for the controller, |
| 380 | * and brings the interface up. Returns the link status, meaning |
| 381 | * that it returns success if the link is up, failure otherwise. |
| 382 | * This allows u-boot to find the first active controller. |
Jon Loeliger | b7ced08 | 2006-10-10 17:03:43 -0500 | [diff] [blame] | 383 | */ |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 384 | static int tsec_init(struct eth_device *dev, bd_t * bd) |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 385 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 386 | uint tempval; |
| 387 | char tmpbuf[MAC_ADDR_LEN]; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 388 | int i; |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 389 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 390 | tsec_t *regs = priv->regs; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 391 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 392 | /* Make sure the controller is stopped */ |
| 393 | tsec_halt(dev); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 394 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 395 | /* Init MACCFG2. Defaults to GMII */ |
| 396 | out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 397 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 398 | /* Init ECNTRL */ |
| 399 | out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 400 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 401 | /* Copy the station address into the address registers. |
| 402 | * Backwards, because little endian MACS are dumb */ |
| 403 | for (i = 0; i < MAC_ADDR_LEN; i++) |
| 404 | tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i]; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 405 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 406 | tempval = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) | |
| 407 | tmpbuf[3]; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 408 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 409 | out_be32(®s->macstnaddr1, tempval); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 410 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 411 | tempval = *((uint *) (tmpbuf + 4)); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 412 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 413 | out_be32(®s->macstnaddr2, tempval); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 414 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 415 | /* Clear out (for the most part) the other registers */ |
| 416 | init_registers(regs); |
| 417 | |
| 418 | /* Ready the device for tx/rx */ |
| 419 | startup_tsec(dev); |
| 420 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 421 | /* Start up the PHY */ |
| 422 | phy_startup(priv->phydev); |
| 423 | |
| 424 | adjust_link(priv, priv->phydev); |
| 425 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 426 | /* If there's no link, fail */ |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 427 | return priv->phydev->link ? 0 : -1; |
| 428 | } |
| 429 | |
| 430 | static phy_interface_t tsec_get_interface(struct tsec_private *priv) |
| 431 | { |
| 432 | tsec_t *regs = priv->regs; |
| 433 | u32 ecntrl; |
| 434 | |
| 435 | ecntrl = in_be32(®s->ecntrl); |
| 436 | |
| 437 | if (ecntrl & ECNTRL_SGMII_MODE) |
| 438 | return PHY_INTERFACE_MODE_SGMII; |
| 439 | |
| 440 | if (ecntrl & ECNTRL_TBI_MODE) { |
| 441 | if (ecntrl & ECNTRL_REDUCED_MODE) |
| 442 | return PHY_INTERFACE_MODE_RTBI; |
| 443 | else |
| 444 | return PHY_INTERFACE_MODE_TBI; |
| 445 | } |
| 446 | |
| 447 | if (ecntrl & ECNTRL_REDUCED_MODE) { |
| 448 | if (ecntrl & ECNTRL_REDUCED_MII_MODE) |
| 449 | return PHY_INTERFACE_MODE_RMII; |
| 450 | else { |
| 451 | phy_interface_t interface = priv->interface; |
| 452 | |
| 453 | /* |
| 454 | * This isn't autodetected, so it must |
| 455 | * be set by the platform code. |
| 456 | */ |
| 457 | if ((interface == PHY_INTERFACE_MODE_RGMII_ID) || |
| 458 | (interface == PHY_INTERFACE_MODE_RGMII_TXID) || |
| 459 | (interface == PHY_INTERFACE_MODE_RGMII_RXID)) |
| 460 | return interface; |
| 461 | |
| 462 | return PHY_INTERFACE_MODE_RGMII; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (priv->flags & TSEC_GIGABIT) |
| 467 | return PHY_INTERFACE_MODE_GMII; |
| 468 | |
| 469 | return PHY_INTERFACE_MODE_MII; |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 470 | } |
| 471 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 472 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 473 | /* Discover which PHY is attached to the device, and configure it |
| 474 | * properly. If the PHY is not recognized, then return 0 |
| 475 | * (failure). Otherwise, return 1 |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 476 | */ |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 477 | static int init_phy(struct eth_device *dev) |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 478 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 479 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 480 | struct phy_device *phydev; |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 481 | tsec_t *regs = priv->regs; |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 482 | u32 supported = (SUPPORTED_10baseT_Half | |
| 483 | SUPPORTED_10baseT_Full | |
| 484 | SUPPORTED_100baseT_Half | |
| 485 | SUPPORTED_100baseT_Full); |
| 486 | |
| 487 | if (priv->flags & TSEC_GIGABIT) |
| 488 | supported |= SUPPORTED_1000baseT_Full; |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 489 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 490 | /* Assign a Physical address to the TBI */ |
| 491 | out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE); |
| 492 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 493 | priv->interface = tsec_get_interface(priv); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 494 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 495 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) |
| 496 | tsec_configure_serdes(priv); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 497 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 498 | phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 499 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 500 | phydev->supported &= supported; |
| 501 | phydev->advertising = phydev->supported; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 502 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 503 | priv->phydev = phydev; |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 504 | |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 505 | phy_config(phydev); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 506 | |
| 507 | return 1; |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 510 | /* Initialize device structure. Returns success if PHY |
| 511 | * initialization succeeded (i.e. if it recognizes the PHY) |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 512 | */ |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 513 | static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info) |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 514 | { |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 515 | struct eth_device *dev; |
| 516 | int i; |
| 517 | struct tsec_private *priv; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 518 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 519 | dev = (struct eth_device *)malloc(sizeof *dev); |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 520 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 521 | if (NULL == dev) |
| 522 | return 0; |
wdenk | 78924a7 | 2004-04-18 21:45:42 +0000 | [diff] [blame] | 523 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 524 | memset(dev, 0, sizeof *dev); |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 525 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 526 | priv = (struct tsec_private *)malloc(sizeof(*priv)); |
| 527 | |
| 528 | if (NULL == priv) |
| 529 | return 0; |
| 530 | |
| 531 | privlist[num_tsecs++] = priv; |
| 532 | priv->regs = tsec_info->regs; |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 533 | priv->phyregs_sgmii = tsec_info->miiregs_sgmii; |
| 534 | |
| 535 | priv->phyaddr = tsec_info->phyaddr; |
| 536 | priv->flags = tsec_info->flags; |
wdenk | a445ddf | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 537 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 538 | sprintf(dev->name, tsec_info->devname); |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 539 | priv->interface = tsec_info->interface; |
| 540 | priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname); |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 541 | dev->iobase = 0; |
| 542 | dev->priv = priv; |
| 543 | dev->init = tsec_init; |
| 544 | dev->halt = tsec_halt; |
| 545 | dev->send = tsec_send; |
| 546 | dev->recv = tsec_recv; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 547 | #ifdef CONFIG_MCAST_TFTP |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 548 | dev->mcast = tsec_mcast_addr; |
| 549 | #endif |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 550 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 551 | /* Tell u-boot to get the addr from the env */ |
| 552 | for (i = 0; i < 6; i++) |
| 553 | dev->enetaddr[i] = 0; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 554 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 555 | eth_register(dev); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 556 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 557 | /* Reset the MAC */ |
| 558 | setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
| 559 | udelay(2); /* Soft Reset must be asserted for 3 TX clocks */ |
| 560 | clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 561 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 562 | /* Try to initialize PHY here, and return */ |
| 563 | return init_phy(dev); |
| 564 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 565 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 566 | /* |
| 567 | * Initialize all the TSEC devices |
| 568 | * |
| 569 | * Returns the number of TSEC devices that were initialized |
| 570 | */ |
| 571 | int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num) |
| 572 | { |
| 573 | int i; |
| 574 | int ret, count = 0; |
| 575 | |
| 576 | for (i = 0; i < num; i++) { |
| 577 | ret = tsec_initialize(bis, &tsecs[i]); |
| 578 | if (ret > 0) |
| 579 | count += ret; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 580 | } |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 581 | |
| 582 | return count; |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 583 | } |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 584 | |
| 585 | int tsec_standard_init(bd_t *bis) |
| 586 | { |
Andy Fleming | 422effd | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 587 | struct fsl_pq_mdio_info info; |
| 588 | |
| 589 | info.regs = (struct tsec_mii_mng *)CONFIG_SYS_MDIO_BASE_ADDR; |
| 590 | info.name = DEFAULT_MII_NAME; |
| 591 | |
| 592 | fsl_pq_mdio_init(bis, &info); |
| 593 | |
Mingkai Hu | e0653bf | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 594 | return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info)); |
| 595 | } |