blob: 6481ee24a6010099cbabb5a213641c63706176a2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk42d1f032003-10-15 23:53:47 +00002/*
wdenk97d80fc2004-06-09 00:34:46 +00003 * Freescale Three Speed Ethernet Controller driver
wdenk42d1f032003-10-15 23:53:47 +00004 *
Claudiu Manoilaec84bf2013-09-30 12:44:42 +03005 * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
wdenk42d1f032003-10-15 23:53:47 +00006 * (C) Copyright 2003, Motorola, Inc.
wdenk42d1f032003-10-15 23:53:47 +00007 * author Andy Fleming
wdenk42d1f032003-10-15 23:53:47 +00008 */
9
10#include <config.h>
Bin Meng9a1d6af2016-01-11 22:41:24 -080011#include <dm.h>
wdenk42d1f032003-10-15 23:53:47 +000012#include <malloc.h>
13#include <net.h>
14#include <command.h>
Andy Flemingdd3d1f52008-08-31 16:33:25 -050015#include <tsec.h>
Andy Fleming063c1262011-04-08 02:10:54 -050016#include <fsl_mdio.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090019#include <linux/errno.h>
Hou Zhiqiangb4eb9cf2020-07-16 18:09:12 +080020#include <miiphy.h>
chenhui zhaoaada81d2011-10-03 08:38:50 -050021#include <asm/processor.h>
Alison Wang52d00a82014-09-05 13:52:38 +080022#include <asm/io.h>
wdenk42d1f032003-10-15 23:53:47 +000023
Andy Fleming2abe3612008-08-31 16:33:27 -050024#define TBIANA_SETTINGS ( \
25 TBIANA_ASYMMETRIC_PAUSE \
26 | TBIANA_SYMMETRIC_PAUSE \
27 | TBIANA_FULL_DUPLEX \
28 )
29
Felix Radensky90b5bf22010-06-28 01:57:39 +030030/* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
Tom Rinie7cebff2022-12-04 10:14:01 -050031#ifndef CFG_TSEC_TBICR_SETTINGS
32#define CFG_TSEC_TBICR_SETTINGS ( \
Andy Fleming2abe3612008-08-31 16:33:27 -050033 TBICR_PHY_RESET \
Kumar Gala72c96a62010-12-01 22:55:54 -060034 | TBICR_ANEG_ENABLE \
Andy Fleming2abe3612008-08-31 16:33:27 -050035 | TBICR_FULL_DUPLEX \
36 | TBICR_SPEED1_SET \
37 )
Tom Rinie7cebff2022-12-04 10:14:01 -050038#endif /* CFG_TSEC_TBICR_SETTINGS */
Peter Tyser46e91672009-11-03 17:52:07 -060039
Andy Fleming2abe3612008-08-31 16:33:27 -050040/* Configure the TBI for SGMII operation */
41static void tsec_configure_serdes(struct tsec_private *priv)
42{
Bin Meng9872b732016-01-11 22:41:18 -080043 /*
44 * Access TBI PHY registers at given TSEC register offset as opposed
45 * to the register offset used for external PHY accesses
46 */
Andy Fleming063c1262011-04-08 02:10:54 -050047 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Mario Sixd38de332018-01-15 11:08:21 +010048 0, TBI_ANA, TBIANA_SETTINGS);
Andy Fleming063c1262011-04-08 02:10:54 -050049 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Mario Sixd38de332018-01-15 11:08:21 +010050 0, TBI_TBICON, TBICON_CLK_SELECT);
Andy Fleming063c1262011-04-08 02:10:54 -050051 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Tom Rinie7cebff2022-12-04 10:14:01 -050052 0, TBI_CR, CFG_TSEC_TBICR_SETTINGS);
Andy Fleming2abe3612008-08-31 16:33:27 -050053}
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +000054
Chris Packham1a4af5c2018-11-26 21:00:28 +130055/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
56 * and this is the ethernet-crc method needed for TSEC -- and perhaps
57 * some other adapter -- hash tables
58 */
59#define CRCPOLY_LE 0xedb88320
60static u32 ether_crc(size_t len, unsigned char const *p)
61{
62 int i;
63 u32 crc;
64
65 crc = ~0;
66 while (len--) {
67 crc ^= *p++;
68 for (i = 0; i < 8; i++)
69 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
70 }
71 /* an reverse the bits, cuz of way they arrive -- last-first */
72 crc = (crc >> 16) | (crc << 16);
73 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
74 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
75 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
76 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
77 return crc;
78}
79
David Updegraff53a5c422007-06-11 10:41:07 -050080/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
81
82/* Set the appropriate hash bit for the given addr */
83
Bin Meng9872b732016-01-11 22:41:18 -080084/*
85 * The algorithm works like so:
David Updegraff53a5c422007-06-11 10:41:07 -050086 * 1) Take the Destination Address (ie the multicast address), and
87 * do a CRC on it (little endian), and reverse the bits of the
88 * result.
89 * 2) Use the 8 most significant bits as a hash into a 256-entry
90 * table. The table is controlled through 8 32-bit registers:
Claudiu Manoil876d4512013-09-30 12:44:40 +030091 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
92 * 255. This means that the 3 most significant bits in the
David Updegraff53a5c422007-06-11 10:41:07 -050093 * hash index which gaddr register to use, and the 5 other bits
94 * indicate which bit (assuming an IBM numbering scheme, which
Claudiu Manoil876d4512013-09-30 12:44:40 +030095 * for PowerPC (tm) is usually the case) in the register holds
Bin Meng9872b732016-01-11 22:41:18 -080096 * the entry.
97 */
Chris Packham67bb9842018-11-26 21:00:29 +130098static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
David Updegraff53a5c422007-06-11 10:41:07 -050099{
Simon Glass0fd3d912020-12-22 19:30:28 -0700100 struct tsec_private *priv;
101 struct tsec __iomem *regs;
Claudiu Manoil876d4512013-09-30 12:44:40 +0300102 u32 result, value;
103 u8 whichbit, whichreg;
David Updegraff53a5c422007-06-11 10:41:07 -0500104
Simon Glass0fd3d912020-12-22 19:30:28 -0700105 priv = dev_get_priv(dev);
Simon Glass0fd3d912020-12-22 19:30:28 -0700106 regs = priv->regs;
Claudiu Manoil876d4512013-09-30 12:44:40 +0300107 result = ether_crc(MAC_ADDR_LEN, mcast_mac);
108 whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
109 whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
David Updegraff53a5c422007-06-11 10:41:07 -0500110
Mario Sixd38de332018-01-15 11:08:21 +0100111 value = BIT(31 - whichbit);
David Updegraff53a5c422007-06-11 10:41:07 -0500112
Chris Packham67bb9842018-11-26 21:00:29 +1300113 if (join)
Claudiu Manoil876d4512013-09-30 12:44:40 +0300114 setbits_be32(&regs->hash.gaddr0 + whichreg, value);
115 else
116 clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
117
David Updegraff53a5c422007-06-11 10:41:07 -0500118 return 0;
119}
Mingkai Hu90751912011-01-27 12:52:46 +0800120
Marek Vasutbee35512022-12-17 18:41:13 +0100121static int __maybe_unused tsec_set_promisc(struct udevice *dev, bool enable)
Vladimir Oltean9dcb8102021-09-29 18:04:36 +0300122{
123 struct tsec_private *priv = dev_get_priv(dev);
124 struct tsec __iomem *regs = priv->regs;
125
126 if (enable)
127 setbits_be32(&regs->rctrl, RCTRL_PROM);
128 else
129 clrbits_be32(&regs->rctrl, RCTRL_PROM);
130
131 return 0;
132}
133
Bin Meng9872b732016-01-11 22:41:18 -0800134/*
135 * Initialized required registers to appropriate values, zeroing
Mingkai Hu90751912011-01-27 12:52:46 +0800136 * those we don't care about (unless zero is bad, in which case,
137 * choose a more appropriate value)
138 */
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300139static void init_registers(struct tsec __iomem *regs)
Mingkai Hu90751912011-01-27 12:52:46 +0800140{
141 /* Clear IEVENT */
142 out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
143
144 out_be32(&regs->imask, IMASK_INIT_CLEAR);
145
146 out_be32(&regs->hash.iaddr0, 0);
147 out_be32(&regs->hash.iaddr1, 0);
148 out_be32(&regs->hash.iaddr2, 0);
149 out_be32(&regs->hash.iaddr3, 0);
150 out_be32(&regs->hash.iaddr4, 0);
151 out_be32(&regs->hash.iaddr5, 0);
152 out_be32(&regs->hash.iaddr6, 0);
153 out_be32(&regs->hash.iaddr7, 0);
154
155 out_be32(&regs->hash.gaddr0, 0);
156 out_be32(&regs->hash.gaddr1, 0);
157 out_be32(&regs->hash.gaddr2, 0);
158 out_be32(&regs->hash.gaddr3, 0);
159 out_be32(&regs->hash.gaddr4, 0);
160 out_be32(&regs->hash.gaddr5, 0);
161 out_be32(&regs->hash.gaddr6, 0);
162 out_be32(&regs->hash.gaddr7, 0);
163
Mingkai Hu90751912011-01-27 12:52:46 +0800164 /* Init RMON mib registers */
Claudiu Manoil82ef75c2013-09-30 12:44:46 +0300165 memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
Mingkai Hu90751912011-01-27 12:52:46 +0800166
167 out_be32(&regs->rmon.cam1, 0xffffffff);
168 out_be32(&regs->rmon.cam2, 0xffffffff);
169
170 out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
171
172 out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
173
174 out_be32(&regs->attr, ATTR_INIT_SETTINGS);
175 out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
Mingkai Hu90751912011-01-27 12:52:46 +0800176}
177
Bin Meng9872b732016-01-11 22:41:18 -0800178/*
179 * Configure maccfg2 based on negotiated speed and duplex
Mingkai Hu90751912011-01-27 12:52:46 +0800180 * reported by PHY handling code
181 */
Andy Fleming063c1262011-04-08 02:10:54 -0500182static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
Mingkai Hu90751912011-01-27 12:52:46 +0800183{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300184 struct tsec __iomem *regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800185 u32 ecntrl, maccfg2;
186
Andy Fleming063c1262011-04-08 02:10:54 -0500187 if (!phydev->link) {
188 printf("%s: No link.\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800189 return;
190 }
191
192 /* clear all bits relative with interface mode */
193 ecntrl = in_be32(&regs->ecntrl);
194 ecntrl &= ~ECNTRL_R100;
195
196 maccfg2 = in_be32(&regs->maccfg2);
197 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
198
Andy Fleming063c1262011-04-08 02:10:54 -0500199 if (phydev->duplex)
Mingkai Hu90751912011-01-27 12:52:46 +0800200 maccfg2 |= MACCFG2_FULL_DUPLEX;
201
Andy Fleming063c1262011-04-08 02:10:54 -0500202 switch (phydev->speed) {
Mingkai Hu90751912011-01-27 12:52:46 +0800203 case 1000:
204 maccfg2 |= MACCFG2_GMII;
205 break;
206 case 100:
207 case 10:
208 maccfg2 |= MACCFG2_MII;
209
Bin Meng9872b732016-01-11 22:41:18 -0800210 /*
211 * Set R100 bit in all modes although
Mingkai Hu90751912011-01-27 12:52:46 +0800212 * it is only used in RGMII mode
213 */
Andy Fleming063c1262011-04-08 02:10:54 -0500214 if (phydev->speed == 100)
Mingkai Hu90751912011-01-27 12:52:46 +0800215 ecntrl |= ECNTRL_R100;
216 break;
217 default:
Andy Fleming063c1262011-04-08 02:10:54 -0500218 printf("%s: Speed was bad\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800219 break;
220 }
221
222 out_be32(&regs->ecntrl, ecntrl);
223 out_be32(&regs->maccfg2, maccfg2);
224
Andy Fleming063c1262011-04-08 02:10:54 -0500225 printf("Speed: %d, %s duplex%s\n", phydev->speed,
Mario Sixd38de332018-01-15 11:08:21 +0100226 (phydev->duplex) ? "full" : "half",
227 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Mingkai Hu90751912011-01-27 12:52:46 +0800228}
229
Bin Meng8ba50172016-01-11 22:41:21 -0800230/*
231 * This returns the status bits of the device. The return value
232 * is never checked, and this is what the 8260 driver did, so we
233 * do the same. Presumably, this would be zero if there were no
234 * errors
235 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800236static int tsec_send(struct udevice *dev, void *packet, int length)
Bin Meng8ba50172016-01-11 22:41:21 -0800237{
Simon Glass0fd3d912020-12-22 19:30:28 -0700238 struct tsec_private *priv;
239 struct tsec __iomem *regs;
Bin Meng8ba50172016-01-11 22:41:21 -0800240 int result = 0;
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300241 u16 status;
Bin Meng8ba50172016-01-11 22:41:21 -0800242 int i;
243
Simon Glass0fd3d912020-12-22 19:30:28 -0700244 priv = dev_get_priv(dev);
Simon Glass0fd3d912020-12-22 19:30:28 -0700245 regs = priv->regs;
Bin Meng8ba50172016-01-11 22:41:21 -0800246 /* Find an empty buffer descriptor */
247 for (i = 0;
248 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
249 i++) {
250 if (i >= TOUT_LOOP) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300251 printf("%s: tsec: tx buffers full\n", dev->name);
Bin Meng8ba50172016-01-11 22:41:21 -0800252 return result;
253 }
254 }
255
256 out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
257 out_be16(&priv->txbd[priv->tx_idx].length, length);
258 status = in_be16(&priv->txbd[priv->tx_idx].status);
259 out_be16(&priv->txbd[priv->tx_idx].status, status |
260 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
261
262 /* Tell the DMA to go */
263 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
264
265 /* Wait for buffer to be transmitted */
266 for (i = 0;
267 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
268 i++) {
269 if (i >= TOUT_LOOP) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300270 printf("%s: tsec: tx error\n", dev->name);
Bin Meng8ba50172016-01-11 22:41:21 -0800271 return result;
272 }
273 }
274
275 priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
276 result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
277
278 return result;
279}
280
Bin Meng9a1d6af2016-01-11 22:41:24 -0800281static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
282{
Simon Glass0fd3d912020-12-22 19:30:28 -0700283 struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800284 struct tsec __iomem *regs = priv->regs;
285 int ret = -1;
286
287 if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
288 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
Mario Sixd38de332018-01-15 11:08:21 +0100289 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
290 u32 buf;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800291
292 /* Send the packet up if there were no errors */
293 if (!(status & RXBD_STATS)) {
294 buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
295 *packetp = (uchar *)buf;
296 ret = length - 4;
297 } else {
298 printf("Got error %x\n", (status & RXBD_STATS));
299 }
300 }
301
302 if (in_be32(&regs->ievent) & IEVENT_BSY) {
303 out_be32(&regs->ievent, IEVENT_BSY);
304 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
305 }
306
307 return ret;
308}
309
310static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
311{
Simon Glass0fd3d912020-12-22 19:30:28 -0700312 struct tsec_private *priv = (struct tsec_private *)dev_get_priv(dev);
Mario Sixd38de332018-01-15 11:08:21 +0100313 u16 status;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800314
315 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
316
317 status = RXBD_EMPTY;
318 /* Set the wrap bit if this is the last element in the list */
319 if ((priv->rx_idx + 1) == PKTBUFSRX)
320 status |= RXBD_WRAP;
321 out_be16(&priv->rxbd[priv->rx_idx].status, status);
322
323 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
324
325 return 0;
326}
Bin Meng8ba50172016-01-11 22:41:21 -0800327
Bin Meng9a1d6af2016-01-11 22:41:24 -0800328static void tsec_halt(struct udevice *dev)
Bin Meng8ba50172016-01-11 22:41:21 -0800329{
Simon Glass0fd3d912020-12-22 19:30:28 -0700330 struct tsec_private *priv;
331 struct tsec __iomem *regs;
Simon Glass0fd3d912020-12-22 19:30:28 -0700332 priv = dev_get_priv(dev);
Simon Glass0fd3d912020-12-22 19:30:28 -0700333 regs = priv->regs;
Bin Meng8ba50172016-01-11 22:41:21 -0800334
335 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
336 setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
337
338 while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
339 != (IEVENT_GRSC | IEVENT_GTSC))
340 ;
341
342 clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
343
344 /* Shut down the PHY, as needed */
345 phy_shutdown(priv->phydev);
346}
347
chenhui zhaoaada81d2011-10-03 08:38:50 -0500348#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
349/*
350 * When MACCFG1[Rx_EN] is enabled during system boot as part
351 * of the eTSEC port initialization sequence,
352 * the eTSEC Rx logic may not be properly initialized.
353 */
Bin Meng10aaefb2021-11-01 14:15:12 +0800354static void redundant_init(struct tsec_private *priv)
chenhui zhaoaada81d2011-10-03 08:38:50 -0500355{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300356 struct tsec __iomem *regs = priv->regs;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500357 uint t, count = 0;
358 int fail = 1;
359 static const u8 pkt[] = {
360 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
361 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
362 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
363 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
364 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
365 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
366 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
367 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
368 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
369 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
370 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
371 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
372 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
373 0x71, 0x72};
374
375 /* Enable promiscuous mode */
Vladimir Oltean9dcb8102021-09-29 18:04:36 +0300376 setbits_be32(&regs->rctrl, RCTRL_PROM);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500377 /* Enable loopback mode */
378 setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
379 /* Enable transmit and receive */
380 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
381
382 /* Tell the DMA it is clear to go */
383 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
384 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
385 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
386 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
387
388 do {
Mario Sixd38de332018-01-15 11:08:21 +0100389 u16 status;
390
Bin Meng56a27a12016-01-11 22:41:22 -0800391 tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
chenhui zhaoaada81d2011-10-03 08:38:50 -0500392
393 /* Wait for buffer to be received */
Bin Menge677da92016-01-11 22:41:20 -0800394 for (t = 0;
395 in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800396 t++) {
chenhui zhaoaada81d2011-10-03 08:38:50 -0500397 if (t >= 10 * TOUT_LOOP) {
Bin Meng56a27a12016-01-11 22:41:22 -0800398 printf("%s: tsec: rx error\n", priv->dev->name);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500399 break;
400 }
401 }
402
Bin Meng362b1232016-01-11 22:41:19 -0800403 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
chenhui zhaoaada81d2011-10-03 08:38:50 -0500404 fail = 0;
405
Bin Menge677da92016-01-11 22:41:20 -0800406 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300407 status = RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800408 if ((priv->rx_idx + 1) == PKTBUFSRX)
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300409 status |= RXBD_WRAP;
Bin Menge677da92016-01-11 22:41:20 -0800410 out_be16(&priv->rxbd[priv->rx_idx].status, status);
Bin Meng362b1232016-01-11 22:41:19 -0800411 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500412
413 if (in_be32(&regs->ievent) & IEVENT_BSY) {
414 out_be32(&regs->ievent, IEVENT_BSY);
415 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
416 }
417 if (fail) {
418 printf("loopback recv packet error!\n");
419 clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
420 udelay(1000);
421 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
422 }
423 } while ((count++ < 4) && (fail == 1));
424
425 if (fail)
426 panic("eTSEC init fail!\n");
427 /* Disable promiscuous mode */
Vladimir Oltean9dcb8102021-09-29 18:04:36 +0300428 clrbits_be32(&regs->rctrl, RCTRL_PROM);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500429 /* Disable loopback mode */
430 clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
431}
432#endif
433
Bin Meng9872b732016-01-11 22:41:18 -0800434/*
435 * Set up the buffers and their descriptors, and bring up the
Mingkai Hu90751912011-01-27 12:52:46 +0800436 * interface
437 */
Bin Meng56a27a12016-01-11 22:41:22 -0800438static void startup_tsec(struct tsec_private *priv)
Mingkai Hu90751912011-01-27 12:52:46 +0800439{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300440 struct tsec __iomem *regs = priv->regs;
Mario Sixd38de332018-01-15 11:08:21 +0100441 u16 status;
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300442 int i;
Mingkai Hu90751912011-01-27 12:52:46 +0800443
Andy Fleming063c1262011-04-08 02:10:54 -0500444 /* reset the indices to zero */
Bin Meng362b1232016-01-11 22:41:19 -0800445 priv->rx_idx = 0;
446 priv->tx_idx = 0;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500447#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
448 uint svr;
449#endif
Andy Fleming063c1262011-04-08 02:10:54 -0500450
Mingkai Hu90751912011-01-27 12:52:46 +0800451 /* Point to the buffer descriptors */
Bin Menge677da92016-01-11 22:41:20 -0800452 out_be32(&regs->tbase, (u32)&priv->txbd[0]);
453 out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
Mingkai Hu90751912011-01-27 12:52:46 +0800454
455 /* Initialize the Rx Buffer descriptors */
456 for (i = 0; i < PKTBUFSRX; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800457 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
458 out_be16(&priv->rxbd[i].length, 0);
459 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
Mingkai Hu90751912011-01-27 12:52:46 +0800460 }
Bin Menge677da92016-01-11 22:41:20 -0800461 status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
462 out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800463
464 /* Initialize the TX Buffer Descriptors */
465 for (i = 0; i < TX_BUF_CNT; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800466 out_be16(&priv->txbd[i].status, 0);
467 out_be16(&priv->txbd[i].length, 0);
468 out_be32(&priv->txbd[i].bufptr, 0);
Mingkai Hu90751912011-01-27 12:52:46 +0800469 }
Bin Menge677da92016-01-11 22:41:20 -0800470 status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
471 out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800472
chenhui zhaoaada81d2011-10-03 08:38:50 -0500473#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
474 svr = get_svr();
475 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
Bin Meng56a27a12016-01-11 22:41:22 -0800476 redundant_init(priv);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500477#endif
Mingkai Hu90751912011-01-27 12:52:46 +0800478 /* Enable Transmit and Receive */
479 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
480
481 /* Tell the DMA it is clear to go */
482 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
483 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
484 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
485 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
486}
487
Bin Meng9872b732016-01-11 22:41:18 -0800488/*
Bin Meng9872b732016-01-11 22:41:18 -0800489 * Initializes data structures and registers for the controller,
490 * and brings the interface up. Returns the link status, meaning
Mingkai Hu90751912011-01-27 12:52:46 +0800491 * that it returns success if the link is up, failure otherwise.
Bin Meng9872b732016-01-11 22:41:18 -0800492 * This allows U-Boot to find the first active controller.
Mingkai Hu90751912011-01-27 12:52:46 +0800493 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800494static int tsec_init(struct udevice *dev)
Mingkai Hu90751912011-01-27 12:52:46 +0800495{
Simon Glass0fd3d912020-12-22 19:30:28 -0700496 struct tsec_private *priv;
497 struct tsec __iomem *regs;
Simon Glassc69cda22020-12-03 16:55:20 -0700498 struct eth_pdata *pdata = dev_get_plat(dev);
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300499 u32 tempval;
Timur Tabi11af8d62012-07-09 08:52:43 +0000500 int ret;
Mingkai Hu90751912011-01-27 12:52:46 +0800501
Simon Glass0fd3d912020-12-22 19:30:28 -0700502 priv = dev_get_priv(dev);
Simon Glass0fd3d912020-12-22 19:30:28 -0700503 regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800504 /* Make sure the controller is stopped */
505 tsec_halt(dev);
506
507 /* Init MACCFG2. Defaults to GMII */
508 out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
509
510 /* Init ECNTRL */
511 out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
512
Bin Meng9872b732016-01-11 22:41:18 -0800513 /*
514 * Copy the station address into the address registers.
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300515 * For a station address of 0x12345678ABCD in transmission
516 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
517 * MACnADDR2 is set to 0x34120000.
518 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800519 tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
520 (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
Mingkai Hu90751912011-01-27 12:52:46 +0800521
522 out_be32(&regs->macstnaddr1, tempval);
523
Bin Meng9a1d6af2016-01-11 22:41:24 -0800524 tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
Mingkai Hu90751912011-01-27 12:52:46 +0800525
526 out_be32(&regs->macstnaddr2, tempval);
527
Mingkai Hu90751912011-01-27 12:52:46 +0800528 /* Clear out (for the most part) the other registers */
529 init_registers(regs);
530
531 /* Ready the device for tx/rx */
Bin Meng56a27a12016-01-11 22:41:22 -0800532 startup_tsec(priv);
Mingkai Hu90751912011-01-27 12:52:46 +0800533
Andy Fleming063c1262011-04-08 02:10:54 -0500534 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000535 ret = phy_startup(priv->phydev);
536 if (ret) {
537 printf("Could not initialize PHY %s\n",
538 priv->phydev->dev->name);
539 return ret;
540 }
Andy Fleming063c1262011-04-08 02:10:54 -0500541
542 adjust_link(priv, priv->phydev);
543
Mingkai Hu90751912011-01-27 12:52:46 +0800544 /* If there's no link, fail */
Andy Fleming063c1262011-04-08 02:10:54 -0500545 return priv->phydev->link ? 0 : -1;
Mingkai Hu90751912011-01-27 12:52:46 +0800546}
547
Ramon Fried596ec9b2021-09-28 18:49:02 +0300548static phy_interface_t __maybe_unused tsec_get_interface(struct tsec_private *priv)
Andy Fleming063c1262011-04-08 02:10:54 -0500549{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300550 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500551 u32 ecntrl;
552
553 ecntrl = in_be32(&regs->ecntrl);
554
555 if (ecntrl & ECNTRL_SGMII_MODE)
556 return PHY_INTERFACE_MODE_SGMII;
557
558 if (ecntrl & ECNTRL_TBI_MODE) {
559 if (ecntrl & ECNTRL_REDUCED_MODE)
560 return PHY_INTERFACE_MODE_RTBI;
561 else
562 return PHY_INTERFACE_MODE_TBI;
563 }
564
565 if (ecntrl & ECNTRL_REDUCED_MODE) {
Mario Sixd38de332018-01-15 11:08:21 +0100566 phy_interface_t interface;
567
Andy Fleming063c1262011-04-08 02:10:54 -0500568 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
569 return PHY_INTERFACE_MODE_RMII;
Andy Fleming063c1262011-04-08 02:10:54 -0500570
Mario Sixd38de332018-01-15 11:08:21 +0100571 interface = priv->interface;
Andy Fleming063c1262011-04-08 02:10:54 -0500572
Mario Sixd38de332018-01-15 11:08:21 +0100573 /*
574 * This isn't autodetected, so it must
575 * be set by the platform code.
576 */
577 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
578 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
579 interface == PHY_INTERFACE_MODE_RGMII_RXID)
580 return interface;
581
582 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming063c1262011-04-08 02:10:54 -0500583 }
584
585 if (priv->flags & TSEC_GIGABIT)
586 return PHY_INTERFACE_MODE_GMII;
587
588 return PHY_INTERFACE_MODE_MII;
589}
590
Bin Meng9872b732016-01-11 22:41:18 -0800591/*
592 * Discover which PHY is attached to the device, and configure it
Mingkai Hu90751912011-01-27 12:52:46 +0800593 * properly. If the PHY is not recognized, then return 0
594 * (failure). Otherwise, return 1
595 */
Bin Meng56a27a12016-01-11 22:41:22 -0800596static int init_phy(struct tsec_private *priv)
Mingkai Hu90751912011-01-27 12:52:46 +0800597{
Andy Fleming063c1262011-04-08 02:10:54 -0500598 struct phy_device *phydev;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300599 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500600 u32 supported = (SUPPORTED_10baseT_Half |
601 SUPPORTED_10baseT_Full |
602 SUPPORTED_100baseT_Half |
603 SUPPORTED_100baseT_Full);
604
605 if (priv->flags & TSEC_GIGABIT)
606 supported |= SUPPORTED_1000baseT_Full;
Mingkai Hu90751912011-01-27 12:52:46 +0800607
608 /* Assign a Physical address to the TBI */
Bin Menga1c76c12016-01-11 22:41:25 -0800609 out_be32(&regs->tbipa, priv->tbiaddr);
Mingkai Hu90751912011-01-27 12:52:46 +0800610
Andy Fleming063c1262011-04-08 02:10:54 -0500611 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
Mingkai Hu90751912011-01-27 12:52:46 +0800612 tsec_configure_serdes(priv);
613
Tom Rini2f420f12022-11-27 10:25:04 -0500614#if defined(CONFIG_DM_MDIO)
Vladimir Oltean3c562512021-03-14 20:14:56 +0800615 phydev = dm_eth_phy_connect(priv->dev);
Hou Zhiqiangb4eb9cf2020-07-16 18:09:12 +0800616#else
Bin Meng56a27a12016-01-11 22:41:22 -0800617 phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
618 priv->interface);
Hou Zhiqiangb4eb9cf2020-07-16 18:09:12 +0800619#endif
Claudiu Manoil7f233c02013-12-10 15:21:04 +0200620 if (!phydev)
621 return 0;
Mingkai Hu90751912011-01-27 12:52:46 +0800622
Andy Fleming063c1262011-04-08 02:10:54 -0500623 phydev->supported &= supported;
624 phydev->advertising = phydev->supported;
625
626 priv->phydev = phydev;
627
628 phy_config(phydev);
Mingkai Hu90751912011-01-27 12:52:46 +0800629
630 return 1;
631}
632
Bin Meng9a1d6af2016-01-11 22:41:24 -0800633int tsec_probe(struct udevice *dev)
634{
Simon Glassc69cda22020-12-03 16:55:20 -0700635 struct eth_pdata *pdata = dev_get_plat(dev);
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300636 struct tsec_private *priv = dev_get_priv(dev);
Mario Six1313aaf2018-01-15 11:08:23 +0100637 struct ofnode_phandle_args phandle_args;
Tom Rini65cc0e22022-11-16 13:10:41 -0500638 u32 tbiaddr = CFG_SYS_TBIPA_VALUE;
Hou Zhiqiang7fb568d2020-07-16 18:09:14 +0800639 struct tsec_data *data;
Bin Menga0815462021-03-14 20:15:01 +0800640 ofnode parent, child;
Vladimir Olteanbca686a2019-07-19 00:29:54 +0300641 fdt_addr_t reg;
Aleksandar Gerasimovski50dae8e2021-06-04 13:40:58 +0000642 u32 max_speed;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800643 int ret;
644
Hou Zhiqiang7fb568d2020-07-16 18:09:14 +0800645 data = (struct tsec_data *)dev_get_driver_data(dev);
646
Mario Six1313aaf2018-01-15 11:08:23 +0100647 pdata->iobase = (phys_addr_t)dev_read_addr(dev);
Bin Menga0815462021-03-14 20:15:01 +0800648 if (pdata->iobase == FDT_ADDR_T_NONE) {
649 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
650 if (strncmp(ofnode_get_name(child), "queue-group",
651 strlen("queue-group")))
652 continue;
653
654 reg = ofnode_get_addr(child);
655 if (reg == FDT_ADDR_T_NONE) {
656 printf("No 'reg' property of <queue-group>\n");
657 return -ENOENT;
658 }
659 pdata->iobase = reg;
660
661 /*
662 * if there are multiple queue groups,
663 * only the first one is used.
664 */
665 break;
666 }
667
668 if (!ofnode_valid(child)) {
669 printf("No child node for <queue-group>?\n");
670 return -ENOENT;
671 }
672 }
673
Bin Meng408f0562021-03-14 20:14:59 +0800674 priv->regs = map_physmem(pdata->iobase, 0, MAP_NOCACHE);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800675
Vladimir Oltean29db3102019-07-19 00:29:53 +0300676 ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
677 &phandle_args);
Hou Zhiqianga0f47e02020-05-03 22:48:43 +0800678 if (ret == 0) {
Vladimir Oltean29db3102019-07-19 00:29:53 +0300679 ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
680
Hou Zhiqianga0f47e02020-05-03 22:48:43 +0800681 parent = ofnode_get_parent(phandle_args.node);
682 if (!ofnode_valid(parent)) {
683 printf("No parent node for TBI PHY?\n");
684 return -ENOENT;
685 }
686
687 reg = ofnode_get_addr_index(parent, 0);
688 if (reg == FDT_ADDR_T_NONE) {
689 printf("No 'reg' property of MII for TBI PHY\n");
690 return -ENOENT;
691 }
692
Hou Zhiqiang7fb568d2020-07-16 18:09:14 +0800693 priv->phyregs_sgmii = map_physmem(reg + data->mdio_regs_off,
Hou Zhiqianga0f47e02020-05-03 22:48:43 +0800694 0, MAP_NOCACHE);
695 }
696
Vladimir Oltean29db3102019-07-19 00:29:53 +0300697 priv->tbiaddr = tbiaddr;
Bin Menga1c76c12016-01-11 22:41:25 -0800698
Marek BehĂșn123ca112022-04-07 00:33:01 +0200699 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșnffb0f6f2022-04-07 00:33:03 +0200700 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Vladimir Olteand883a5f2021-09-18 15:46:54 +0300701 pdata->phy_interface = tsec_get_interface(priv);
702
Bin Meng9a1d6af2016-01-11 22:41:24 -0800703 priv->interface = pdata->phy_interface;
704
Aleksandar Gerasimovski50dae8e2021-06-04 13:40:58 +0000705 /* Check for speed limit, default is 1000Mbps */
706 max_speed = dev_read_u32_default(dev, "max-speed", 1000);
707
Bin Meng9a1d6af2016-01-11 22:41:24 -0800708 /* Initialize flags */
Aleksandar Gerasimovski50dae8e2021-06-04 13:40:58 +0000709 if (max_speed == 1000)
710 priv->flags = TSEC_GIGABIT;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800711 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
712 priv->flags |= TSEC_SGMII;
713
Bin Meng9a1d6af2016-01-11 22:41:24 -0800714 /* Reset the MAC */
715 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
716 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
717 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
718
719 priv->dev = dev;
720 priv->bus = miiphy_get_dev_by_name(dev->name);
721
722 /* Try to initialize PHY here, and return */
723 return !init_phy(priv);
724}
725
726int tsec_remove(struct udevice *dev)
727{
Simon Glass0fd3d912020-12-22 19:30:28 -0700728 struct tsec_private *priv = dev_get_priv(dev);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800729
730 free(priv->phydev);
731 mdio_unregister(priv->bus);
732 mdio_free(priv->bus);
733
734 return 0;
735}
736
737static const struct eth_ops tsec_ops = {
738 .start = tsec_init,
739 .send = tsec_send,
740 .recv = tsec_recv,
741 .free_pkt = tsec_free_pkt,
742 .stop = tsec_halt,
Bin Meng9a1d6af2016-01-11 22:41:24 -0800743 .mcast = tsec_mcast_addr,
Vladimir Oltean9dcb8102021-09-29 18:04:36 +0300744 .set_promisc = tsec_set_promisc,
Bin Meng9a1d6af2016-01-11 22:41:24 -0800745};
746
Hou Zhiqiang7fb568d2020-07-16 18:09:14 +0800747static struct tsec_data etsec2_data = {
748 .mdio_regs_off = TSEC_MDIO_REGS_OFFSET,
749};
750
751static struct tsec_data gianfar_data = {
752 .mdio_regs_off = 0x0,
753};
754
Bin Meng9a1d6af2016-01-11 22:41:24 -0800755static const struct udevice_id tsec_ids[] = {
Hou Zhiqiang7fb568d2020-07-16 18:09:14 +0800756 { .compatible = "fsl,etsec2", .data = (ulong)&etsec2_data },
757 { .compatible = "gianfar", .data = (ulong)&gianfar_data },
Bin Meng9a1d6af2016-01-11 22:41:24 -0800758 { }
759};
760
761U_BOOT_DRIVER(eth_tsec) = {
762 .name = "tsec",
763 .id = UCLASS_ETH,
764 .of_match = tsec_ids,
765 .probe = tsec_probe,
766 .remove = tsec_remove,
767 .ops = &tsec_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700768 .priv_auto = sizeof(struct tsec_private),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700769 .plat_auto = sizeof(struct eth_pdata),
Bin Meng9a1d6af2016-01-11 22:41:24 -0800770 .flags = DM_FLAG_ALLOC_PRIV_DMA,
771};