Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 2 | /* |
wdenk | 97d80fc | 2004-06-09 00:34:46 +0000 | [diff] [blame] | 3 | * Freescale Three Speed Ethernet Controller driver |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 4 | * |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 5 | * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc. |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 6 | * (C) Copyright 2003, Motorola, Inc. |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 7 | * author Andy Fleming |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <config.h> |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 11 | #include <common.h> |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 12 | #include <dm.h> |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <net.h> |
| 15 | #include <command.h> |
Andy Fleming | dd3d1f5 | 2008-08-31 16:33:25 -0500 | [diff] [blame] | 16 | #include <tsec.h> |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 17 | #include <fsl_mdio.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 19 | #include <asm/processor.h> |
Alison Wang | 52d00a8 | 2014-09-05 13:52:38 +0800 | [diff] [blame] | 20 | #include <asm/io.h> |
wdenk | 42d1f03 | 2003-10-15 23:53:47 +0000 | [diff] [blame] | 21 | |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 22 | #ifndef CONFIG_DM_ETH |
Andy Fleming | 75b9d4a | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 23 | /* Default initializations for TSEC controllers. */ |
| 24 | |
| 25 | static struct tsec_info_struct tsec_info[] = { |
| 26 | #ifdef CONFIG_TSEC1 |
| 27 | STD_TSEC_INFO(1), /* TSEC1 */ |
| 28 | #endif |
| 29 | #ifdef CONFIG_TSEC2 |
| 30 | STD_TSEC_INFO(2), /* TSEC2 */ |
| 31 | #endif |
| 32 | #ifdef CONFIG_MPC85XX_FEC |
| 33 | { |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 34 | .regs = TSEC_GET_REGS(2, 0x2000), |
Andy Fleming | 75b9d4a | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 35 | .devname = CONFIG_MPC85XX_FEC_NAME, |
| 36 | .phyaddr = FEC_PHY_ADDR, |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 37 | .flags = FEC_FLAGS, |
| 38 | .mii_devname = DEFAULT_MII_NAME |
Andy Fleming | 75b9d4a | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 39 | }, /* FEC */ |
| 40 | #endif |
| 41 | #ifdef CONFIG_TSEC3 |
| 42 | STD_TSEC_INFO(3), /* TSEC3 */ |
| 43 | #endif |
| 44 | #ifdef CONFIG_TSEC4 |
| 45 | STD_TSEC_INFO(4), /* TSEC4 */ |
| 46 | #endif |
| 47 | }; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 48 | #endif /* CONFIG_DM_ETH */ |
Andy Fleming | 75b9d4a | 2008-08-31 16:33:26 -0500 | [diff] [blame] | 49 | |
Andy Fleming | 2abe361 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 50 | #define TBIANA_SETTINGS ( \ |
| 51 | TBIANA_ASYMMETRIC_PAUSE \ |
| 52 | | TBIANA_SYMMETRIC_PAUSE \ |
| 53 | | TBIANA_FULL_DUPLEX \ |
| 54 | ) |
| 55 | |
Felix Radensky | 90b5bf2 | 2010-06-28 01:57:39 +0300 | [diff] [blame] | 56 | /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */ |
| 57 | #ifndef CONFIG_TSEC_TBICR_SETTINGS |
Kumar Gala | 72c96a6 | 2010-12-01 22:55:54 -0600 | [diff] [blame] | 58 | #define CONFIG_TSEC_TBICR_SETTINGS ( \ |
Andy Fleming | 2abe361 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 59 | TBICR_PHY_RESET \ |
Kumar Gala | 72c96a6 | 2010-12-01 22:55:54 -0600 | [diff] [blame] | 60 | | TBICR_ANEG_ENABLE \ |
Andy Fleming | 2abe361 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 61 | | TBICR_FULL_DUPLEX \ |
| 62 | | TBICR_SPEED1_SET \ |
| 63 | ) |
Felix Radensky | 90b5bf2 | 2010-06-28 01:57:39 +0300 | [diff] [blame] | 64 | #endif /* CONFIG_TSEC_TBICR_SETTINGS */ |
Peter Tyser | 46e9167 | 2009-11-03 17:52:07 -0600 | [diff] [blame] | 65 | |
Andy Fleming | 2abe361 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 66 | /* Configure the TBI for SGMII operation */ |
| 67 | static void tsec_configure_serdes(struct tsec_private *priv) |
| 68 | { |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 69 | /* |
| 70 | * Access TBI PHY registers at given TSEC register offset as opposed |
| 71 | * to the register offset used for external PHY accesses |
| 72 | */ |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 73 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 74 | 0, TBI_ANA, TBIANA_SETTINGS); |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 75 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 76 | 0, TBI_TBICON, TBICON_CLK_SELECT); |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 77 | tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa), |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 78 | 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS); |
Andy Fleming | 2abe361 | 2008-08-31 16:33:27 -0500 | [diff] [blame] | 79 | } |
michael.firth@bt.com | 55fe7c5 | 2008-01-16 11:40:51 +0000 | [diff] [blame] | 80 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 81 | #ifdef CONFIG_MCAST_TFTP |
| 82 | |
| 83 | /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */ |
| 84 | |
| 85 | /* Set the appropriate hash bit for the given addr */ |
| 86 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 87 | /* |
| 88 | * The algorithm works like so: |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 89 | * 1) Take the Destination Address (ie the multicast address), and |
| 90 | * do a CRC on it (little endian), and reverse the bits of the |
| 91 | * result. |
| 92 | * 2) Use the 8 most significant bits as a hash into a 256-entry |
| 93 | * table. The table is controlled through 8 32-bit registers: |
Claudiu Manoil | 876d451 | 2013-09-30 12:44:40 +0300 | [diff] [blame] | 94 | * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry |
| 95 | * 255. This means that the 3 most significant bits in the |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 96 | * hash index which gaddr register to use, and the 5 other bits |
| 97 | * indicate which bit (assuming an IBM numbering scheme, which |
Claudiu Manoil | 876d451 | 2013-09-30 12:44:40 +0300 | [diff] [blame] | 98 | * for PowerPC (tm) is usually the case) in the register holds |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 99 | * the entry. |
| 100 | */ |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 101 | #ifndef CONFIG_DM_ETH |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 102 | static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac, u8 set) |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 103 | #else |
| 104 | static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int set) |
| 105 | #endif |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 106 | { |
Claudiu Manoil | b200204 | 2013-09-30 12:44:41 +0300 | [diff] [blame] | 107 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
Claudiu Manoil | 876d451 | 2013-09-30 12:44:40 +0300 | [diff] [blame] | 108 | struct tsec __iomem *regs = priv->regs; |
| 109 | u32 result, value; |
| 110 | u8 whichbit, whichreg; |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 111 | |
Claudiu Manoil | 876d451 | 2013-09-30 12:44:40 +0300 | [diff] [blame] | 112 | result = ether_crc(MAC_ADDR_LEN, mcast_mac); |
| 113 | whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */ |
| 114 | whichreg = result >> 29; /* the 3 MSB = which reg to set it in */ |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 115 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 116 | value = BIT(31 - whichbit); |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 117 | |
Claudiu Manoil | 876d451 | 2013-09-30 12:44:40 +0300 | [diff] [blame] | 118 | if (set) |
| 119 | setbits_be32(®s->hash.gaddr0 + whichreg, value); |
| 120 | else |
| 121 | clrbits_be32(®s->hash.gaddr0 + whichreg, value); |
| 122 | |
David Updegraff | 53a5c42 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | #endif /* Multicast TFTP ? */ |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 126 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 127 | /* |
| 128 | * Initialized required registers to appropriate values, zeroing |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 129 | * those we don't care about (unless zero is bad, in which case, |
| 130 | * choose a more appropriate value) |
| 131 | */ |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 132 | static void init_registers(struct tsec __iomem *regs) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 133 | { |
| 134 | /* Clear IEVENT */ |
| 135 | out_be32(®s->ievent, IEVENT_INIT_CLEAR); |
| 136 | |
| 137 | out_be32(®s->imask, IMASK_INIT_CLEAR); |
| 138 | |
| 139 | out_be32(®s->hash.iaddr0, 0); |
| 140 | out_be32(®s->hash.iaddr1, 0); |
| 141 | out_be32(®s->hash.iaddr2, 0); |
| 142 | out_be32(®s->hash.iaddr3, 0); |
| 143 | out_be32(®s->hash.iaddr4, 0); |
| 144 | out_be32(®s->hash.iaddr5, 0); |
| 145 | out_be32(®s->hash.iaddr6, 0); |
| 146 | out_be32(®s->hash.iaddr7, 0); |
| 147 | |
| 148 | out_be32(®s->hash.gaddr0, 0); |
| 149 | out_be32(®s->hash.gaddr1, 0); |
| 150 | out_be32(®s->hash.gaddr2, 0); |
| 151 | out_be32(®s->hash.gaddr3, 0); |
| 152 | out_be32(®s->hash.gaddr4, 0); |
| 153 | out_be32(®s->hash.gaddr5, 0); |
| 154 | out_be32(®s->hash.gaddr6, 0); |
| 155 | out_be32(®s->hash.gaddr7, 0); |
| 156 | |
| 157 | out_be32(®s->rctrl, 0x00000000); |
| 158 | |
| 159 | /* Init RMON mib registers */ |
Claudiu Manoil | 82ef75c | 2013-09-30 12:44:46 +0300 | [diff] [blame] | 160 | memset((void *)®s->rmon, 0, sizeof(regs->rmon)); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 161 | |
| 162 | out_be32(®s->rmon.cam1, 0xffffffff); |
| 163 | out_be32(®s->rmon.cam2, 0xffffffff); |
| 164 | |
| 165 | out_be32(®s->mrblr, MRBLR_INIT_SETTINGS); |
| 166 | |
| 167 | out_be32(®s->minflr, MINFLR_INIT_SETTINGS); |
| 168 | |
| 169 | out_be32(®s->attr, ATTR_INIT_SETTINGS); |
| 170 | out_be32(®s->attreli, ATTRELI_INIT_SETTINGS); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 173 | /* |
| 174 | * Configure maccfg2 based on negotiated speed and duplex |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 175 | * reported by PHY handling code |
| 176 | */ |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 177 | static void adjust_link(struct tsec_private *priv, struct phy_device *phydev) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 178 | { |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 179 | struct tsec __iomem *regs = priv->regs; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 180 | u32 ecntrl, maccfg2; |
| 181 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 182 | if (!phydev->link) { |
| 183 | printf("%s: No link.\n", phydev->dev->name); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | /* clear all bits relative with interface mode */ |
| 188 | ecntrl = in_be32(®s->ecntrl); |
| 189 | ecntrl &= ~ECNTRL_R100; |
| 190 | |
| 191 | maccfg2 = in_be32(®s->maccfg2); |
| 192 | maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX); |
| 193 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 194 | if (phydev->duplex) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 195 | maccfg2 |= MACCFG2_FULL_DUPLEX; |
| 196 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 197 | switch (phydev->speed) { |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 198 | case 1000: |
| 199 | maccfg2 |= MACCFG2_GMII; |
| 200 | break; |
| 201 | case 100: |
| 202 | case 10: |
| 203 | maccfg2 |= MACCFG2_MII; |
| 204 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 205 | /* |
| 206 | * Set R100 bit in all modes although |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 207 | * it is only used in RGMII mode |
| 208 | */ |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 209 | if (phydev->speed == 100) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 210 | ecntrl |= ECNTRL_R100; |
| 211 | break; |
| 212 | default: |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 213 | printf("%s: Speed was bad\n", phydev->dev->name); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | |
| 217 | out_be32(®s->ecntrl, ecntrl); |
| 218 | out_be32(®s->maccfg2, maccfg2); |
| 219 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 220 | printf("Speed: %d, %s duplex%s\n", phydev->speed, |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 221 | (phydev->duplex) ? "full" : "half", |
| 222 | (phydev->port == PORT_FIBRE) ? ", fiber mode" : ""); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 225 | /* |
| 226 | * This returns the status bits of the device. The return value |
| 227 | * is never checked, and this is what the 8260 driver did, so we |
| 228 | * do the same. Presumably, this would be zero if there were no |
| 229 | * errors |
| 230 | */ |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 231 | #ifndef CONFIG_DM_ETH |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 232 | static int tsec_send(struct eth_device *dev, void *packet, int length) |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 233 | #else |
| 234 | static int tsec_send(struct udevice *dev, void *packet, int length) |
| 235 | #endif |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 236 | { |
| 237 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 238 | struct tsec __iomem *regs = priv->regs; |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 239 | u16 status; |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 240 | int result = 0; |
| 241 | int i; |
| 242 | |
| 243 | /* Find an empty buffer descriptor */ |
| 244 | for (i = 0; |
| 245 | in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY; |
| 246 | i++) { |
| 247 | if (i >= TOUT_LOOP) { |
| 248 | debug("%s: tsec: tx buffers full\n", dev->name); |
| 249 | return result; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet); |
| 254 | out_be16(&priv->txbd[priv->tx_idx].length, length); |
| 255 | status = in_be16(&priv->txbd[priv->tx_idx].status); |
| 256 | out_be16(&priv->txbd[priv->tx_idx].status, status | |
| 257 | (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT)); |
| 258 | |
| 259 | /* Tell the DMA to go */ |
| 260 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); |
| 261 | |
| 262 | /* Wait for buffer to be transmitted */ |
| 263 | for (i = 0; |
| 264 | in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY; |
| 265 | i++) { |
| 266 | if (i >= TOUT_LOOP) { |
| 267 | debug("%s: tsec: tx error\n", dev->name); |
| 268 | return result; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT; |
| 273 | result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS; |
| 274 | |
| 275 | return result; |
| 276 | } |
| 277 | |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 278 | #ifndef CONFIG_DM_ETH |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 279 | static int tsec_recv(struct eth_device *dev) |
| 280 | { |
| 281 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 282 | struct tsec __iomem *regs = priv->regs; |
| 283 | |
| 284 | while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) { |
| 285 | int length = in_be16(&priv->rxbd[priv->rx_idx].length); |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 286 | u16 status = in_be16(&priv->rxbd[priv->rx_idx].status); |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 287 | uchar *packet = net_rx_packets[priv->rx_idx]; |
| 288 | |
| 289 | /* Send the packet up if there were no errors */ |
| 290 | if (!(status & RXBD_STATS)) |
| 291 | net_process_received_packet(packet, length - 4); |
| 292 | else |
| 293 | printf("Got error %x\n", (status & RXBD_STATS)); |
| 294 | |
| 295 | out_be16(&priv->rxbd[priv->rx_idx].length, 0); |
| 296 | |
| 297 | status = RXBD_EMPTY; |
| 298 | /* Set the wrap bit if this is the last element in the list */ |
| 299 | if ((priv->rx_idx + 1) == PKTBUFSRX) |
| 300 | status |= RXBD_WRAP; |
| 301 | out_be16(&priv->rxbd[priv->rx_idx].status, status); |
| 302 | |
| 303 | priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX; |
| 304 | } |
| 305 | |
| 306 | if (in_be32(®s->ievent) & IEVENT_BSY) { |
| 307 | out_be32(®s->ievent, IEVENT_BSY); |
| 308 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 309 | } |
| 310 | |
| 311 | return -1; |
| 312 | } |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 313 | #else |
| 314 | static int tsec_recv(struct udevice *dev, int flags, uchar **packetp) |
| 315 | { |
| 316 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 317 | struct tsec __iomem *regs = priv->regs; |
| 318 | int ret = -1; |
| 319 | |
| 320 | if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) { |
| 321 | int length = in_be16(&priv->rxbd[priv->rx_idx].length); |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 322 | u16 status = in_be16(&priv->rxbd[priv->rx_idx].status); |
| 323 | u32 buf; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 324 | |
| 325 | /* Send the packet up if there were no errors */ |
| 326 | if (!(status & RXBD_STATS)) { |
| 327 | buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr); |
| 328 | *packetp = (uchar *)buf; |
| 329 | ret = length - 4; |
| 330 | } else { |
| 331 | printf("Got error %x\n", (status & RXBD_STATS)); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if (in_be32(®s->ievent) & IEVENT_BSY) { |
| 336 | out_be32(®s->ievent, IEVENT_BSY); |
| 337 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 338 | } |
| 339 | |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 344 | { |
| 345 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 346 | u16 status; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 347 | |
| 348 | out_be16(&priv->rxbd[priv->rx_idx].length, 0); |
| 349 | |
| 350 | status = RXBD_EMPTY; |
| 351 | /* Set the wrap bit if this is the last element in the list */ |
| 352 | if ((priv->rx_idx + 1) == PKTBUFSRX) |
| 353 | status |= RXBD_WRAP; |
| 354 | out_be16(&priv->rxbd[priv->rx_idx].status, status); |
| 355 | |
| 356 | priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX; |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | #endif |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 361 | |
| 362 | /* Stop the interface */ |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 363 | #ifndef CONFIG_DM_ETH |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 364 | static void tsec_halt(struct eth_device *dev) |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 365 | #else |
| 366 | static void tsec_halt(struct udevice *dev) |
| 367 | #endif |
Bin Meng | 8ba5017 | 2016-01-11 22:41:21 -0800 | [diff] [blame] | 368 | { |
| 369 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
| 370 | struct tsec __iomem *regs = priv->regs; |
| 371 | |
| 372 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 373 | setbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 374 | |
| 375 | while ((in_be32(®s->ievent) & (IEVENT_GRSC | IEVENT_GTSC)) |
| 376 | != (IEVENT_GRSC | IEVENT_GTSC)) |
| 377 | ; |
| 378 | |
| 379 | clrbits_be32(®s->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN); |
| 380 | |
| 381 | /* Shut down the PHY, as needed */ |
| 382 | phy_shutdown(priv->phydev); |
| 383 | } |
| 384 | |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 385 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
| 386 | /* |
| 387 | * When MACCFG1[Rx_EN] is enabled during system boot as part |
| 388 | * of the eTSEC port initialization sequence, |
| 389 | * the eTSEC Rx logic may not be properly initialized. |
| 390 | */ |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 391 | void redundant_init(struct tsec_private *priv) |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 392 | { |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 393 | struct tsec __iomem *regs = priv->regs; |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 394 | uint t, count = 0; |
| 395 | int fail = 1; |
| 396 | static const u8 pkt[] = { |
| 397 | 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25, |
| 398 | 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00, |
| 399 | 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01, |
| 400 | 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1, |
| 401 | 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00, |
| 402 | 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 403 | 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, |
| 404 | 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, |
| 405 | 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, |
| 406 | 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, |
| 407 | 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, |
| 408 | 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, |
| 409 | 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, |
| 410 | 0x71, 0x72}; |
| 411 | |
| 412 | /* Enable promiscuous mode */ |
| 413 | setbits_be32(®s->rctrl, 0x8); |
| 414 | /* Enable loopback mode */ |
| 415 | setbits_be32(®s->maccfg1, MACCFG1_LOOPBACK); |
| 416 | /* Enable transmit and receive */ |
| 417 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN); |
| 418 | |
| 419 | /* Tell the DMA it is clear to go */ |
| 420 | setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS); |
| 421 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); |
| 422 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 423 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 424 | |
| 425 | do { |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 426 | u16 status; |
| 427 | |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 428 | tsec_send(priv->dev, (void *)pkt, sizeof(pkt)); |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 429 | |
| 430 | /* Wait for buffer to be received */ |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 431 | for (t = 0; |
| 432 | in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY; |
Bin Meng | 362b123 | 2016-01-11 22:41:19 -0800 | [diff] [blame] | 433 | t++) { |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 434 | if (t >= 10 * TOUT_LOOP) { |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 435 | printf("%s: tsec: rx error\n", priv->dev->name); |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
Bin Meng | 362b123 | 2016-01-11 22:41:19 -0800 | [diff] [blame] | 440 | if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt))) |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 441 | fail = 0; |
| 442 | |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 443 | out_be16(&priv->rxbd[priv->rx_idx].length, 0); |
Claudiu Manoil | 9c9141f | 2013-10-04 19:13:53 +0300 | [diff] [blame] | 444 | status = RXBD_EMPTY; |
Bin Meng | 362b123 | 2016-01-11 22:41:19 -0800 | [diff] [blame] | 445 | if ((priv->rx_idx + 1) == PKTBUFSRX) |
Claudiu Manoil | 9c9141f | 2013-10-04 19:13:53 +0300 | [diff] [blame] | 446 | status |= RXBD_WRAP; |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 447 | out_be16(&priv->rxbd[priv->rx_idx].status, status); |
Bin Meng | 362b123 | 2016-01-11 22:41:19 -0800 | [diff] [blame] | 448 | priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX; |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 449 | |
| 450 | if (in_be32(®s->ievent) & IEVENT_BSY) { |
| 451 | out_be32(®s->ievent, IEVENT_BSY); |
| 452 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 453 | } |
| 454 | if (fail) { |
| 455 | printf("loopback recv packet error!\n"); |
| 456 | clrbits_be32(®s->maccfg1, MACCFG1_RX_EN); |
| 457 | udelay(1000); |
| 458 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN); |
| 459 | } |
| 460 | } while ((count++ < 4) && (fail == 1)); |
| 461 | |
| 462 | if (fail) |
| 463 | panic("eTSEC init fail!\n"); |
| 464 | /* Disable promiscuous mode */ |
| 465 | clrbits_be32(®s->rctrl, 0x8); |
| 466 | /* Disable loopback mode */ |
| 467 | clrbits_be32(®s->maccfg1, MACCFG1_LOOPBACK); |
| 468 | } |
| 469 | #endif |
| 470 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 471 | /* |
| 472 | * Set up the buffers and their descriptors, and bring up the |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 473 | * interface |
| 474 | */ |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 475 | static void startup_tsec(struct tsec_private *priv) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 476 | { |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 477 | struct tsec __iomem *regs = priv->regs; |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 478 | u16 status; |
Claudiu Manoil | 9c9141f | 2013-10-04 19:13:53 +0300 | [diff] [blame] | 479 | int i; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 480 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 481 | /* reset the indices to zero */ |
Bin Meng | 362b123 | 2016-01-11 22:41:19 -0800 | [diff] [blame] | 482 | priv->rx_idx = 0; |
| 483 | priv->tx_idx = 0; |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 484 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
| 485 | uint svr; |
| 486 | #endif |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 487 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 488 | /* Point to the buffer descriptors */ |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 489 | out_be32(®s->tbase, (u32)&priv->txbd[0]); |
| 490 | out_be32(®s->rbase, (u32)&priv->rxbd[0]); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 491 | |
| 492 | /* Initialize the Rx Buffer descriptors */ |
| 493 | for (i = 0; i < PKTBUFSRX; i++) { |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 494 | out_be16(&priv->rxbd[i].status, RXBD_EMPTY); |
| 495 | out_be16(&priv->rxbd[i].length, 0); |
| 496 | out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 497 | } |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 498 | status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status); |
| 499 | out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 500 | |
| 501 | /* Initialize the TX Buffer Descriptors */ |
| 502 | for (i = 0; i < TX_BUF_CNT; i++) { |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 503 | out_be16(&priv->txbd[i].status, 0); |
| 504 | out_be16(&priv->txbd[i].length, 0); |
| 505 | out_be32(&priv->txbd[i].bufptr, 0); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 506 | } |
Bin Meng | e677da9 | 2016-01-11 22:41:20 -0800 | [diff] [blame] | 507 | status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status); |
| 508 | out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 509 | |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 510 | #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129 |
| 511 | svr = get_svr(); |
| 512 | if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0)) |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 513 | redundant_init(priv); |
chenhui zhao | aada81d | 2011-10-03 08:38:50 -0500 | [diff] [blame] | 514 | #endif |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 515 | /* Enable Transmit and Receive */ |
| 516 | setbits_be32(®s->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN); |
| 517 | |
| 518 | /* Tell the DMA it is clear to go */ |
| 519 | setbits_be32(®s->dmactrl, DMACTRL_INIT_SETTINGS); |
| 520 | out_be32(®s->tstat, TSTAT_CLEAR_THALT); |
| 521 | out_be32(®s->rstat, RSTAT_CLEAR_RHALT); |
| 522 | clrbits_be32(®s->dmactrl, DMACTRL_GRS | DMACTRL_GTS); |
| 523 | } |
| 524 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 525 | /* |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 526 | * Initializes data structures and registers for the controller, |
| 527 | * and brings the interface up. Returns the link status, meaning |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 528 | * that it returns success if the link is up, failure otherwise. |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 529 | * This allows U-Boot to find the first active controller. |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 530 | */ |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 531 | #ifndef CONFIG_DM_ETH |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 532 | static int tsec_init(struct eth_device *dev, bd_t *bd) |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 533 | #else |
| 534 | static int tsec_init(struct udevice *dev) |
| 535 | #endif |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 536 | { |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 537 | struct tsec_private *priv = (struct tsec_private *)dev->priv; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 538 | #ifdef CONFIG_DM_ETH |
| 539 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 540 | #endif |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 541 | struct tsec __iomem *regs = priv->regs; |
Claudiu Manoil | b1690bc | 2013-09-30 12:44:47 +0300 | [diff] [blame] | 542 | u32 tempval; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 543 | int ret; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 544 | |
| 545 | /* Make sure the controller is stopped */ |
| 546 | tsec_halt(dev); |
| 547 | |
| 548 | /* Init MACCFG2. Defaults to GMII */ |
| 549 | out_be32(®s->maccfg2, MACCFG2_INIT_SETTINGS); |
| 550 | |
| 551 | /* Init ECNTRL */ |
| 552 | out_be32(®s->ecntrl, ECNTRL_INIT_SETTINGS); |
| 553 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 554 | /* |
| 555 | * Copy the station address into the address registers. |
Claudiu Manoil | b1690bc | 2013-09-30 12:44:47 +0300 | [diff] [blame] | 556 | * For a station address of 0x12345678ABCD in transmission |
| 557 | * order (BE), MACnADDR1 is set to 0xCDAB7856 and |
| 558 | * MACnADDR2 is set to 0x34120000. |
| 559 | */ |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 560 | #ifndef CONFIG_DM_ETH |
Claudiu Manoil | b1690bc | 2013-09-30 12:44:47 +0300 | [diff] [blame] | 561 | tempval = (dev->enetaddr[5] << 24) | (dev->enetaddr[4] << 16) | |
| 562 | (dev->enetaddr[3] << 8) | dev->enetaddr[2]; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 563 | #else |
| 564 | tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) | |
| 565 | (pdata->enetaddr[3] << 8) | pdata->enetaddr[2]; |
| 566 | #endif |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 567 | |
| 568 | out_be32(®s->macstnaddr1, tempval); |
| 569 | |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 570 | #ifndef CONFIG_DM_ETH |
Claudiu Manoil | b1690bc | 2013-09-30 12:44:47 +0300 | [diff] [blame] | 571 | tempval = (dev->enetaddr[1] << 24) | (dev->enetaddr[0] << 16); |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 572 | #else |
| 573 | tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16); |
| 574 | #endif |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 575 | |
| 576 | out_be32(®s->macstnaddr2, tempval); |
| 577 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 578 | /* Clear out (for the most part) the other registers */ |
| 579 | init_registers(regs); |
| 580 | |
| 581 | /* Ready the device for tx/rx */ |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 582 | startup_tsec(priv); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 583 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 584 | /* Start up the PHY */ |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 585 | ret = phy_startup(priv->phydev); |
| 586 | if (ret) { |
| 587 | printf("Could not initialize PHY %s\n", |
| 588 | priv->phydev->dev->name); |
| 589 | return ret; |
| 590 | } |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 591 | |
| 592 | adjust_link(priv, priv->phydev); |
| 593 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 594 | /* If there's no link, fail */ |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 595 | return priv->phydev->link ? 0 : -1; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 596 | } |
| 597 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 598 | static phy_interface_t tsec_get_interface(struct tsec_private *priv) |
| 599 | { |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 600 | struct tsec __iomem *regs = priv->regs; |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 601 | u32 ecntrl; |
| 602 | |
| 603 | ecntrl = in_be32(®s->ecntrl); |
| 604 | |
| 605 | if (ecntrl & ECNTRL_SGMII_MODE) |
| 606 | return PHY_INTERFACE_MODE_SGMII; |
| 607 | |
| 608 | if (ecntrl & ECNTRL_TBI_MODE) { |
| 609 | if (ecntrl & ECNTRL_REDUCED_MODE) |
| 610 | return PHY_INTERFACE_MODE_RTBI; |
| 611 | else |
| 612 | return PHY_INTERFACE_MODE_TBI; |
| 613 | } |
| 614 | |
| 615 | if (ecntrl & ECNTRL_REDUCED_MODE) { |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 616 | phy_interface_t interface; |
| 617 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 618 | if (ecntrl & ECNTRL_REDUCED_MII_MODE) |
| 619 | return PHY_INTERFACE_MODE_RMII; |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 620 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 621 | interface = priv->interface; |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 622 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 623 | /* |
| 624 | * This isn't autodetected, so it must |
| 625 | * be set by the platform code. |
| 626 | */ |
| 627 | if (interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 628 | interface == PHY_INTERFACE_MODE_RGMII_TXID || |
| 629 | interface == PHY_INTERFACE_MODE_RGMII_RXID) |
| 630 | return interface; |
| 631 | |
| 632 | return PHY_INTERFACE_MODE_RGMII; |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | if (priv->flags & TSEC_GIGABIT) |
| 636 | return PHY_INTERFACE_MODE_GMII; |
| 637 | |
| 638 | return PHY_INTERFACE_MODE_MII; |
| 639 | } |
| 640 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 641 | /* |
| 642 | * Discover which PHY is attached to the device, and configure it |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 643 | * properly. If the PHY is not recognized, then return 0 |
| 644 | * (failure). Otherwise, return 1 |
| 645 | */ |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 646 | static int init_phy(struct tsec_private *priv) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 647 | { |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 648 | struct phy_device *phydev; |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 649 | struct tsec __iomem *regs = priv->regs; |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 650 | u32 supported = (SUPPORTED_10baseT_Half | |
| 651 | SUPPORTED_10baseT_Full | |
| 652 | SUPPORTED_100baseT_Half | |
| 653 | SUPPORTED_100baseT_Full); |
| 654 | |
| 655 | if (priv->flags & TSEC_GIGABIT) |
| 656 | supported |= SUPPORTED_1000baseT_Full; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 657 | |
| 658 | /* Assign a Physical address to the TBI */ |
Bin Meng | a1c76c1 | 2016-01-11 22:41:25 -0800 | [diff] [blame] | 659 | out_be32(®s->tbipa, priv->tbiaddr); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 660 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 661 | priv->interface = tsec_get_interface(priv); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 662 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 663 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 664 | tsec_configure_serdes(priv); |
| 665 | |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 666 | phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev, |
| 667 | priv->interface); |
Claudiu Manoil | 7f233c0 | 2013-12-10 15:21:04 +0200 | [diff] [blame] | 668 | if (!phydev) |
| 669 | return 0; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 670 | |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 671 | phydev->supported &= supported; |
| 672 | phydev->advertising = phydev->supported; |
| 673 | |
| 674 | priv->phydev = phydev; |
| 675 | |
| 676 | phy_config(phydev); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 677 | |
| 678 | return 1; |
| 679 | } |
| 680 | |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 681 | #ifndef CONFIG_DM_ETH |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 682 | /* |
| 683 | * Initialize device structure. Returns success if PHY |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 684 | * initialization succeeded (i.e. if it recognizes the PHY) |
| 685 | */ |
| 686 | static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info) |
| 687 | { |
| 688 | struct eth_device *dev; |
| 689 | int i; |
| 690 | struct tsec_private *priv; |
| 691 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 692 | dev = (struct eth_device *)malloc(sizeof(*dev)); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 693 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 694 | if (!dev) |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 695 | return 0; |
| 696 | |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 697 | memset(dev, 0, sizeof(*dev)); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 698 | |
| 699 | priv = (struct tsec_private *)malloc(sizeof(*priv)); |
| 700 | |
Mario Six | 5775f00 | 2018-01-15 11:08:22 +0100 | [diff] [blame] | 701 | if (!priv) { |
| 702 | free(dev); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 703 | return 0; |
Mario Six | 5775f00 | 2018-01-15 11:08:22 +0100 | [diff] [blame] | 704 | } |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 705 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 706 | priv->regs = tsec_info->regs; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 707 | priv->phyregs_sgmii = tsec_info->miiregs_sgmii; |
| 708 | |
| 709 | priv->phyaddr = tsec_info->phyaddr; |
Bin Meng | a1c76c1 | 2016-01-11 22:41:25 -0800 | [diff] [blame] | 710 | priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 711 | priv->flags = tsec_info->flags; |
| 712 | |
Ben Whitten | 192bc69 | 2015-12-30 13:05:58 +0000 | [diff] [blame] | 713 | strcpy(dev->name, tsec_info->devname); |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 714 | priv->interface = tsec_info->interface; |
| 715 | priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname); |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 716 | priv->dev = dev; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 717 | dev->iobase = 0; |
| 718 | dev->priv = priv; |
| 719 | dev->init = tsec_init; |
| 720 | dev->halt = tsec_halt; |
| 721 | dev->send = tsec_send; |
| 722 | dev->recv = tsec_recv; |
| 723 | #ifdef CONFIG_MCAST_TFTP |
| 724 | dev->mcast = tsec_mcast_addr; |
| 725 | #endif |
| 726 | |
Bin Meng | 9872b73 | 2016-01-11 22:41:18 -0800 | [diff] [blame] | 727 | /* Tell U-Boot to get the addr from the env */ |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 728 | for (i = 0; i < 6; i++) |
| 729 | dev->enetaddr[i] = 0; |
| 730 | |
| 731 | eth_register(dev); |
| 732 | |
| 733 | /* Reset the MAC */ |
| 734 | setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
| 735 | udelay(2); /* Soft Reset must be asserted for 3 TX clocks */ |
| 736 | clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
| 737 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 738 | /* Try to initialize PHY here, and return */ |
Bin Meng | 56a27a1 | 2016-01-11 22:41:22 -0800 | [diff] [blame] | 739 | return init_phy(priv); |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* |
| 743 | * Initialize all the TSEC devices |
| 744 | * |
| 745 | * Returns the number of TSEC devices that were initialized |
| 746 | */ |
| 747 | int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num) |
| 748 | { |
| 749 | int i; |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 750 | int count = 0; |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 751 | |
| 752 | for (i = 0; i < num; i++) { |
Mario Six | d38de33 | 2018-01-15 11:08:21 +0100 | [diff] [blame] | 753 | int ret = tsec_initialize(bis, &tsecs[i]); |
| 754 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 755 | if (ret > 0) |
| 756 | count += ret; |
| 757 | } |
| 758 | |
| 759 | return count; |
| 760 | } |
| 761 | |
| 762 | int tsec_standard_init(bd_t *bis) |
| 763 | { |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 764 | struct fsl_pq_mdio_info info; |
| 765 | |
Claudiu Manoil | aec84bf | 2013-09-30 12:44:42 +0300 | [diff] [blame] | 766 | info.regs = TSEC_GET_MDIO_REGS_BASE(1); |
Andy Fleming | 063c126 | 2011-04-08 02:10:54 -0500 | [diff] [blame] | 767 | info.name = DEFAULT_MII_NAME; |
| 768 | |
| 769 | fsl_pq_mdio_init(bis, &info); |
| 770 | |
Mingkai Hu | 9075191 | 2011-01-27 12:52:46 +0800 | [diff] [blame] | 771 | return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info)); |
| 772 | } |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 773 | #else /* CONFIG_DM_ETH */ |
| 774 | int tsec_probe(struct udevice *dev) |
| 775 | { |
| 776 | struct tsec_private *priv = dev_get_priv(dev); |
| 777 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 778 | struct fsl_pq_mdio_info mdio_info; |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 779 | struct ofnode_phandle_args phandle_args; |
| 780 | ofnode parent; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 781 | const char *phy_mode; |
| 782 | int ret; |
| 783 | |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 784 | pdata->iobase = (phys_addr_t)dev_read_addr(dev); |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 785 | priv->regs = (struct tsec *)pdata->iobase; |
| 786 | |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 787 | if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, |
| 788 | &phandle_args)) { |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 789 | debug("phy-handle does not exist under tsec %s\n", dev->name); |
| 790 | return -ENOENT; |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 791 | } else { |
| 792 | int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0); |
| 793 | |
| 794 | priv->phyaddr = reg; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 797 | parent = ofnode_get_parent(phandle_args.node); |
| 798 | if (ofnode_valid(parent)) { |
Mario Six | 5e9d9ab | 2018-04-27 14:52:57 +0200 | [diff] [blame] | 799 | int reg = ofnode_get_addr_index(parent, 0); |
| 800 | |
| 801 | priv->phyregs_sgmii = (struct tsec_mii_mng *)reg; |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 802 | } else { |
| 803 | debug("No parent node for PHY?\n"); |
| 804 | return -ENOENT; |
| 805 | } |
| 806 | |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 807 | if (dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0, |
| 808 | &phandle_args)) { |
Bin Meng | a1c76c1 | 2016-01-11 22:41:25 -0800 | [diff] [blame] | 809 | priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE; |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 810 | } else { |
| 811 | int reg = ofnode_read_u32_default(phandle_args.node, "reg", |
| 812 | CONFIG_SYS_TBIPA_VALUE); |
| 813 | priv->tbiaddr = reg; |
Bin Meng | a1c76c1 | 2016-01-11 22:41:25 -0800 | [diff] [blame] | 814 | } |
| 815 | |
Mario Six | 1313aaf | 2018-01-15 11:08:23 +0100 | [diff] [blame] | 816 | phy_mode = dev_read_prop(dev, "phy-connection-type", NULL); |
Bin Meng | 9a1d6af | 2016-01-11 22:41:24 -0800 | [diff] [blame] | 817 | if (phy_mode) |
| 818 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 819 | if (pdata->phy_interface == -1) { |
| 820 | debug("Invalid PHY interface '%s'\n", phy_mode); |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | priv->interface = pdata->phy_interface; |
| 824 | |
| 825 | /* Initialize flags */ |
| 826 | priv->flags = TSEC_GIGABIT; |
| 827 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) |
| 828 | priv->flags |= TSEC_SGMII; |
| 829 | |
| 830 | mdio_info.regs = priv->phyregs_sgmii; |
| 831 | mdio_info.name = (char *)dev->name; |
| 832 | ret = fsl_pq_mdio_init(NULL, &mdio_info); |
| 833 | if (ret) |
| 834 | return ret; |
| 835 | |
| 836 | /* Reset the MAC */ |
| 837 | setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
| 838 | udelay(2); /* Soft Reset must be asserted for 3 TX clocks */ |
| 839 | clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET); |
| 840 | |
| 841 | priv->dev = dev; |
| 842 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 843 | |
| 844 | /* Try to initialize PHY here, and return */ |
| 845 | return !init_phy(priv); |
| 846 | } |
| 847 | |
| 848 | int tsec_remove(struct udevice *dev) |
| 849 | { |
| 850 | struct tsec_private *priv = dev->priv; |
| 851 | |
| 852 | free(priv->phydev); |
| 853 | mdio_unregister(priv->bus); |
| 854 | mdio_free(priv->bus); |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | static const struct eth_ops tsec_ops = { |
| 860 | .start = tsec_init, |
| 861 | .send = tsec_send, |
| 862 | .recv = tsec_recv, |
| 863 | .free_pkt = tsec_free_pkt, |
| 864 | .stop = tsec_halt, |
| 865 | #ifdef CONFIG_MCAST_TFTP |
| 866 | .mcast = tsec_mcast_addr, |
| 867 | #endif |
| 868 | }; |
| 869 | |
| 870 | static const struct udevice_id tsec_ids[] = { |
| 871 | { .compatible = "fsl,tsec" }, |
| 872 | { } |
| 873 | }; |
| 874 | |
| 875 | U_BOOT_DRIVER(eth_tsec) = { |
| 876 | .name = "tsec", |
| 877 | .id = UCLASS_ETH, |
| 878 | .of_match = tsec_ids, |
| 879 | .probe = tsec_probe, |
| 880 | .remove = tsec_remove, |
| 881 | .ops = &tsec_ops, |
| 882 | .priv_auto_alloc_size = sizeof(struct tsec_private), |
| 883 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 884 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 885 | }; |
| 886 | #endif /* CONFIG_DM_ETH */ |