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