blob: 9a3b1a9874e7f0cc1f1b7e9e136a7755eb9fad06 [file] [log] [blame]
wdenk42d1f032003-10-15 23:53:47 +00001/*
wdenk97d80fc2004-06-09 00:34:46 +00002 * Freescale Three Speed Ethernet Controller driver
wdenk42d1f032003-10-15 23:53:47 +00003 *
Claudiu Manoilaec84bf2013-09-30 12:44:42 +03004 * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
wdenk42d1f032003-10-15 23:53:47 +00005 * (C) Copyright 2003, Motorola, Inc.
wdenk42d1f032003-10-15 23:53:47 +00006 * author Andy Fleming
7 *
Bin Meng9872b732016-01-11 22:41:18 -08008 * SPDX-License-Identifier: GPL-2.0+
wdenk42d1f032003-10-15 23:53:47 +00009 */
10
11#include <config.h>
wdenk42d1f032003-10-15 23:53:47 +000012#include <common.h>
13#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>
Kim Phillips0d071cd2009-08-24 14:32:26 -050018#include <asm/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
Wolfgang Denkd87080b2006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Joe Hershbergerc8a60b52012-05-21 09:46:36 +000024static int tsec_send(struct eth_device *dev, void *packet, int length);
chenhui zhaoaada81d2011-10-03 08:38:50 -050025
Andy Fleming75b9d4a2008-08-31 16:33:26 -050026/* Default initializations for TSEC controllers. */
27
28static 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 Manoilaec84bf2013-09-30 12:44:42 +030037 .regs = TSEC_GET_REGS(2, 0x2000),
Andy Fleming75b9d4a2008-08-31 16:33:26 -050038 .devname = CONFIG_MPC85XX_FEC_NAME,
39 .phyaddr = FEC_PHY_ADDR,
Andy Fleming063c1262011-04-08 02:10:54 -050040 .flags = FEC_FLAGS,
41 .mii_devname = DEFAULT_MII_NAME
Andy Fleming75b9d4a2008-08-31 16:33:26 -050042 }, /* 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};
51
Andy Fleming2abe3612008-08-31 16:33:27 -050052#define TBIANA_SETTINGS ( \
53 TBIANA_ASYMMETRIC_PAUSE \
54 | TBIANA_SYMMETRIC_PAUSE \
55 | TBIANA_FULL_DUPLEX \
56 )
57
Felix Radensky90b5bf22010-06-28 01:57:39 +030058/* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
59#ifndef CONFIG_TSEC_TBICR_SETTINGS
Kumar Gala72c96a62010-12-01 22:55:54 -060060#define CONFIG_TSEC_TBICR_SETTINGS ( \
Andy Fleming2abe3612008-08-31 16:33:27 -050061 TBICR_PHY_RESET \
Kumar Gala72c96a62010-12-01 22:55:54 -060062 | TBICR_ANEG_ENABLE \
Andy Fleming2abe3612008-08-31 16:33:27 -050063 | TBICR_FULL_DUPLEX \
64 | TBICR_SPEED1_SET \
65 )
Felix Radensky90b5bf22010-06-28 01:57:39 +030066#endif /* CONFIG_TSEC_TBICR_SETTINGS */
Peter Tyser46e91672009-11-03 17:52:07 -060067
Andy Fleming2abe3612008-08-31 16:33:27 -050068/* Configure the TBI for SGMII operation */
69static void tsec_configure_serdes(struct tsec_private *priv)
70{
Bin Meng9872b732016-01-11 22:41:18 -080071 /*
72 * Access TBI PHY registers at given TSEC register offset as opposed
73 * to the register offset used for external PHY accesses
74 */
Andy Fleming063c1262011-04-08 02:10:54 -050075 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
76 0, TBI_ANA, TBIANA_SETTINGS);
77 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
78 0, TBI_TBICON, TBICON_CLK_SELECT);
79 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
80 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
Andy Fleming2abe3612008-08-31 16:33:27 -050081}
michael.firth@bt.com55fe7c52008-01-16 11:40:51 +000082
David Updegraff53a5c422007-06-11 10:41:07 -050083#ifdef CONFIG_MCAST_TFTP
84
85/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
86
87/* Set the appropriate hash bit for the given addr */
88
Bin Meng9872b732016-01-11 22:41:18 -080089/*
90 * The algorithm works like so:
David Updegraff53a5c422007-06-11 10:41:07 -050091 * 1) Take the Destination Address (ie the multicast address), and
92 * do a CRC on it (little endian), and reverse the bits of the
93 * result.
94 * 2) Use the 8 most significant bits as a hash into a 256-entry
95 * table. The table is controlled through 8 32-bit registers:
Claudiu Manoil876d4512013-09-30 12:44:40 +030096 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is entry
97 * 255. This means that the 3 most significant bits in the
David Updegraff53a5c422007-06-11 10:41:07 -050098 * hash index which gaddr register to use, and the 5 other bits
99 * indicate which bit (assuming an IBM numbering scheme, which
Claudiu Manoil876d4512013-09-30 12:44:40 +0300100 * for PowerPC (tm) is usually the case) in the register holds
Bin Meng9872b732016-01-11 22:41:18 -0800101 * the entry.
102 */
103static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac, u8 set)
David Updegraff53a5c422007-06-11 10:41:07 -0500104{
Claudiu Manoilb2002042013-09-30 12:44:41 +0300105 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoil876d4512013-09-30 12:44:40 +0300106 struct tsec __iomem *regs = priv->regs;
107 u32 result, value;
108 u8 whichbit, whichreg;
David Updegraff53a5c422007-06-11 10:41:07 -0500109
Claudiu Manoil876d4512013-09-30 12:44:40 +0300110 result = ether_crc(MAC_ADDR_LEN, mcast_mac);
111 whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
112 whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
David Updegraff53a5c422007-06-11 10:41:07 -0500113
Claudiu Manoil876d4512013-09-30 12:44:40 +0300114 value = 1 << (31-whichbit);
David Updegraff53a5c422007-06-11 10:41:07 -0500115
Claudiu Manoil876d4512013-09-30 12:44:40 +0300116 if (set)
117 setbits_be32(&regs->hash.gaddr0 + whichreg, value);
118 else
119 clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
120
David Updegraff53a5c422007-06-11 10:41:07 -0500121 return 0;
122}
123#endif /* Multicast TFTP ? */
Mingkai Hu90751912011-01-27 12:52:46 +0800124
Bin Meng9872b732016-01-11 22:41:18 -0800125/*
126 * Initialized required registers to appropriate values, zeroing
Mingkai Hu90751912011-01-27 12:52:46 +0800127 * those we don't care about (unless zero is bad, in which case,
128 * choose a more appropriate value)
129 */
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300130static void init_registers(struct tsec __iomem *regs)
Mingkai Hu90751912011-01-27 12:52:46 +0800131{
132 /* Clear IEVENT */
133 out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
134
135 out_be32(&regs->imask, IMASK_INIT_CLEAR);
136
137 out_be32(&regs->hash.iaddr0, 0);
138 out_be32(&regs->hash.iaddr1, 0);
139 out_be32(&regs->hash.iaddr2, 0);
140 out_be32(&regs->hash.iaddr3, 0);
141 out_be32(&regs->hash.iaddr4, 0);
142 out_be32(&regs->hash.iaddr5, 0);
143 out_be32(&regs->hash.iaddr6, 0);
144 out_be32(&regs->hash.iaddr7, 0);
145
146 out_be32(&regs->hash.gaddr0, 0);
147 out_be32(&regs->hash.gaddr1, 0);
148 out_be32(&regs->hash.gaddr2, 0);
149 out_be32(&regs->hash.gaddr3, 0);
150 out_be32(&regs->hash.gaddr4, 0);
151 out_be32(&regs->hash.gaddr5, 0);
152 out_be32(&regs->hash.gaddr6, 0);
153 out_be32(&regs->hash.gaddr7, 0);
154
155 out_be32(&regs->rctrl, 0x00000000);
156
157 /* Init RMON mib registers */
Claudiu Manoil82ef75c2013-09-30 12:44:46 +0300158 memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
Mingkai Hu90751912011-01-27 12:52:46 +0800159
160 out_be32(&regs->rmon.cam1, 0xffffffff);
161 out_be32(&regs->rmon.cam2, 0xffffffff);
162
163 out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
164
165 out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
166
167 out_be32(&regs->attr, ATTR_INIT_SETTINGS);
168 out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
169
170}
171
Bin Meng9872b732016-01-11 22:41:18 -0800172/*
173 * Configure maccfg2 based on negotiated speed and duplex
Mingkai Hu90751912011-01-27 12:52:46 +0800174 * reported by PHY handling code
175 */
Andy Fleming063c1262011-04-08 02:10:54 -0500176static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
Mingkai Hu90751912011-01-27 12:52:46 +0800177{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300178 struct tsec __iomem *regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800179 u32 ecntrl, maccfg2;
180
Andy Fleming063c1262011-04-08 02:10:54 -0500181 if (!phydev->link) {
182 printf("%s: No link.\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800183 return;
184 }
185
186 /* clear all bits relative with interface mode */
187 ecntrl = in_be32(&regs->ecntrl);
188 ecntrl &= ~ECNTRL_R100;
189
190 maccfg2 = in_be32(&regs->maccfg2);
191 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
192
Andy Fleming063c1262011-04-08 02:10:54 -0500193 if (phydev->duplex)
Mingkai Hu90751912011-01-27 12:52:46 +0800194 maccfg2 |= MACCFG2_FULL_DUPLEX;
195
Andy Fleming063c1262011-04-08 02:10:54 -0500196 switch (phydev->speed) {
Mingkai Hu90751912011-01-27 12:52:46 +0800197 case 1000:
198 maccfg2 |= MACCFG2_GMII;
199 break;
200 case 100:
201 case 10:
202 maccfg2 |= MACCFG2_MII;
203
Bin Meng9872b732016-01-11 22:41:18 -0800204 /*
205 * Set R100 bit in all modes although
Mingkai Hu90751912011-01-27 12:52:46 +0800206 * it is only used in RGMII mode
207 */
Andy Fleming063c1262011-04-08 02:10:54 -0500208 if (phydev->speed == 100)
Mingkai Hu90751912011-01-27 12:52:46 +0800209 ecntrl |= ECNTRL_R100;
210 break;
211 default:
Andy Fleming063c1262011-04-08 02:10:54 -0500212 printf("%s: Speed was bad\n", phydev->dev->name);
Mingkai Hu90751912011-01-27 12:52:46 +0800213 break;
214 }
215
216 out_be32(&regs->ecntrl, ecntrl);
217 out_be32(&regs->maccfg2, maccfg2);
218
Andy Fleming063c1262011-04-08 02:10:54 -0500219 printf("Speed: %d, %s duplex%s\n", phydev->speed,
220 (phydev->duplex) ? "full" : "half",
221 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Mingkai Hu90751912011-01-27 12:52:46 +0800222}
223
chenhui zhaoaada81d2011-10-03 08:38:50 -0500224#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
225/*
226 * When MACCFG1[Rx_EN] is enabled during system boot as part
227 * of the eTSEC port initialization sequence,
228 * the eTSEC Rx logic may not be properly initialized.
229 */
230void redundant_init(struct eth_device *dev)
231{
232 struct tsec_private *priv = dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300233 struct tsec __iomem *regs = priv->regs;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500234 uint t, count = 0;
235 int fail = 1;
236 static const u8 pkt[] = {
237 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
238 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
239 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
240 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
241 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
242 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
243 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
244 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
245 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
246 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
247 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
248 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
249 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
250 0x71, 0x72};
251
252 /* Enable promiscuous mode */
253 setbits_be32(&regs->rctrl, 0x8);
254 /* Enable loopback mode */
255 setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
256 /* Enable transmit and receive */
257 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
258
259 /* Tell the DMA it is clear to go */
260 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
261 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
262 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
263 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
264
265 do {
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300266 uint16_t status;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500267 tsec_send(dev, (void *)pkt, sizeof(pkt));
268
269 /* Wait for buffer to be received */
Bin Menge677da92016-01-11 22:41:20 -0800270 for (t = 0;
271 in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800272 t++) {
chenhui zhaoaada81d2011-10-03 08:38:50 -0500273 if (t >= 10 * TOUT_LOOP) {
274 printf("%s: tsec: rx error\n", dev->name);
275 break;
276 }
277 }
278
Bin Meng362b1232016-01-11 22:41:19 -0800279 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
chenhui zhaoaada81d2011-10-03 08:38:50 -0500280 fail = 0;
281
Bin Menge677da92016-01-11 22:41:20 -0800282 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300283 status = RXBD_EMPTY;
Bin Meng362b1232016-01-11 22:41:19 -0800284 if ((priv->rx_idx + 1) == PKTBUFSRX)
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300285 status |= RXBD_WRAP;
Bin Menge677da92016-01-11 22:41:20 -0800286 out_be16(&priv->rxbd[priv->rx_idx].status, status);
Bin Meng362b1232016-01-11 22:41:19 -0800287 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500288
289 if (in_be32(&regs->ievent) & IEVENT_BSY) {
290 out_be32(&regs->ievent, IEVENT_BSY);
291 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
292 }
293 if (fail) {
294 printf("loopback recv packet error!\n");
295 clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
296 udelay(1000);
297 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
298 }
299 } while ((count++ < 4) && (fail == 1));
300
301 if (fail)
302 panic("eTSEC init fail!\n");
303 /* Disable promiscuous mode */
304 clrbits_be32(&regs->rctrl, 0x8);
305 /* Disable loopback mode */
306 clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
307}
308#endif
309
Bin Meng9872b732016-01-11 22:41:18 -0800310/*
311 * Set up the buffers and their descriptors, and bring up the
Mingkai Hu90751912011-01-27 12:52:46 +0800312 * interface
313 */
314static void startup_tsec(struct eth_device *dev)
315{
Mingkai Hu90751912011-01-27 12:52:46 +0800316 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300317 struct tsec __iomem *regs = priv->regs;
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300318 uint16_t status;
319 int i;
Mingkai Hu90751912011-01-27 12:52:46 +0800320
Andy Fleming063c1262011-04-08 02:10:54 -0500321 /* reset the indices to zero */
Bin Meng362b1232016-01-11 22:41:19 -0800322 priv->rx_idx = 0;
323 priv->tx_idx = 0;
chenhui zhaoaada81d2011-10-03 08:38:50 -0500324#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
325 uint svr;
326#endif
Andy Fleming063c1262011-04-08 02:10:54 -0500327
Mingkai Hu90751912011-01-27 12:52:46 +0800328 /* Point to the buffer descriptors */
Bin Menge677da92016-01-11 22:41:20 -0800329 out_be32(&regs->tbase, (u32)&priv->txbd[0]);
330 out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
Mingkai Hu90751912011-01-27 12:52:46 +0800331
332 /* Initialize the Rx Buffer descriptors */
333 for (i = 0; i < PKTBUFSRX; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800334 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
335 out_be16(&priv->rxbd[i].length, 0);
336 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
Mingkai Hu90751912011-01-27 12:52:46 +0800337 }
Bin Menge677da92016-01-11 22:41:20 -0800338 status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
339 out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800340
341 /* Initialize the TX Buffer Descriptors */
342 for (i = 0; i < TX_BUF_CNT; i++) {
Bin Menge677da92016-01-11 22:41:20 -0800343 out_be16(&priv->txbd[i].status, 0);
344 out_be16(&priv->txbd[i].length, 0);
345 out_be32(&priv->txbd[i].bufptr, 0);
Mingkai Hu90751912011-01-27 12:52:46 +0800346 }
Bin Menge677da92016-01-11 22:41:20 -0800347 status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
348 out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
Mingkai Hu90751912011-01-27 12:52:46 +0800349
chenhui zhaoaada81d2011-10-03 08:38:50 -0500350#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
351 svr = get_svr();
352 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
353 redundant_init(dev);
354#endif
Mingkai Hu90751912011-01-27 12:52:46 +0800355 /* Enable Transmit and Receive */
356 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
357
358 /* Tell the DMA it is clear to go */
359 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
360 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
361 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
362 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
363}
364
Bin Meng9872b732016-01-11 22:41:18 -0800365/*
366 * This returns the status bits of the device. The return value
Mingkai Hu90751912011-01-27 12:52:46 +0800367 * is never checked, and this is what the 8260 driver did, so we
Bin Meng9872b732016-01-11 22:41:18 -0800368 * do the same. Presumably, this would be zero if there were no
Mingkai Hu90751912011-01-27 12:52:46 +0800369 * errors
370 */
Joe Hershbergerc8a60b52012-05-21 09:46:36 +0000371static int tsec_send(struct eth_device *dev, void *packet, int length)
Mingkai Hu90751912011-01-27 12:52:46 +0800372{
Mingkai Hu90751912011-01-27 12:52:46 +0800373 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300374 struct tsec __iomem *regs = priv->regs;
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300375 uint16_t status;
376 int result = 0;
377 int i;
Mingkai Hu90751912011-01-27 12:52:46 +0800378
379 /* Find an empty buffer descriptor */
Bin Menge677da92016-01-11 22:41:20 -0800380 for (i = 0;
381 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
382 i++) {
Mingkai Hu90751912011-01-27 12:52:46 +0800383 if (i >= TOUT_LOOP) {
384 debug("%s: tsec: tx buffers full\n", dev->name);
385 return result;
386 }
387 }
388
Bin Menge677da92016-01-11 22:41:20 -0800389 out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
390 out_be16(&priv->txbd[priv->tx_idx].length, length);
391 status = in_be16(&priv->txbd[priv->tx_idx].status);
392 out_be16(&priv->txbd[priv->tx_idx].status, status |
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300393 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
Mingkai Hu90751912011-01-27 12:52:46 +0800394
395 /* Tell the DMA to go */
396 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
397
398 /* Wait for buffer to be transmitted */
Bin Menge677da92016-01-11 22:41:20 -0800399 for (i = 0;
400 in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
401 i++) {
Mingkai Hu90751912011-01-27 12:52:46 +0800402 if (i >= TOUT_LOOP) {
403 debug("%s: tsec: tx error\n", dev->name);
404 return result;
405 }
406 }
407
Bin Meng362b1232016-01-11 22:41:19 -0800408 priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
Bin Menge677da92016-01-11 22:41:20 -0800409 result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
Mingkai Hu90751912011-01-27 12:52:46 +0800410
411 return result;
412}
413
414static int tsec_recv(struct eth_device *dev)
415{
Mingkai Hu90751912011-01-27 12:52:46 +0800416 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300417 struct tsec __iomem *regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800418
Bin Menge677da92016-01-11 22:41:20 -0800419 while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
420 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
421 uint16_t status = in_be16(&priv->rxbd[priv->rx_idx].status);
Bin Meng362b1232016-01-11 22:41:19 -0800422 uchar *packet = net_rx_packets[priv->rx_idx];
Mingkai Hu90751912011-01-27 12:52:46 +0800423
424 /* Send the packet up if there were no errors */
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300425 if (!(status & RXBD_STATS))
Bin Meng362b1232016-01-11 22:41:19 -0800426 net_process_received_packet(packet, length - 4);
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300427 else
428 printf("Got error %x\n", (status & RXBD_STATS));
Mingkai Hu90751912011-01-27 12:52:46 +0800429
Bin Menge677da92016-01-11 22:41:20 -0800430 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
Mingkai Hu90751912011-01-27 12:52:46 +0800431
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300432 status = RXBD_EMPTY;
Mingkai Hu90751912011-01-27 12:52:46 +0800433 /* Set the wrap bit if this is the last element in the list */
Bin Meng362b1232016-01-11 22:41:19 -0800434 if ((priv->rx_idx + 1) == PKTBUFSRX)
Claudiu Manoil9c9141f2013-10-04 19:13:53 +0300435 status |= RXBD_WRAP;
Bin Menge677da92016-01-11 22:41:20 -0800436 out_be16(&priv->rxbd[priv->rx_idx].status, status);
Mingkai Hu90751912011-01-27 12:52:46 +0800437
Bin Meng362b1232016-01-11 22:41:19 -0800438 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
Mingkai Hu90751912011-01-27 12:52:46 +0800439 }
440
441 if (in_be32(&regs->ievent) & IEVENT_BSY) {
442 out_be32(&regs->ievent, IEVENT_BSY);
443 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
444 }
445
446 return -1;
Mingkai Hu90751912011-01-27 12:52:46 +0800447}
448
449/* Stop the interface */
450static void tsec_halt(struct eth_device *dev)
451{
452 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300453 struct tsec __iomem *regs = priv->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800454
455 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
456 setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
457
458 while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
459 != (IEVENT_GRSC | IEVENT_GTSC))
460 ;
461
462 clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
463
464 /* Shut down the PHY, as needed */
Andy Fleming063c1262011-04-08 02:10:54 -0500465 phy_shutdown(priv->phydev);
Mingkai Hu90751912011-01-27 12:52:46 +0800466}
467
Bin Meng9872b732016-01-11 22:41:18 -0800468/*
469 * Initializes data structures and registers for the controller,
470 * and brings the interface up. Returns the link status, meaning
Mingkai Hu90751912011-01-27 12:52:46 +0800471 * that it returns success if the link is up, failure otherwise.
Bin Meng9872b732016-01-11 22:41:18 -0800472 * This allows U-Boot to find the first active controller.
Mingkai Hu90751912011-01-27 12:52:46 +0800473 */
474static int tsec_init(struct eth_device *dev, bd_t * bd)
475{
Mingkai Hu90751912011-01-27 12:52:46 +0800476 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300477 struct tsec __iomem *regs = priv->regs;
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300478 u32 tempval;
Timur Tabi11af8d62012-07-09 08:52:43 +0000479 int ret;
Mingkai Hu90751912011-01-27 12:52:46 +0800480
481 /* Make sure the controller is stopped */
482 tsec_halt(dev);
483
484 /* Init MACCFG2. Defaults to GMII */
485 out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
486
487 /* Init ECNTRL */
488 out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
489
Bin Meng9872b732016-01-11 22:41:18 -0800490 /*
491 * Copy the station address into the address registers.
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300492 * For a station address of 0x12345678ABCD in transmission
493 * order (BE), MACnADDR1 is set to 0xCDAB7856 and
494 * MACnADDR2 is set to 0x34120000.
495 */
496 tempval = (dev->enetaddr[5] << 24) | (dev->enetaddr[4] << 16) |
497 (dev->enetaddr[3] << 8) | dev->enetaddr[2];
Mingkai Hu90751912011-01-27 12:52:46 +0800498
499 out_be32(&regs->macstnaddr1, tempval);
500
Claudiu Manoilb1690bc2013-09-30 12:44:47 +0300501 tempval = (dev->enetaddr[1] << 24) | (dev->enetaddr[0] << 16);
Mingkai Hu90751912011-01-27 12:52:46 +0800502
503 out_be32(&regs->macstnaddr2, tempval);
504
Mingkai Hu90751912011-01-27 12:52:46 +0800505 /* Clear out (for the most part) the other registers */
506 init_registers(regs);
507
508 /* Ready the device for tx/rx */
509 startup_tsec(dev);
510
Andy Fleming063c1262011-04-08 02:10:54 -0500511 /* Start up the PHY */
Timur Tabi11af8d62012-07-09 08:52:43 +0000512 ret = phy_startup(priv->phydev);
513 if (ret) {
514 printf("Could not initialize PHY %s\n",
515 priv->phydev->dev->name);
516 return ret;
517 }
Andy Fleming063c1262011-04-08 02:10:54 -0500518
519 adjust_link(priv, priv->phydev);
520
Mingkai Hu90751912011-01-27 12:52:46 +0800521 /* If there's no link, fail */
Andy Fleming063c1262011-04-08 02:10:54 -0500522 return priv->phydev->link ? 0 : -1;
Mingkai Hu90751912011-01-27 12:52:46 +0800523}
524
Andy Fleming063c1262011-04-08 02:10:54 -0500525static phy_interface_t tsec_get_interface(struct tsec_private *priv)
526{
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300527 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500528 u32 ecntrl;
529
530 ecntrl = in_be32(&regs->ecntrl);
531
532 if (ecntrl & ECNTRL_SGMII_MODE)
533 return PHY_INTERFACE_MODE_SGMII;
534
535 if (ecntrl & ECNTRL_TBI_MODE) {
536 if (ecntrl & ECNTRL_REDUCED_MODE)
537 return PHY_INTERFACE_MODE_RTBI;
538 else
539 return PHY_INTERFACE_MODE_TBI;
540 }
541
542 if (ecntrl & ECNTRL_REDUCED_MODE) {
543 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
544 return PHY_INTERFACE_MODE_RMII;
545 else {
546 phy_interface_t interface = priv->interface;
547
548 /*
549 * This isn't autodetected, so it must
550 * be set by the platform code.
551 */
552 if ((interface == PHY_INTERFACE_MODE_RGMII_ID) ||
Bin Meng9872b732016-01-11 22:41:18 -0800553 (interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
554 (interface == PHY_INTERFACE_MODE_RGMII_RXID))
Andy Fleming063c1262011-04-08 02:10:54 -0500555 return interface;
556
557 return PHY_INTERFACE_MODE_RGMII;
558 }
559 }
560
561 if (priv->flags & TSEC_GIGABIT)
562 return PHY_INTERFACE_MODE_GMII;
563
564 return PHY_INTERFACE_MODE_MII;
565}
566
Bin Meng9872b732016-01-11 22:41:18 -0800567/*
568 * Discover which PHY is attached to the device, and configure it
Mingkai Hu90751912011-01-27 12:52:46 +0800569 * properly. If the PHY is not recognized, then return 0
570 * (failure). Otherwise, return 1
571 */
572static int init_phy(struct eth_device *dev)
573{
574 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Andy Fleming063c1262011-04-08 02:10:54 -0500575 struct phy_device *phydev;
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300576 struct tsec __iomem *regs = priv->regs;
Andy Fleming063c1262011-04-08 02:10:54 -0500577 u32 supported = (SUPPORTED_10baseT_Half |
578 SUPPORTED_10baseT_Full |
579 SUPPORTED_100baseT_Half |
580 SUPPORTED_100baseT_Full);
581
582 if (priv->flags & TSEC_GIGABIT)
583 supported |= SUPPORTED_1000baseT_Full;
Mingkai Hu90751912011-01-27 12:52:46 +0800584
585 /* Assign a Physical address to the TBI */
586 out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
587
Andy Fleming063c1262011-04-08 02:10:54 -0500588 priv->interface = tsec_get_interface(priv);
Mingkai Hu90751912011-01-27 12:52:46 +0800589
Andy Fleming063c1262011-04-08 02:10:54 -0500590 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
Mingkai Hu90751912011-01-27 12:52:46 +0800591 tsec_configure_serdes(priv);
592
Andy Fleming063c1262011-04-08 02:10:54 -0500593 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Claudiu Manoil7f233c02013-12-10 15:21:04 +0200594 if (!phydev)
595 return 0;
Mingkai Hu90751912011-01-27 12:52:46 +0800596
Andy Fleming063c1262011-04-08 02:10:54 -0500597 phydev->supported &= supported;
598 phydev->advertising = phydev->supported;
599
600 priv->phydev = phydev;
601
602 phy_config(phydev);
Mingkai Hu90751912011-01-27 12:52:46 +0800603
604 return 1;
605}
606
Bin Meng9872b732016-01-11 22:41:18 -0800607/*
608 * Initialize device structure. Returns success if PHY
Mingkai Hu90751912011-01-27 12:52:46 +0800609 * initialization succeeded (i.e. if it recognizes the PHY)
610 */
611static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
612{
613 struct eth_device *dev;
614 int i;
615 struct tsec_private *priv;
616
617 dev = (struct eth_device *)malloc(sizeof *dev);
618
619 if (NULL == dev)
620 return 0;
621
622 memset(dev, 0, sizeof *dev);
623
624 priv = (struct tsec_private *)malloc(sizeof(*priv));
625
626 if (NULL == priv)
627 return 0;
628
Mingkai Hu90751912011-01-27 12:52:46 +0800629 priv->regs = tsec_info->regs;
Mingkai Hu90751912011-01-27 12:52:46 +0800630 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
631
632 priv->phyaddr = tsec_info->phyaddr;
633 priv->flags = tsec_info->flags;
634
635 sprintf(dev->name, tsec_info->devname);
Andy Fleming063c1262011-04-08 02:10:54 -0500636 priv->interface = tsec_info->interface;
637 priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
Mingkai Hu90751912011-01-27 12:52:46 +0800638 dev->iobase = 0;
639 dev->priv = priv;
640 dev->init = tsec_init;
641 dev->halt = tsec_halt;
642 dev->send = tsec_send;
643 dev->recv = tsec_recv;
644#ifdef CONFIG_MCAST_TFTP
645 dev->mcast = tsec_mcast_addr;
646#endif
647
Bin Meng9872b732016-01-11 22:41:18 -0800648 /* Tell U-Boot to get the addr from the env */
Mingkai Hu90751912011-01-27 12:52:46 +0800649 for (i = 0; i < 6; i++)
650 dev->enetaddr[i] = 0;
651
652 eth_register(dev);
653
654 /* Reset the MAC */
655 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
656 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
657 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
658
Mingkai Hu90751912011-01-27 12:52:46 +0800659 /* Try to initialize PHY here, and return */
660 return init_phy(dev);
661}
662
663/*
664 * Initialize all the TSEC devices
665 *
666 * Returns the number of TSEC devices that were initialized
667 */
668int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
669{
670 int i;
671 int ret, count = 0;
672
673 for (i = 0; i < num; i++) {
674 ret = tsec_initialize(bis, &tsecs[i]);
675 if (ret > 0)
676 count += ret;
677 }
678
679 return count;
680}
681
682int tsec_standard_init(bd_t *bis)
683{
Andy Fleming063c1262011-04-08 02:10:54 -0500684 struct fsl_pq_mdio_info info;
685
Claudiu Manoilaec84bf2013-09-30 12:44:42 +0300686 info.regs = TSEC_GET_MDIO_REGS_BASE(1);
Andy Fleming063c1262011-04-08 02:10:54 -0500687 info.name = DEFAULT_MII_NAME;
688
689 fsl_pq_mdio_init(bis, &info);
690
Mingkai Hu90751912011-01-27 12:52:46 +0800691 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
692}