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