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