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