blob: f85cdcb97ebf4d0b943ba3194953b694a7dea290 [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>
wdenk42d1f032003-10-15 23:53:47 +000011#include <common.h>
Bin Meng9a1d6af2016-01-11 22:41:24 -080012#include <dm.h>
wdenk42d1f032003-10-15 23:53:47 +000013#include <malloc.h>
14#include <net.h>
15#include <command.h>
Andy Flemingdd3d1f52008-08-31 16:33:25 -050016#include <tsec.h>
Andy Fleming063c1262011-04-08 02:10:54 -050017#include <fsl_mdio.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
chenhui zhaoaada81d2011-10-03 08:38:50 -050019#include <asm/processor.h>
Alison Wang52d00a82014-09-05 13:52:38 +080020#include <asm/io.h>
wdenk42d1f032003-10-15 23:53:47 +000021
Bin Meng9a1d6af2016-01-11 22:41:24 -080022#ifndef CONFIG_DM_ETH
Andy Fleming75b9d4a2008-08-31 16:33:26 -050023/* Default initializations for TSEC controllers. */
24
25static struct tsec_info_struct tsec_info[] = {
26#ifdef CONFIG_TSEC1
27 STD_TSEC_INFO(1), /* TSEC1 */
28#endif
29#ifdef CONFIG_TSEC2
30 STD_TSEC_INFO(2), /* TSEC2 */
31#endif
32#ifdef CONFIG_MPC85XX_FEC
33 {
Claudiu Manoilaec84bf2013-09-30 12:44:42 +030034 .regs = TSEC_GET_REGS(2, 0x2000),
Andy Fleming75b9d4a2008-08-31 16:33:26 -050035 .devname = CONFIG_MPC85XX_FEC_NAME,
36 .phyaddr = FEC_PHY_ADDR,
Andy Fleming063c1262011-04-08 02:10:54 -050037 .flags = FEC_FLAGS,
38 .mii_devname = DEFAULT_MII_NAME
Andy Fleming75b9d4a2008-08-31 16:33:26 -050039 }, /* FEC */
40#endif
41#ifdef CONFIG_TSEC3
42 STD_TSEC_INFO(3), /* TSEC3 */
43#endif
44#ifdef CONFIG_TSEC4
45 STD_TSEC_INFO(4), /* TSEC4 */
46#endif
47};
Bin Meng9a1d6af2016-01-11 22:41:24 -080048#endif /* CONFIG_DM_ETH */
Andy Fleming75b9d4a2008-08-31 16:33:26 -050049
Andy Fleming2abe3612008-08-31 16:33:27 -050050#define TBIANA_SETTINGS ( \
51 TBIANA_ASYMMETRIC_PAUSE \
52 | TBIANA_SYMMETRIC_PAUSE \
53 | TBIANA_FULL_DUPLEX \
54 )
55
Felix Radensky90b5bf22010-06-28 01:57:39 +030056/* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
57#ifndef CONFIG_TSEC_TBICR_SETTINGS
Kumar Gala72c96a62010-12-01 22:55:54 -060058#define CONFIG_TSEC_TBICR_SETTINGS ( \
Andy Fleming2abe3612008-08-31 16:33:27 -050059 TBICR_PHY_RESET \
Kumar Gala72c96a62010-12-01 22:55:54 -060060 | TBICR_ANEG_ENABLE \
Andy Fleming2abe3612008-08-31 16:33:27 -050061 | TBICR_FULL_DUPLEX \
62 | TBICR_SPEED1_SET \
63 )
Felix Radensky90b5bf22010-06-28 01:57:39 +030064#endif /* CONFIG_TSEC_TBICR_SETTINGS */
Peter Tyser46e91672009-11-03 17:52:07 -060065
Andy Fleming2abe3612008-08-31 16:33:27 -050066/* Configure the TBI for SGMII operation */
67static void tsec_configure_serdes(struct tsec_private *priv)
68{
Bin Meng9872b732016-01-11 22:41:18 -080069 /*
70 * Access TBI PHY registers at given TSEC register offset as opposed
71 * to the register offset used for external PHY accesses
72 */
Andy Fleming063c1262011-04-08 02:10:54 -050073 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Mario Sixd38de332018-01-15 11:08:21 +010074 0, TBI_ANA, TBIANA_SETTINGS);
Andy Fleming063c1262011-04-08 02:10:54 -050075 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Mario Sixd38de332018-01-15 11:08:21 +010076 0, TBI_TBICON, TBICON_CLK_SELECT);
Andy Fleming063c1262011-04-08 02:10:54 -050077 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
Mario Sixd38de332018-01-15 11:08:21 +010078 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
Andy Fleming2abe3612008-08-31 16:33:27 -050079}
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +000080
Chris Packham1a4af5c2018-11-26 21:00:28 +130081/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
82 * and this is the ethernet-crc method needed for TSEC -- and perhaps
83 * some other adapter -- hash tables
84 */
85#define CRCPOLY_LE 0xedb88320
86static u32 ether_crc(size_t len, unsigned char const *p)
87{
88 int i;
89 u32 crc;
90
91 crc = ~0;
92 while (len--) {
93 crc ^= *p++;
94 for (i = 0; i < 8; i++)
95 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
96 }
97 /* an reverse the bits, cuz of way they arrive -- last-first */
98 crc = (crc >> 16) | (crc << 16);
99 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
100 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
101 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
102 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
103 return crc;
104}
105
David Updegraff53a5c422007-06-11 10:41:07 -0500106/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
107
108/* Set the appropriate hash bit for the given addr */
109
Bin Meng9872b732016-01-11 22:41:18 -0800110/*
111 * The algorithm works like so:
David Updegraff53a5c422007-06-11 10:41:07 -0500112 * 1) Take the Destination Address (ie the multicast address), and
113 * do a CRC on it (little endian), and reverse the bits of the
114 * result.
115 * 2) Use the 8 most significant bits as a hash into a 256-entry
116 * table. The table is controlled through 8 32-bit registers:
Claudiu Manoil876d4512013-09-30 12:44:40 +0300117 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
118 * 255. This means that the 3 most significant bits in the
David Updegraff53a5c422007-06-11 10:41:07 -0500119 * hash index which gaddr register to use, and the 5 other bits
120 * indicate which bit (assuming an IBM numbering scheme, which
Claudiu Manoil876d4512013-09-30 12:44:40 +0300121 * for PowerPC (tm) is usually the case) in the register holds
Bin Meng9872b732016-01-11 22:41:18 -0800122 * the entry.
123 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800124#ifndef CONFIG_DM_ETH
Chris Packham67bb9842018-11-26 21:00:29 +1300125static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac,
126 int join)
Bin Meng9a1d6af2016-01-11 22:41:24 -0800127#else
Chris Packham67bb9842018-11-26 21:00:29 +1300128static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
Bin Meng9a1d6af2016-01-11 22:41:24 -0800129#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500130{
Claudiu Manoilb2002042013-09-30 12:44:41 +0300131 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoil876d4512013-09-30 12:44:40 +0300132 struct tsec __iomem *regs = priv->regs;
133 u32 result, value;
134 u8 whichbit, whichreg;
David Updegraff53a5c422007-06-11 10:41:07 -0500135
Claudiu Manoil876d4512013-09-30 12:44:40 +0300136 result = ether_crc(MAC_ADDR_LEN, mcast_mac);
137 whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
138 whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
David Updegraff53a5c422007-06-11 10:41:07 -0500139
Mario Sixd38de332018-01-15 11:08:21 +0100140 value = BIT(31 - whichbit);
David Updegraff53a5c422007-06-11 10:41:07 -0500141
Chris Packham67bb9842018-11-26 21:00:29 +1300142 if (join)
Claudiu Manoil876d4512013-09-30 12:44:40 +0300143 setbits_be32(&regs->hash.gaddr0 + whichreg, value);
144 else
145 clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
146
David Updegraff53a5c422007-06-11 10:41:07 -0500147 return 0;
148}
Mingkai Hu90751912011-01-27 12:52:46 +0800149
Bin Meng9872b732016-01-11 22:41:18 -0800150/*
151 * Initialized required registers to appropriate values, zeroing
Mingkai Hu90751912011-01-27 12:52:46 +0800152 * those we don't care about (unless zero is bad, in which case,
153 * choose a more appropriate value)
154 */
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300155static void init_registers(struct tsec __iomem *regs)
Mingkai Hu90751912011-01-27 12:52:46 +0800156{
157 /* Clear IEVENT */
158 out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
159
160 out_be32(&regs->imask, IMASK_INIT_CLEAR);
161
162 out_be32(&regs->hash.iaddr0, 0);
163 out_be32(&regs->hash.iaddr1, 0);
164 out_be32(&regs->hash.iaddr2, 0);
165 out_be32(&regs->hash.iaddr3, 0);
166 out_be32(&regs->hash.iaddr4, 0);
167 out_be32(&regs->hash.iaddr5, 0);
168 out_be32(&regs->hash.iaddr6, 0);
169 out_be32(&regs->hash.iaddr7, 0);
170
171 out_be32(&regs->hash.gaddr0, 0);
172 out_be32(&regs->hash.gaddr1, 0);
173 out_be32(&regs->hash.gaddr2, 0);
174 out_be32(&regs->hash.gaddr3, 0);
175 out_be32(&regs->hash.gaddr4, 0);
176 out_be32(&regs->hash.gaddr5, 0);
177 out_be32(&regs->hash.gaddr6, 0);
178 out_be32(&regs->hash.gaddr7, 0);
179
180 out_be32(&regs->rctrl, 0x00000000);
181
182 /* Init RMON mib registers */
Claudiu Manoil82ef75c2013-09-30 12:44:46 +0300183 memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
Mingkai Hu90751912011-01-27 12:52:46 +0800184
185 out_be32(&regs->rmon.cam1, 0xffffffff);
186 out_be32(&regs->rmon.cam2, 0xffffffff);
187
188 out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
189
190 out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
191
192 out_be32(&regs->attr, ATTR_INIT_SETTINGS);
193 out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
Mingkai Hu90751912011-01-27 12:52:46 +0800194}
195
Bin Meng9872b732016-01-11 22:41:18 -0800196/*
197 * Configure maccfg2 based on negotiated speed and duplex
Mingkai Hu90751912011-01-27 12:52:46 +0800198 * reported by PHY handling code
199 */
Andy Fleming063c1262011-04-08 02:10:54 -0500200static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
Mingkai Hu90751912011-01-27 12:52:46 +0800201{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300202 struct tsec __iomem *regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800203 u32 ecntrl, maccfg2;
204
Andy Fleming063c1262011-04-08 02:10:54 -0500205 if (!phydev->link) {
206 printf("%s: No link.\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800207 return;
208 }
209
210 /* clear all bits relative with interface mode */
211 ecntrl = in_be32(&regs->ecntrl);
212 ecntrl &= ~ECNTRL_R100;
213
214 maccfg2 = in_be32(&regs->maccfg2);
215 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
216
Andy Fleming063c1262011-04-08 02:10:54 -0500217 if (phydev->duplex)
Mingkai Hu90751912011-01-27 12:52:46 +0800218 maccfg2 |= MACCFG2_FULL_DUPLEX;
219
Andy Fleming063c1262011-04-08 02:10:54 -0500220 switch (phydev->speed) {
Mingkai Hu90751912011-01-27 12:52:46 +0800221 case 1000:
222 maccfg2 |= MACCFG2_GMII;
223 break;
224 case 100:
225 case 10:
226 maccfg2 |= MACCFG2_MII;
227
Bin Meng9872b732016-01-11 22:41:18 -0800228 /*
229 * Set R100 bit in all modes although
Mingkai Hu90751912011-01-27 12:52:46 +0800230 * it is only used in RGMII mode
231 */
Andy Fleming063c1262011-04-08 02:10:54 -0500232 if (phydev->speed == 100)
Mingkai Hu90751912011-01-27 12:52:46 +0800233 ecntrl |= ECNTRL_R100;
234 break;
235 default:
Andy Fleming063c1262011-04-08 02:10:54 -0500236 printf("%s: Speed was bad\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800237 break;
238 }
239
240 out_be32(&regs->ecntrl, ecntrl);
241 out_be32(&regs->maccfg2, maccfg2);
242
Andy Fleming063c1262011-04-08 02:10:54 -0500243 printf("Speed: %d, %s duplex%s\n", phydev->speed,
Mario Sixd38de332018-01-15 11:08:21 +0100244 (phydev->duplex) ? "full" : "half",
245 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Mingkai Hu90751912011-01-27 12:52:46 +0800246}
247
Bin Meng8ba50172016-01-11 22:41:21 -0800248/*
249 * This returns the status bits of the device. The return value
250 * is never checked, and this is what the 8260 driver did, so we
251 * do the same. Presumably, this would be zero if there were no
252 * errors
253 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800254#ifndef CONFIG_DM_ETH
Bin Meng8ba50172016-01-11 22:41:21 -0800255static int tsec_send(struct eth_device *dev, void *packet, int length)
Bin Meng9a1d6af2016-01-11 22:41:24 -0800256#else
257static int tsec_send(struct udevice *dev, void *packet, int length)
258#endif
Bin Meng8ba50172016-01-11 22:41:21 -0800259{
260 struct tsec_private *priv = (struct tsec_private *)dev->priv;
261 struct tsec __iomem *regs = priv->regs;
Bin Meng8ba50172016-01-11 22:41:21 -0800262 int result = 0;
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300263 u16 status;
Bin Meng8ba50172016-01-11 22:41:21 -0800264 int i;
265
266 /* Find an empty buffer descriptor */
267 for (i = 0;
268 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
269 i++) {
270 if (i >= TOUT_LOOP) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300271 printf("%s: tsec: tx buffers full\n", dev->name);
Bin Meng8ba50172016-01-11 22:41:21 -0800272 return result;
273 }
274 }
275
276 out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
277 out_be16(&priv->txbd[priv->tx_idx].length, length);
278 status = in_be16(&priv->txbd[priv->tx_idx].status);
279 out_be16(&priv->txbd[priv->tx_idx].status, status |
280 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
281
282 /* Tell the DMA to go */
283 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
284
285 /* Wait for buffer to be transmitted */
286 for (i = 0;
287 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
288 i++) {
289 if (i >= TOUT_LOOP) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300290 printf("%s: tsec: tx error\n", dev->name);
Bin Meng8ba50172016-01-11 22:41:21 -0800291 return result;
292 }
293 }
294
295 priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
296 result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
297
298 return result;
299}
300
Bin Meng9a1d6af2016-01-11 22:41:24 -0800301#ifndef CONFIG_DM_ETH
Bin Meng8ba50172016-01-11 22:41:21 -0800302static int tsec_recv(struct eth_device *dev)
303{
304 struct tsec_private *priv = (struct tsec_private *)dev->priv;
305 struct tsec __iomem *regs = priv->regs;
306
307 while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
308 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
Mario Sixd38de332018-01-15 11:08:21 +0100309 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
Bin Meng8ba50172016-01-11 22:41:21 -0800310 uchar *packet = net_rx_packets[priv->rx_idx];
311
312 /* Send the packet up if there were no errors */
313 if (!(status & RXBD_STATS))
314 net_process_received_packet(packet, length - 4);
315 else
316 printf("Got error %x\n", (status & RXBD_STATS));
317
318 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
319
320 status = RXBD_EMPTY;
321 /* Set the wrap bit if this is the last element in the list */
322 if ((priv->rx_idx + 1) == PKTBUFSRX)
323 status |= RXBD_WRAP;
324 out_be16(&priv->rxbd[priv->rx_idx].status, status);
325
326 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
327 }
328
329 if (in_be32(&regs->ievent) & IEVENT_BSY) {
330 out_be32(&regs->ievent, IEVENT_BSY);
331 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
332 }
333
334 return -1;
335}
Bin Meng9a1d6af2016-01-11 22:41:24 -0800336#else
337static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
338{
339 struct tsec_private *priv = (struct tsec_private *)dev->priv;
340 struct tsec __iomem *regs = priv->regs;
341 int ret = -1;
342
343 if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
344 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
Mario Sixd38de332018-01-15 11:08:21 +0100345 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
346 u32 buf;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800347
348 /* Send the packet up if there were no errors */
349 if (!(status & RXBD_STATS)) {
350 buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
351 *packetp = (uchar *)buf;
352 ret = length - 4;
353 } else {
354 printf("Got error %x\n", (status & RXBD_STATS));
355 }
356 }
357
358 if (in_be32(&regs->ievent) & IEVENT_BSY) {
359 out_be32(&regs->ievent, IEVENT_BSY);
360 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
361 }
362
363 return ret;
364}
365
366static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
367{
368 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Mario Sixd38de332018-01-15 11:08:21 +0100369 u16 status;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800370
371 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
372
373 status = RXBD_EMPTY;
374 /* Set the wrap bit if this is the last element in the list */
375 if ((priv->rx_idx + 1) == PKTBUFSRX)
376 status |= RXBD_WRAP;
377 out_be16(&priv->rxbd[priv->rx_idx].status, status);
378
379 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
380
381 return 0;
382}
383#endif
Bin Meng8ba50172016-01-11 22:41:21 -0800384
385/* Stop the interface */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800386#ifndef CONFIG_DM_ETH
Bin Meng8ba50172016-01-11 22:41:21 -0800387static void tsec_halt(struct eth_device *dev)
Bin Meng9a1d6af2016-01-11 22:41:24 -0800388#else
389static void tsec_halt(struct udevice *dev)
390#endif
Bin Meng8ba50172016-01-11 22:41:21 -0800391{
392 struct tsec_private *priv = (struct tsec_private *)dev->priv;
393 struct tsec __iomem *regs = priv->regs;
394
395 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
396 setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
397
398 while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
399 != (IEVENT_GRSC | IEVENT_GTSC))
400 ;
401
402 clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
403
404 /* Shut down the PHY, as needed */
405 phy_shutdown(priv->phydev);
406}
407
chenhui zhaoaada81d2011-10-03 08:38:50 -0500408#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
409/*
410 * When MACCFG1[Rx_EN] is enabled during system boot as part
411 * of the eTSEC port initialization sequence,
412 * the eTSEC Rx logic may not be properly initialized.
413 */
Bin Meng56a27a12016-01-11 22:41:22 -0800414void redundant_init(struct tsec_private *priv)
chenhui zhaoaada81d2011-10-03 08:38:50 -0500415{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300416 struct tsec __iomem *regs = priv->regs;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500417 uint t, count = 0;
418 int fail = 1;
419 static const u8 pkt[] = {
420 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
421 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
422 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
423 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
424 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
425 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
426 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
427 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
428 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
429 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
430 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
431 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
432 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
433 0x71, 0x72};
434
435 /* Enable promiscuous mode */
436 setbits_be32(&regs->rctrl, 0x8);
437 /* Enable loopback mode */
438 setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
439 /* Enable transmit and receive */
440 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
441
442 /* Tell the DMA it is clear to go */
443 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
444 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
445 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
446 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
447
448 do {
Mario Sixd38de332018-01-15 11:08:21 +0100449 u16 status;
450
Bin Meng56a27a12016-01-11 22:41:22 -0800451 tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
chenhui zhaoaada81d2011-10-03 08:38:50 -0500452
453 /* Wait for buffer to be received */
Bin Menge677da92016-01-11 22:41:20 -0800454 for (t = 0;
455 in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800456 t++) {
chenhui zhaoaada81d2011-10-03 08:38:50 -0500457 if (t >= 10 * TOUT_LOOP) {
Bin Meng56a27a12016-01-11 22:41:22 -0800458 printf("%s: tsec: rx error\n", priv->dev->name);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500459 break;
460 }
461 }
462
Bin Meng362b1232016-01-11 22:41:19 -0800463 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
chenhui zhaoaada81d2011-10-03 08:38:50 -0500464 fail = 0;
465
Bin Menge677da92016-01-11 22:41:20 -0800466 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300467 status = RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800468 if ((priv->rx_idx + 1) == PKTBUFSRX)
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300469 status |= RXBD_WRAP;
Bin Menge677da92016-01-11 22:41:20 -0800470 out_be16(&priv->rxbd[priv->rx_idx].status, status);
Bin Meng362b1232016-01-11 22:41:19 -0800471 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500472
473 if (in_be32(&regs->ievent) & IEVENT_BSY) {
474 out_be32(&regs->ievent, IEVENT_BSY);
475 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
476 }
477 if (fail) {
478 printf("loopback recv packet error!\n");
479 clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
480 udelay(1000);
481 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
482 }
483 } while ((count++ < 4) && (fail == 1));
484
485 if (fail)
486 panic("eTSEC init fail!\n");
487 /* Disable promiscuous mode */
488 clrbits_be32(&regs->rctrl, 0x8);
489 /* Disable loopback mode */
490 clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
491}
492#endif
493
Bin Meng9872b732016-01-11 22:41:18 -0800494/*
495 * Set up the buffers and their descriptors, and bring up the
Mingkai Hu90751912011-01-27 12:52:46 +0800496 * interface
497 */
Bin Meng56a27a12016-01-11 22:41:22 -0800498static void startup_tsec(struct tsec_private *priv)
Mingkai Hu90751912011-01-27 12:52:46 +0800499{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300500 struct tsec __iomem *regs = priv->regs;
Mario Sixd38de332018-01-15 11:08:21 +0100501 u16 status;
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300502 int i;
Mingkai Hu90751912011-01-27 12:52:46 +0800503
Andy Fleming063c1262011-04-08 02:10:54 -0500504 /* reset the indices to zero */
Bin Meng362b1232016-01-11 22:41:19 -0800505 priv->rx_idx = 0;
506 priv->tx_idx = 0;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500507#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
508 uint svr;
509#endif
Andy Fleming063c1262011-04-08 02:10:54 -0500510
Mingkai Hu90751912011-01-27 12:52:46 +0800511 /* Point to the buffer descriptors */
Bin Menge677da92016-01-11 22:41:20 -0800512 out_be32(&regs->tbase, (u32)&priv->txbd[0]);
513 out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
Mingkai Hu90751912011-01-27 12:52:46 +0800514
515 /* Initialize the Rx Buffer descriptors */
516 for (i = 0; i < PKTBUFSRX; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800517 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
518 out_be16(&priv->rxbd[i].length, 0);
519 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
Mingkai Hu90751912011-01-27 12:52:46 +0800520 }
Bin Menge677da92016-01-11 22:41:20 -0800521 status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
522 out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800523
524 /* Initialize the TX Buffer Descriptors */
525 for (i = 0; i < TX_BUF_CNT; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800526 out_be16(&priv->txbd[i].status, 0);
527 out_be16(&priv->txbd[i].length, 0);
528 out_be32(&priv->txbd[i].bufptr, 0);
Mingkai Hu90751912011-01-27 12:52:46 +0800529 }
Bin Menge677da92016-01-11 22:41:20 -0800530 status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
531 out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800532
chenhui zhaoaada81d2011-10-03 08:38:50 -0500533#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
534 svr = get_svr();
535 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
Bin Meng56a27a12016-01-11 22:41:22 -0800536 redundant_init(priv);
chenhui zhaoaada81d2011-10-03 08:38:50 -0500537#endif
Mingkai Hu90751912011-01-27 12:52:46 +0800538 /* Enable Transmit and Receive */
539 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
540
541 /* Tell the DMA it is clear to go */
542 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
543 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
544 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
545 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
546}
547
Bin Meng9872b732016-01-11 22:41:18 -0800548/*
Bin Meng9872b732016-01-11 22:41:18 -0800549 * Initializes data structures and registers for the controller,
550 * and brings the interface up. Returns the link status, meaning
Mingkai Hu90751912011-01-27 12:52:46 +0800551 * that it returns success if the link is up, failure otherwise.
Bin Meng9872b732016-01-11 22:41:18 -0800552 * This allows U-Boot to find the first active controller.
Mingkai Hu90751912011-01-27 12:52:46 +0800553 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800554#ifndef CONFIG_DM_ETH
Mario Sixd38de332018-01-15 11:08:21 +0100555static int tsec_init(struct eth_device *dev, bd_t *bd)
Bin Meng9a1d6af2016-01-11 22:41:24 -0800556#else
557static int tsec_init(struct udevice *dev)
558#endif
Mingkai Hu90751912011-01-27 12:52:46 +0800559{
Mingkai Hu90751912011-01-27 12:52:46 +0800560 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800561#ifdef CONFIG_DM_ETH
562 struct eth_pdata *pdata = dev_get_platdata(dev);
Vladimir Olteanf6297c02019-07-19 00:29:57 +0300563#else
564 struct eth_device *pdata = dev;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800565#endif
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300566 struct tsec __iomem *regs = priv->regs;
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300567 u32 tempval;
Timur Tabi11af8d62012-07-09 08:52:43 +0000568 int ret;
Mingkai Hu90751912011-01-27 12:52:46 +0800569
570 /* Make sure the controller is stopped */
571 tsec_halt(dev);
572
573 /* Init MACCFG2. Defaults to GMII */
574 out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
575
576 /* Init ECNTRL */
577 out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
578
Bin Meng9872b732016-01-11 22:41:18 -0800579 /*
580 * Copy the station address into the address registers.
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300581 * For a station address of 0x12345678ABCD in transmission
582 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
583 * MACnADDR2 is set to 0x34120000.
584 */
Bin Meng9a1d6af2016-01-11 22:41:24 -0800585 tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
586 (pdata->enetaddr[3] << 8) | pdata->enetaddr[2];
Mingkai Hu90751912011-01-27 12:52:46 +0800587
588 out_be32(&regs->macstnaddr1, tempval);
589
Bin Meng9a1d6af2016-01-11 22:41:24 -0800590 tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
Mingkai Hu90751912011-01-27 12:52:46 +0800591
592 out_be32(&regs->macstnaddr2, tempval);
593
Mingkai Hu90751912011-01-27 12:52:46 +0800594 /* Clear out (for the most part) the other registers */
595 init_registers(regs);
596
597 /* Ready the device for tx/rx */
Bin Meng56a27a12016-01-11 22:41:22 -0800598 startup_tsec(priv);
Mingkai Hu90751912011-01-27 12:52:46 +0800599
Andy Fleming063c1262011-04-08 02:10:54 -0500600 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000601 ret = phy_startup(priv->phydev);
602 if (ret) {
603 printf("Could not initialize PHY %s\n",
604 priv->phydev->dev->name);
605 return ret;
606 }
Andy Fleming063c1262011-04-08 02:10:54 -0500607
608 adjust_link(priv, priv->phydev);
609
Mingkai Hu90751912011-01-27 12:52:46 +0800610 /* If there's no link, fail */
Andy Fleming063c1262011-04-08 02:10:54 -0500611 return priv->phydev->link ? 0 : -1;
Mingkai Hu90751912011-01-27 12:52:46 +0800612}
613
Andy Fleming063c1262011-04-08 02:10:54 -0500614static phy_interface_t tsec_get_interface(struct tsec_private *priv)
615{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300616 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500617 u32 ecntrl;
618
619 ecntrl = in_be32(&regs->ecntrl);
620
621 if (ecntrl & ECNTRL_SGMII_MODE)
622 return PHY_INTERFACE_MODE_SGMII;
623
624 if (ecntrl & ECNTRL_TBI_MODE) {
625 if (ecntrl & ECNTRL_REDUCED_MODE)
626 return PHY_INTERFACE_MODE_RTBI;
627 else
628 return PHY_INTERFACE_MODE_TBI;
629 }
630
631 if (ecntrl & ECNTRL_REDUCED_MODE) {
Mario Sixd38de332018-01-15 11:08:21 +0100632 phy_interface_t interface;
633
Andy Fleming063c1262011-04-08 02:10:54 -0500634 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
635 return PHY_INTERFACE_MODE_RMII;
Andy Fleming063c1262011-04-08 02:10:54 -0500636
Mario Sixd38de332018-01-15 11:08:21 +0100637 interface = priv->interface;
Andy Fleming063c1262011-04-08 02:10:54 -0500638
Mario Sixd38de332018-01-15 11:08:21 +0100639 /*
640 * This isn't autodetected, so it must
641 * be set by the platform code.
642 */
643 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
644 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
645 interface == PHY_INTERFACE_MODE_RGMII_RXID)
646 return interface;
647
648 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming063c1262011-04-08 02:10:54 -0500649 }
650
651 if (priv->flags & TSEC_GIGABIT)
652 return PHY_INTERFACE_MODE_GMII;
653
654 return PHY_INTERFACE_MODE_MII;
655}
656
Bin Meng9872b732016-01-11 22:41:18 -0800657/*
658 * Discover which PHY is attached to the device, and configure it
Mingkai Hu90751912011-01-27 12:52:46 +0800659 * properly. If the PHY is not recognized, then return 0
660 * (failure). Otherwise, return 1
661 */
Bin Meng56a27a12016-01-11 22:41:22 -0800662static int init_phy(struct tsec_private *priv)
Mingkai Hu90751912011-01-27 12:52:46 +0800663{
Andy Fleming063c1262011-04-08 02:10:54 -0500664 struct phy_device *phydev;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300665 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500666 u32 supported = (SUPPORTED_10baseT_Half |
667 SUPPORTED_10baseT_Full |
668 SUPPORTED_100baseT_Half |
669 SUPPORTED_100baseT_Full);
670
671 if (priv->flags & TSEC_GIGABIT)
672 supported |= SUPPORTED_1000baseT_Full;
Mingkai Hu90751912011-01-27 12:52:46 +0800673
674 /* Assign a Physical address to the TBI */
Bin Menga1c76c12016-01-11 22:41:25 -0800675 out_be32(&regs->tbipa, priv->tbiaddr);
Mingkai Hu90751912011-01-27 12:52:46 +0800676
Andy Fleming063c1262011-04-08 02:10:54 -0500677 priv->interface = tsec_get_interface(priv);
Mingkai Hu90751912011-01-27 12:52:46 +0800678
Andy Fleming063c1262011-04-08 02:10:54 -0500679 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
Mingkai Hu90751912011-01-27 12:52:46 +0800680 tsec_configure_serdes(priv);
681
Bin Meng56a27a12016-01-11 22:41:22 -0800682 phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
683 priv->interface);
Claudiu Manoil7f233c02013-12-10 15:21:04 +0200684 if (!phydev)
685 return 0;
Mingkai Hu90751912011-01-27 12:52:46 +0800686
Andy Fleming063c1262011-04-08 02:10:54 -0500687 phydev->supported &= supported;
688 phydev->advertising = phydev->supported;
689
690 priv->phydev = phydev;
691
692 phy_config(phydev);
Mingkai Hu90751912011-01-27 12:52:46 +0800693
694 return 1;
695}
696
Bin Meng9a1d6af2016-01-11 22:41:24 -0800697#ifndef CONFIG_DM_ETH
Bin Meng9872b732016-01-11 22:41:18 -0800698/*
699 * Initialize device structure. Returns success if PHY
Mingkai Hu90751912011-01-27 12:52:46 +0800700 * initialization succeeded (i.e. if it recognizes the PHY)
701 */
702static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
703{
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300704 struct tsec_private *priv;
Mingkai Hu90751912011-01-27 12:52:46 +0800705 struct eth_device *dev;
706 int i;
Mingkai Hu90751912011-01-27 12:52:46 +0800707
Mario Sixd38de332018-01-15 11:08:21 +0100708 dev = (struct eth_device *)malloc(sizeof(*dev));
Mingkai Hu90751912011-01-27 12:52:46 +0800709
Mario Sixd38de332018-01-15 11:08:21 +0100710 if (!dev)
Mingkai Hu90751912011-01-27 12:52:46 +0800711 return 0;
712
Mario Sixd38de332018-01-15 11:08:21 +0100713 memset(dev, 0, sizeof(*dev));
Mingkai Hu90751912011-01-27 12:52:46 +0800714
715 priv = (struct tsec_private *)malloc(sizeof(*priv));
716
Mario Six5775f002018-01-15 11:08:22 +0100717 if (!priv) {
718 free(dev);
Mingkai Hu90751912011-01-27 12:52:46 +0800719 return 0;
Mario Six5775f002018-01-15 11:08:22 +0100720 }
Mingkai Hu90751912011-01-27 12:52:46 +0800721
Mingkai Hu90751912011-01-27 12:52:46 +0800722 priv->regs = tsec_info->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800723 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
724
725 priv->phyaddr = tsec_info->phyaddr;
Bin Menga1c76c12016-01-11 22:41:25 -0800726 priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
Mingkai Hu90751912011-01-27 12:52:46 +0800727 priv->flags = tsec_info->flags;
728
Ben Whitten192bc692015-12-30 13:05:58 +0000729 strcpy(dev->name, tsec_info->devname);
Andy Fleming063c1262011-04-08 02:10:54 -0500730 priv->interface = tsec_info->interface;
731 priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
Bin Meng56a27a12016-01-11 22:41:22 -0800732 priv->dev = dev;
Mingkai Hu90751912011-01-27 12:52:46 +0800733 dev->iobase = 0;
734 dev->priv = priv;
735 dev->init = tsec_init;
736 dev->halt = tsec_halt;
737 dev->send = tsec_send;
738 dev->recv = tsec_recv;
Mingkai Hu90751912011-01-27 12:52:46 +0800739 dev->mcast = tsec_mcast_addr;
Mingkai Hu90751912011-01-27 12:52:46 +0800740
Bin Meng9872b732016-01-11 22:41:18 -0800741 /* Tell U-Boot to get the addr from the env */
Mingkai Hu90751912011-01-27 12:52:46 +0800742 for (i = 0; i < 6; i++)
743 dev->enetaddr[i] = 0;
744
745 eth_register(dev);
746
747 /* Reset the MAC */
748 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
749 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
750 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
751
Mingkai Hu90751912011-01-27 12:52:46 +0800752 /* Try to initialize PHY here, and return */
Bin Meng56a27a12016-01-11 22:41:22 -0800753 return init_phy(priv);
Mingkai Hu90751912011-01-27 12:52:46 +0800754}
755
756/*
757 * Initialize all the TSEC devices
758 *
759 * Returns the number of TSEC devices that were initialized
760 */
761int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
762{
763 int i;
Mario Sixd38de332018-01-15 11:08:21 +0100764 int count = 0;
Mingkai Hu90751912011-01-27 12:52:46 +0800765
766 for (i = 0; i < num; i++) {
Mario Sixd38de332018-01-15 11:08:21 +0100767 int ret = tsec_initialize(bis, &tsecs[i]);
768
Mingkai Hu90751912011-01-27 12:52:46 +0800769 if (ret > 0)
770 count += ret;
771 }
772
773 return count;
774}
775
776int tsec_standard_init(bd_t *bis)
777{
Andy Fleming063c1262011-04-08 02:10:54 -0500778 struct fsl_pq_mdio_info info;
779
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300780 info.regs = TSEC_GET_MDIO_REGS_BASE(1);
Andy Fleming063c1262011-04-08 02:10:54 -0500781 info.name = DEFAULT_MII_NAME;
782
783 fsl_pq_mdio_init(bis, &info);
784
Mingkai Hu90751912011-01-27 12:52:46 +0800785 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
786}
Bin Meng9a1d6af2016-01-11 22:41:24 -0800787#else /* CONFIG_DM_ETH */
788int tsec_probe(struct udevice *dev)
789{
Bin Meng9a1d6af2016-01-11 22:41:24 -0800790 struct eth_pdata *pdata = dev_get_platdata(dev);
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300791 struct tsec_private *priv = dev_get_priv(dev);
Mario Six1313aaf2018-01-15 11:08:23 +0100792 struct ofnode_phandle_args phandle_args;
Vladimir Oltean29db3102019-07-19 00:29:53 +0300793 u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300794 struct fsl_pq_mdio_info mdio_info;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800795 const char *phy_mode;
Vladimir Olteanbca686a2019-07-19 00:29:54 +0300796 fdt_addr_t reg;
Vladimir Oltean07bd39f2019-07-19 00:29:55 +0300797 ofnode parent;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800798 int ret;
799
Mario Six1313aaf2018-01-15 11:08:23 +0100800 pdata->iobase = (phys_addr_t)dev_read_addr(dev);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800801 priv->regs = (struct tsec *)pdata->iobase;
802
Mario Six1313aaf2018-01-15 11:08:23 +0100803 if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
804 &phandle_args)) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300805 printf("phy-handle does not exist under tsec %s\n", dev->name);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800806 return -ENOENT;
Mario Six1313aaf2018-01-15 11:08:23 +0100807 } else {
808 int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
809
810 priv->phyaddr = reg;
Bin Meng9a1d6af2016-01-11 22:41:24 -0800811 }
812
Mario Six1313aaf2018-01-15 11:08:23 +0100813 parent = ofnode_get_parent(phandle_args.node);
Vladimir Olteanbca686a2019-07-19 00:29:54 +0300814 if (!ofnode_valid(parent)) {
815 printf("No parent node for PHY?\n");
Bin Meng9a1d6af2016-01-11 22:41:24 -0800816 return -ENOENT;
817 }
818
Vladimir Olteanbca686a2019-07-19 00:29:54 +0300819 reg = ofnode_get_addr_index(parent, 0);
820 priv->phyregs_sgmii = (struct tsec_mii_mng *)
821 (reg + TSEC_MDIO_REGS_OFFSET);
822
Vladimir Oltean29db3102019-07-19 00:29:53 +0300823 ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
824 &phandle_args);
825 if (ret == 0)
826 ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
827
828 priv->tbiaddr = tbiaddr;
Bin Menga1c76c12016-01-11 22:41:25 -0800829
Mario Six1313aaf2018-01-15 11:08:23 +0100830 phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800831 if (phy_mode)
832 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
833 if (pdata->phy_interface == -1) {
Vladimir Olteanb7be7762019-07-19 00:29:56 +0300834 printf("Invalid PHY interface '%s'\n", phy_mode);
Bin Meng9a1d6af2016-01-11 22:41:24 -0800835 return -EINVAL;
836 }
837 priv->interface = pdata->phy_interface;
838
839 /* Initialize flags */
840 priv->flags = TSEC_GIGABIT;
841 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
842 priv->flags |= TSEC_SGMII;
843
844 mdio_info.regs = priv->phyregs_sgmii;
845 mdio_info.name = (char *)dev->name;
846 ret = fsl_pq_mdio_init(NULL, &mdio_info);
847 if (ret)
848 return ret;
849
850 /* Reset the MAC */
851 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
852 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
853 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
854
855 priv->dev = dev;
856 priv->bus = miiphy_get_dev_by_name(dev->name);
857
858 /* Try to initialize PHY here, and return */
859 return !init_phy(priv);
860}
861
862int tsec_remove(struct udevice *dev)
863{
864 struct tsec_private *priv = dev->priv;
865
866 free(priv->phydev);
867 mdio_unregister(priv->bus);
868 mdio_free(priv->bus);
869
870 return 0;
871}
872
873static const struct eth_ops tsec_ops = {
874 .start = tsec_init,
875 .send = tsec_send,
876 .recv = tsec_recv,
877 .free_pkt = tsec_free_pkt,
878 .stop = tsec_halt,
Bin Meng9a1d6af2016-01-11 22:41:24 -0800879 .mcast = tsec_mcast_addr,
Bin Meng9a1d6af2016-01-11 22:41:24 -0800880};
881
882static const struct udevice_id tsec_ids[] = {
Vladimir Oltean1c8ad082019-07-19 00:29:58 +0300883 { .compatible = "fsl,etsec2" },
Bin Meng9a1d6af2016-01-11 22:41:24 -0800884 { }
885};
886
887U_BOOT_DRIVER(eth_tsec) = {
888 .name = "tsec",
889 .id = UCLASS_ETH,
890 .of_match = tsec_ids,
891 .probe = tsec_probe,
892 .remove = tsec_remove,
893 .ops = &tsec_ops,
894 .priv_auto_alloc_size = sizeof(struct tsec_private),
895 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
896 .flags = DM_FLAG_ALLOC_PRIV_DMA,
897};
898#endif /* CONFIG_DM_ETH */