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