blob: 3c1c8f0799c4177a1a4371e7cda27855510e61ec [file] [log] [blame]
wdenk9c53f402003-10-15 23:53:47 +00001/*
wdenka445ddf2004-06-09 00:34:46 +00002 * Freescale Three Speed Ethernet Controller driver
wdenk9c53f402003-10-15 23:53:47 +00003 *
4 * This software may be used and distributed according to the
5 * terms of the GNU Public License, Version 2, incorporated
6 * herein by reference.
7 *
Mingkai Hua65e6102011-01-27 12:52:45 +08008 * Copyright 2004-2011 Freescale Semiconductor, Inc.
wdenk9c53f402003-10-15 23:53:47 +00009 * (C) Copyright 2003, Motorola, Inc.
wdenk9c53f402003-10-15 23:53:47 +000010 * author Andy Fleming
11 *
12 */
13
14#include <config.h>
wdenk9c53f402003-10-15 23:53:47 +000015#include <common.h>
16#include <malloc.h>
17#include <net.h>
18#include <command.h>
Andy Flemingc067fc12008-08-31 16:33:25 -050019#include <tsec.h>
Andy Fleming422effd2011-04-08 02:10:54 -050020#include <fsl_mdio.h>
Kim Phillipsae4dd972009-08-24 14:32:26 -050021#include <asm/errno.h>
chenhui zhaoc8caa8a2011-10-03 08:38:50 -050022#include <asm/processor.h>
wdenk9c53f402003-10-15 23:53:47 +000023
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Marian Balakowiczaab8c492005-10-28 22:30:33 +020026#define TX_BUF_CNT 2
wdenk9c53f402003-10-15 23:53:47 +000027
Jon Loeligerb7ced082006-10-10 17:03:43 -050028static uint rxIdx; /* index of the current RX buffer */
29static uint txIdx; /* index of the current TX buffer */
wdenk9c53f402003-10-15 23:53:47 +000030
31typedef volatile struct rtxbd {
32 txbd8_t txbd[TX_BUF_CNT];
33 rxbd8_t rxbd[PKTBUFSRX];
Jon Loeligerb7ced082006-10-10 17:03:43 -050034} RTXBD;
wdenk9c53f402003-10-15 23:53:47 +000035
Andy Flemingfecff2b2008-08-31 16:33:26 -050036#define MAXCONTROLLERS (8)
wdenka445ddf2004-06-09 00:34:46 +000037
wdenka445ddf2004-06-09 00:34:46 +000038static struct tsec_private *privlist[MAXCONTROLLERS];
Andy Flemingfecff2b2008-08-31 16:33:26 -050039static int num_tsecs = 0;
wdenka445ddf2004-06-09 00:34:46 +000040
wdenk9c53f402003-10-15 23:53:47 +000041#ifdef __GNUC__
42static RTXBD rtx __attribute__ ((aligned(8)));
43#else
44#error "rtx must be 64-bit aligned"
45#endif
46
Joe Hershberger5d289fe2012-05-21 09:46:36 +000047static int tsec_send(struct eth_device *dev, void *packet, int length);
chenhui zhaoc8caa8a2011-10-03 08:38:50 -050048
Andy Flemingfecff2b2008-08-31 16:33:26 -050049/* Default initializations for TSEC controllers. */
50
51static struct tsec_info_struct tsec_info[] = {
52#ifdef CONFIG_TSEC1
53 STD_TSEC_INFO(1), /* TSEC1 */
54#endif
55#ifdef CONFIG_TSEC2
56 STD_TSEC_INFO(2), /* TSEC2 */
57#endif
58#ifdef CONFIG_MPC85XX_FEC
59 {
60 .regs = (tsec_t *)(TSEC_BASE_ADDR + 0x2000),
Andy Flemingfecff2b2008-08-31 16:33:26 -050061 .devname = CONFIG_MPC85XX_FEC_NAME,
62 .phyaddr = FEC_PHY_ADDR,
Andy Fleming422effd2011-04-08 02:10:54 -050063 .flags = FEC_FLAGS,
64 .mii_devname = DEFAULT_MII_NAME
Andy Flemingfecff2b2008-08-31 16:33:26 -050065 }, /* FEC */
66#endif
67#ifdef CONFIG_TSEC3
68 STD_TSEC_INFO(3), /* TSEC3 */
69#endif
70#ifdef CONFIG_TSEC4
71 STD_TSEC_INFO(4), /* TSEC4 */
72#endif
73};
74
Andy Flemingac65e072008-08-31 16:33:27 -050075#define TBIANA_SETTINGS ( \
76 TBIANA_ASYMMETRIC_PAUSE \
77 | TBIANA_SYMMETRIC_PAUSE \
78 | TBIANA_FULL_DUPLEX \
79 )
80
Felix Radensky27f98e02010-06-28 01:57:39 +030081/* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
82#ifndef CONFIG_TSEC_TBICR_SETTINGS
Kumar Galac1457f92010-12-01 22:55:54 -060083#define CONFIG_TSEC_TBICR_SETTINGS ( \
Andy Flemingac65e072008-08-31 16:33:27 -050084 TBICR_PHY_RESET \
Kumar Galac1457f92010-12-01 22:55:54 -060085 | TBICR_ANEG_ENABLE \
Andy Flemingac65e072008-08-31 16:33:27 -050086 | TBICR_FULL_DUPLEX \
87 | TBICR_SPEED1_SET \
88 )
Felix Radensky27f98e02010-06-28 01:57:39 +030089#endif /* CONFIG_TSEC_TBICR_SETTINGS */
Peter Tyser583c1f42009-11-03 17:52:07 -060090
Andy Flemingac65e072008-08-31 16:33:27 -050091/* Configure the TBI for SGMII operation */
92static void tsec_configure_serdes(struct tsec_private *priv)
93{
Peter Tyser4ef03c02009-11-09 13:09:46 -060094 /* Access TBI PHY registers at given TSEC register offset as opposed
95 * to the register offset used for external PHY accesses */
Andy Fleming422effd2011-04-08 02:10:54 -050096 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
97 0, TBI_ANA, TBIANA_SETTINGS);
98 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
99 0, TBI_TBICON, TBICON_CLK_SELECT);
100 tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
101 0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
David Updegraff0451b012007-04-20 14:34:48 -0500102}
103
Mingkai Hue0653bf2011-01-27 12:52:46 +0800104#ifdef CONFIG_MCAST_TFTP
105
106/* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
107
108/* Set the appropriate hash bit for the given addr */
109
110/* The algorithm works like so:
111 * 1) Take the Destination Address (ie the multicast address), and
112 * do a CRC on it (little endian), and reverse the bits of the
113 * result.
114 * 2) Use the 8 most significant bits as a hash into a 256-entry
115 * table. The table is controlled through 8 32-bit registers:
116 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
117 * gaddr7. This means that the 3 most significant bits in the
118 * hash index which gaddr register to use, and the 5 other bits
119 * indicate which bit (assuming an IBM numbering scheme, which
120 * for PowerPC (tm) is usually the case) in the tregister holds
121 * the entry. */
122static int
123tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
124{
125 struct tsec_private *priv = privlist[1];
126 volatile tsec_t *regs = priv->regs;
127 volatile u32 *reg_array, value;
128 u8 result, whichbit, whichreg;
129
130 result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
131 whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
132 whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
133 value = (1 << (31-whichbit));
134
135 reg_array = &(regs->hash.gaddr0);
136
137 if (set) {
138 reg_array[whichreg] |= value;
139 } else {
140 reg_array[whichreg] &= ~value;
141 }
142 return 0;
143}
144#endif /* Multicast TFTP ? */
145
146/* Initialized required registers to appropriate values, zeroing
147 * those we don't care about (unless zero is bad, in which case,
148 * choose a more appropriate value)
149 */
150static void init_registers(tsec_t *regs)
151{
152 /* Clear IEVENT */
153 out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
154
155 out_be32(&regs->imask, IMASK_INIT_CLEAR);
156
157 out_be32(&regs->hash.iaddr0, 0);
158 out_be32(&regs->hash.iaddr1, 0);
159 out_be32(&regs->hash.iaddr2, 0);
160 out_be32(&regs->hash.iaddr3, 0);
161 out_be32(&regs->hash.iaddr4, 0);
162 out_be32(&regs->hash.iaddr5, 0);
163 out_be32(&regs->hash.iaddr6, 0);
164 out_be32(&regs->hash.iaddr7, 0);
165
166 out_be32(&regs->hash.gaddr0, 0);
167 out_be32(&regs->hash.gaddr1, 0);
168 out_be32(&regs->hash.gaddr2, 0);
169 out_be32(&regs->hash.gaddr3, 0);
170 out_be32(&regs->hash.gaddr4, 0);
171 out_be32(&regs->hash.gaddr5, 0);
172 out_be32(&regs->hash.gaddr6, 0);
173 out_be32(&regs->hash.gaddr7, 0);
174
175 out_be32(&regs->rctrl, 0x00000000);
176
177 /* Init RMON mib registers */
178 memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
179
180 out_be32(&regs->rmon.cam1, 0xffffffff);
181 out_be32(&regs->rmon.cam2, 0xffffffff);
182
183 out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
184
185 out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
186
187 out_be32(&regs->attr, ATTR_INIT_SETTINGS);
188 out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
189
190}
191
192/* Configure maccfg2 based on negotiated speed and duplex
193 * reported by PHY handling code
194 */
Andy Fleming422effd2011-04-08 02:10:54 -0500195static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
Mingkai Hue0653bf2011-01-27 12:52:46 +0800196{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800197 tsec_t *regs = priv->regs;
198 u32 ecntrl, maccfg2;
199
Andy Fleming422effd2011-04-08 02:10:54 -0500200 if (!phydev->link) {
201 printf("%s: No link.\n", phydev->dev->name);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800202 return;
203 }
204
205 /* clear all bits relative with interface mode */
206 ecntrl = in_be32(&regs->ecntrl);
207 ecntrl &= ~ECNTRL_R100;
208
209 maccfg2 = in_be32(&regs->maccfg2);
210 maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
211
Andy Fleming422effd2011-04-08 02:10:54 -0500212 if (phydev->duplex)
Mingkai Hue0653bf2011-01-27 12:52:46 +0800213 maccfg2 |= MACCFG2_FULL_DUPLEX;
214
Andy Fleming422effd2011-04-08 02:10:54 -0500215 switch (phydev->speed) {
Mingkai Hue0653bf2011-01-27 12:52:46 +0800216 case 1000:
217 maccfg2 |= MACCFG2_GMII;
218 break;
219 case 100:
220 case 10:
221 maccfg2 |= MACCFG2_MII;
222
223 /* Set R100 bit in all modes although
224 * it is only used in RGMII mode
225 */
Andy Fleming422effd2011-04-08 02:10:54 -0500226 if (phydev->speed == 100)
Mingkai Hue0653bf2011-01-27 12:52:46 +0800227 ecntrl |= ECNTRL_R100;
228 break;
229 default:
Andy Fleming422effd2011-04-08 02:10:54 -0500230 printf("%s: Speed was bad\n", phydev->dev->name);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800231 break;
232 }
233
234 out_be32(&regs->ecntrl, ecntrl);
235 out_be32(&regs->maccfg2, maccfg2);
wdenkf41ff3b2005-04-04 23:43:44 +0000236
Andy Fleming422effd2011-04-08 02:10:54 -0500237 printf("Speed: %d, %s duplex%s\n", phydev->speed,
238 (phydev->duplex) ? "full" : "half",
239 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
Mingkai Hue0653bf2011-01-27 12:52:46 +0800240}
wdenkbfad55d2005-03-14 23:56:42 +0000241
chenhui zhaoc8caa8a2011-10-03 08:38:50 -0500242#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
243/*
244 * When MACCFG1[Rx_EN] is enabled during system boot as part
245 * of the eTSEC port initialization sequence,
246 * the eTSEC Rx logic may not be properly initialized.
247 */
248void redundant_init(struct eth_device *dev)
249{
250 struct tsec_private *priv = dev->priv;
251 tsec_t *regs = priv->regs;
252 uint t, count = 0;
253 int fail = 1;
254 static const u8 pkt[] = {
255 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
256 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
257 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
258 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
259 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
260 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
261 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
262 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
263 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
264 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
265 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
266 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
267 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
268 0x71, 0x72};
269
270 /* Enable promiscuous mode */
271 setbits_be32(&regs->rctrl, 0x8);
272 /* Enable loopback mode */
273 setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
274 /* Enable transmit and receive */
275 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
276
277 /* Tell the DMA it is clear to go */
278 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
279 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
280 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
281 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
282
283 do {
284 tsec_send(dev, (void *)pkt, sizeof(pkt));
285
286 /* Wait for buffer to be received */
287 for (t = 0; rtx.rxbd[rxIdx].status & RXBD_EMPTY; t++) {
288 if (t >= 10 * TOUT_LOOP) {
289 printf("%s: tsec: rx error\n", dev->name);
290 break;
291 }
292 }
293
294 if (!memcmp(pkt, (void *)NetRxPackets[rxIdx], sizeof(pkt)))
295 fail = 0;
296
297 rtx.rxbd[rxIdx].length = 0;
298 rtx.rxbd[rxIdx].status =
299 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
300 rxIdx = (rxIdx + 1) % PKTBUFSRX;
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 if (fail) {
307 printf("loopback recv packet error!\n");
308 clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
309 udelay(1000);
310 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
311 }
312 } while ((count++ < 4) && (fail == 1));
313
314 if (fail)
315 panic("eTSEC init fail!\n");
316 /* Disable promiscuous mode */
317 clrbits_be32(&regs->rctrl, 0x8);
318 /* Disable loopback mode */
319 clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
320}
321#endif
322
Mingkai Hue0653bf2011-01-27 12:52:46 +0800323/* Set up the buffers and their descriptors, and bring up the
324 * interface
Jon Loeligerb7ced082006-10-10 17:03:43 -0500325 */
Mingkai Hue0653bf2011-01-27 12:52:46 +0800326static void startup_tsec(struct eth_device *dev)
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100327{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800328 int i;
329 struct tsec_private *priv = (struct tsec_private *)dev->priv;
330 tsec_t *regs = priv->regs;
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100331
Andy Fleming422effd2011-04-08 02:10:54 -0500332 /* reset the indices to zero */
333 rxIdx = 0;
334 txIdx = 0;
chenhui zhaoc8caa8a2011-10-03 08:38:50 -0500335#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
336 uint svr;
337#endif
Andy Fleming422effd2011-04-08 02:10:54 -0500338
Mingkai Hue0653bf2011-01-27 12:52:46 +0800339 /* Point to the buffer descriptors */
340 out_be32(&regs->tbase, (unsigned int)(&rtx.txbd[txIdx]));
341 out_be32(&regs->rbase, (unsigned int)(&rtx.rxbd[rxIdx]));
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100342
Mingkai Hue0653bf2011-01-27 12:52:46 +0800343 /* Initialize the Rx Buffer descriptors */
344 for (i = 0; i < PKTBUFSRX; i++) {
345 rtx.rxbd[i].status = RXBD_EMPTY;
346 rtx.rxbd[i].length = 0;
347 rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
348 }
349 rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100350
Mingkai Hue0653bf2011-01-27 12:52:46 +0800351 /* Initialize the TX Buffer Descriptors */
352 for (i = 0; i < TX_BUF_CNT; i++) {
353 rtx.txbd[i].status = 0;
354 rtx.txbd[i].length = 0;
355 rtx.txbd[i].bufPtr = 0;
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100356 }
Mingkai Hue0653bf2011-01-27 12:52:46 +0800357 rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100358
chenhui zhaoc8caa8a2011-10-03 08:38:50 -0500359#ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
360 svr = get_svr();
361 if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
362 redundant_init(dev);
363#endif
Mingkai Hue0653bf2011-01-27 12:52:46 +0800364 /* Enable Transmit and Receive */
365 setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
366
367 /* Tell the DMA it is clear to go */
368 setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
369 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
370 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
371 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100372}
373
Mingkai Hue0653bf2011-01-27 12:52:46 +0800374/* This returns the status bits of the device. The return value
375 * is never checked, and this is what the 8260 driver did, so we
376 * do the same. Presumably, this would be zero if there were no
377 * errors
378 */
Joe Hershberger5d289fe2012-05-21 09:46:36 +0000379static int tsec_send(struct eth_device *dev, void *packet, int length)
Mingkai Hue0653bf2011-01-27 12:52:46 +0800380{
381 int i;
382 int result = 0;
383 struct tsec_private *priv = (struct tsec_private *)dev->priv;
384 tsec_t *regs = priv->regs;
Wolfgang Denkf0c4e462006-03-12 22:50:55 +0100385
Mingkai Hue0653bf2011-01-27 12:52:46 +0800386 /* Find an empty buffer descriptor */
387 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
388 if (i >= TOUT_LOOP) {
389 debug("%s: tsec: tx buffers full\n", dev->name);
390 return result;
391 }
392 }
Dave Liua304a282008-01-11 18:45:28 +0800393
Mingkai Hue0653bf2011-01-27 12:52:46 +0800394 rtx.txbd[txIdx].bufPtr = (uint) packet;
395 rtx.txbd[txIdx].length = length;
396 rtx.txbd[txIdx].status |=
397 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
Li Yang25e38bd2011-01-27 19:02:50 +0800398
Mingkai Hue0653bf2011-01-27 12:52:46 +0800399 /* Tell the DMA to go */
400 out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
wdenka445ddf2004-06-09 00:34:46 +0000401
Mingkai Hue0653bf2011-01-27 12:52:46 +0800402 /* Wait for buffer to be transmitted */
403 for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
404 if (i >= TOUT_LOOP) {
405 debug("%s: tsec: tx error\n", dev->name);
406 return result;
407 }
408 }
409
410 txIdx = (txIdx + 1) % TX_BUF_CNT;
411 result = rtx.txbd[txIdx].status & TXBD_STATS;
412
413 return result;
414}
415
416static int tsec_recv(struct eth_device *dev)
wdenka445ddf2004-06-09 00:34:46 +0000417{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800418 int length;
wdenka445ddf2004-06-09 00:34:46 +0000419 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Mingkai Hue0653bf2011-01-27 12:52:46 +0800420 tsec_t *regs = priv->regs;
wdenka445ddf2004-06-09 00:34:46 +0000421
Mingkai Hue0653bf2011-01-27 12:52:46 +0800422 while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
wdenka445ddf2004-06-09 00:34:46 +0000423
Mingkai Hue0653bf2011-01-27 12:52:46 +0800424 length = rtx.rxbd[rxIdx].length;
wdenka445ddf2004-06-09 00:34:46 +0000425
Mingkai Hue0653bf2011-01-27 12:52:46 +0800426 /* Send the packet up if there were no errors */
427 if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
428 NetReceive(NetRxPackets[rxIdx], length - 4);
429 } else {
430 printf("Got error %x\n",
431 (rtx.rxbd[rxIdx].status & RXBD_STATS));
Andy Flemingb2d14f42007-05-09 00:54:20 -0500432 }
Mingkai Hue0653bf2011-01-27 12:52:46 +0800433
434 rtx.rxbd[rxIdx].length = 0;
435
436 /* Set the wrap bit if this is the last element in the list */
437 rtx.rxbd[rxIdx].status =
438 RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
439
440 rxIdx = (rxIdx + 1) % PKTBUFSRX;
wdenka445ddf2004-06-09 00:34:46 +0000441 }
442
Mingkai Hue0653bf2011-01-27 12:52:46 +0800443 if (in_be32(&regs->ievent) & IEVENT_BSY) {
444 out_be32(&regs->ievent, IEVENT_BSY);
445 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
wdenka445ddf2004-06-09 00:34:46 +0000446 }
447
Mingkai Hue0653bf2011-01-27 12:52:46 +0800448 return -1;
449
wdenka445ddf2004-06-09 00:34:46 +0000450}
451
Mingkai Hue0653bf2011-01-27 12:52:46 +0800452/* Stop the interface */
453static void tsec_halt(struct eth_device *dev)
454{
455 struct tsec_private *priv = (struct tsec_private *)dev->priv;
456 tsec_t *regs = priv->regs;
457
458 clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
459 setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
460
461 while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
462 != (IEVENT_GRSC | IEVENT_GTSC))
463 ;
464
465 clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
466
467 /* Shut down the PHY, as needed */
Andy Fleming422effd2011-04-08 02:10:54 -0500468 phy_shutdown(priv->phydev);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800469}
470
471/* Initializes data structures and registers for the controller,
472 * and brings the interface up. Returns the link status, meaning
473 * that it returns success if the link is up, failure otherwise.
474 * This allows u-boot to find the first active controller.
Jon Loeligerb7ced082006-10-10 17:03:43 -0500475 */
Mingkai Hue0653bf2011-01-27 12:52:46 +0800476static int tsec_init(struct eth_device *dev, bd_t * bd)
wdenka445ddf2004-06-09 00:34:46 +0000477{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800478 uint tempval;
479 char tmpbuf[MAC_ADDR_LEN];
wdenka445ddf2004-06-09 00:34:46 +0000480 int i;
Mingkai Hue0653bf2011-01-27 12:52:46 +0800481 struct tsec_private *priv = (struct tsec_private *)dev->priv;
482 tsec_t *regs = priv->regs;
wdenka445ddf2004-06-09 00:34:46 +0000483
Mingkai Hue0653bf2011-01-27 12:52:46 +0800484 /* Make sure the controller is stopped */
485 tsec_halt(dev);
wdenka445ddf2004-06-09 00:34:46 +0000486
Mingkai Hue0653bf2011-01-27 12:52:46 +0800487 /* Init MACCFG2. Defaults to GMII */
488 out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
wdenka445ddf2004-06-09 00:34:46 +0000489
Mingkai Hue0653bf2011-01-27 12:52:46 +0800490 /* Init ECNTRL */
491 out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
wdenka445ddf2004-06-09 00:34:46 +0000492
Mingkai Hue0653bf2011-01-27 12:52:46 +0800493 /* Copy the station address into the address registers.
494 * Backwards, because little endian MACS are dumb */
495 for (i = 0; i < MAC_ADDR_LEN; i++)
496 tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
wdenka445ddf2004-06-09 00:34:46 +0000497
Mingkai Hue0653bf2011-01-27 12:52:46 +0800498 tempval = (tmpbuf[0] << 24) | (tmpbuf[1] << 16) | (tmpbuf[2] << 8) |
499 tmpbuf[3];
wdenka445ddf2004-06-09 00:34:46 +0000500
Mingkai Hue0653bf2011-01-27 12:52:46 +0800501 out_be32(&regs->macstnaddr1, tempval);
wdenka445ddf2004-06-09 00:34:46 +0000502
Mingkai Hue0653bf2011-01-27 12:52:46 +0800503 tempval = *((uint *) (tmpbuf + 4));
wdenka445ddf2004-06-09 00:34:46 +0000504
Mingkai Hue0653bf2011-01-27 12:52:46 +0800505 out_be32(&regs->macstnaddr2, tempval);
wdenka445ddf2004-06-09 00:34:46 +0000506
Mingkai Hue0653bf2011-01-27 12:52:46 +0800507 /* Clear out (for the most part) the other registers */
508 init_registers(regs);
509
510 /* Ready the device for tx/rx */
511 startup_tsec(dev);
512
Andy Fleming422effd2011-04-08 02:10:54 -0500513 /* Start up the PHY */
514 phy_startup(priv->phydev);
515
516 adjust_link(priv, priv->phydev);
517
Mingkai Hue0653bf2011-01-27 12:52:46 +0800518 /* If there's no link, fail */
Andy Fleming422effd2011-04-08 02:10:54 -0500519 return priv->phydev->link ? 0 : -1;
520}
521
522static phy_interface_t tsec_get_interface(struct tsec_private *priv)
523{
524 tsec_t *regs = priv->regs;
525 u32 ecntrl;
526
527 ecntrl = in_be32(&regs->ecntrl);
528
529 if (ecntrl & ECNTRL_SGMII_MODE)
530 return PHY_INTERFACE_MODE_SGMII;
531
532 if (ecntrl & ECNTRL_TBI_MODE) {
533 if (ecntrl & ECNTRL_REDUCED_MODE)
534 return PHY_INTERFACE_MODE_RTBI;
535 else
536 return PHY_INTERFACE_MODE_TBI;
537 }
538
539 if (ecntrl & ECNTRL_REDUCED_MODE) {
540 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
541 return PHY_INTERFACE_MODE_RMII;
542 else {
543 phy_interface_t interface = priv->interface;
544
545 /*
546 * This isn't autodetected, so it must
547 * be set by the platform code.
548 */
549 if ((interface == PHY_INTERFACE_MODE_RGMII_ID) ||
550 (interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
551 (interface == PHY_INTERFACE_MODE_RGMII_RXID))
552 return interface;
553
554 return PHY_INTERFACE_MODE_RGMII;
555 }
556 }
557
558 if (priv->flags & TSEC_GIGABIT)
559 return PHY_INTERFACE_MODE_GMII;
560
561 return PHY_INTERFACE_MODE_MII;
Mingkai Hue0653bf2011-01-27 12:52:46 +0800562}
563
Andy Fleming422effd2011-04-08 02:10:54 -0500564
Mingkai Hue0653bf2011-01-27 12:52:46 +0800565/* Discover which PHY is attached to the device, and configure it
566 * properly. If the PHY is not recognized, then return 0
567 * (failure). Otherwise, return 1
wdenk78924a72004-04-18 21:45:42 +0000568 */
Mingkai Hue0653bf2011-01-27 12:52:46 +0800569static int init_phy(struct eth_device *dev)
wdenk78924a72004-04-18 21:45:42 +0000570{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800571 struct tsec_private *priv = (struct tsec_private *)dev->priv;
Andy Fleming422effd2011-04-08 02:10:54 -0500572 struct phy_device *phydev;
Mingkai Hue0653bf2011-01-27 12:52:46 +0800573 tsec_t *regs = priv->regs;
Andy Fleming422effd2011-04-08 02:10:54 -0500574 u32 supported = (SUPPORTED_10baseT_Half |
575 SUPPORTED_10baseT_Full |
576 SUPPORTED_100baseT_Half |
577 SUPPORTED_100baseT_Full);
578
579 if (priv->flags & TSEC_GIGABIT)
580 supported |= SUPPORTED_1000baseT_Full;
wdenk78924a72004-04-18 21:45:42 +0000581
Mingkai Hue0653bf2011-01-27 12:52:46 +0800582 /* Assign a Physical address to the TBI */
583 out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
584
Andy Fleming422effd2011-04-08 02:10:54 -0500585 priv->interface = tsec_get_interface(priv);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800586
Andy Fleming422effd2011-04-08 02:10:54 -0500587 if (priv->interface == PHY_INTERFACE_MODE_SGMII)
588 tsec_configure_serdes(priv);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800589
Andy Fleming422effd2011-04-08 02:10:54 -0500590 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800591
Andy Fleming422effd2011-04-08 02:10:54 -0500592 phydev->supported &= supported;
593 phydev->advertising = phydev->supported;
wdenka445ddf2004-06-09 00:34:46 +0000594
Andy Fleming422effd2011-04-08 02:10:54 -0500595 priv->phydev = phydev;
wdenk78924a72004-04-18 21:45:42 +0000596
Andy Fleming422effd2011-04-08 02:10:54 -0500597 phy_config(phydev);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800598
599 return 1;
wdenk78924a72004-04-18 21:45:42 +0000600}
601
Mingkai Hue0653bf2011-01-27 12:52:46 +0800602/* Initialize device structure. Returns success if PHY
603 * initialization succeeded (i.e. if it recognizes the PHY)
wdenk78924a72004-04-18 21:45:42 +0000604 */
Mingkai Hue0653bf2011-01-27 12:52:46 +0800605static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
wdenk78924a72004-04-18 21:45:42 +0000606{
Mingkai Hue0653bf2011-01-27 12:52:46 +0800607 struct eth_device *dev;
608 int i;
609 struct tsec_private *priv;
wdenka445ddf2004-06-09 00:34:46 +0000610
Mingkai Hue0653bf2011-01-27 12:52:46 +0800611 dev = (struct eth_device *)malloc(sizeof *dev);
wdenk78924a72004-04-18 21:45:42 +0000612
Mingkai Hue0653bf2011-01-27 12:52:46 +0800613 if (NULL == dev)
614 return 0;
wdenk78924a72004-04-18 21:45:42 +0000615
Mingkai Hue0653bf2011-01-27 12:52:46 +0800616 memset(dev, 0, sizeof *dev);
wdenka445ddf2004-06-09 00:34:46 +0000617
Mingkai Hue0653bf2011-01-27 12:52:46 +0800618 priv = (struct tsec_private *)malloc(sizeof(*priv));
619
620 if (NULL == priv)
621 return 0;
622
623 privlist[num_tsecs++] = priv;
624 priv->regs = tsec_info->regs;
Mingkai Hue0653bf2011-01-27 12:52:46 +0800625 priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
626
627 priv->phyaddr = tsec_info->phyaddr;
628 priv->flags = tsec_info->flags;
wdenka445ddf2004-06-09 00:34:46 +0000629
Mingkai Hue0653bf2011-01-27 12:52:46 +0800630 sprintf(dev->name, tsec_info->devname);
Andy Fleming422effd2011-04-08 02:10:54 -0500631 priv->interface = tsec_info->interface;
632 priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
Mingkai Hue0653bf2011-01-27 12:52:46 +0800633 dev->iobase = 0;
634 dev->priv = priv;
635 dev->init = tsec_init;
636 dev->halt = tsec_halt;
637 dev->send = tsec_send;
638 dev->recv = tsec_recv;
David Updegraff7280da72007-06-11 10:41:07 -0500639#ifdef CONFIG_MCAST_TFTP
Mingkai Hue0653bf2011-01-27 12:52:46 +0800640 dev->mcast = tsec_mcast_addr;
641#endif
David Updegraff7280da72007-06-11 10:41:07 -0500642
Mingkai Hue0653bf2011-01-27 12:52:46 +0800643 /* Tell u-boot to get the addr from the env */
644 for (i = 0; i < 6; i++)
645 dev->enetaddr[i] = 0;
David Updegraff7280da72007-06-11 10:41:07 -0500646
Mingkai Hue0653bf2011-01-27 12:52:46 +0800647 eth_register(dev);
David Updegraff7280da72007-06-11 10:41:07 -0500648
Mingkai Hue0653bf2011-01-27 12:52:46 +0800649 /* Reset the MAC */
650 setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
651 udelay(2); /* Soft Reset must be asserted for 3 TX clocks */
652 clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
David Updegraff7280da72007-06-11 10:41:07 -0500653
Mingkai Hue0653bf2011-01-27 12:52:46 +0800654 /* Try to initialize PHY here, and return */
655 return init_phy(dev);
656}
David Updegraff7280da72007-06-11 10:41:07 -0500657
Mingkai Hue0653bf2011-01-27 12:52:46 +0800658/*
659 * Initialize all the TSEC devices
660 *
661 * Returns the number of TSEC devices that were initialized
662 */
663int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
664{
665 int i;
666 int ret, count = 0;
667
668 for (i = 0; i < num; i++) {
669 ret = tsec_initialize(bis, &tsecs[i]);
670 if (ret > 0)
671 count += ret;
David Updegraff7280da72007-06-11 10:41:07 -0500672 }
Mingkai Hue0653bf2011-01-27 12:52:46 +0800673
674 return count;
David Updegraff7280da72007-06-11 10:41:07 -0500675}
Mingkai Hue0653bf2011-01-27 12:52:46 +0800676
677int tsec_standard_init(bd_t *bis)
678{
Andy Fleming422effd2011-04-08 02:10:54 -0500679 struct fsl_pq_mdio_info info;
680
681 info.regs = (struct tsec_mii_mng *)CONFIG_SYS_MDIO_BASE_ADDR;
682 info.name = DEFAULT_MII_NAME;
683
684 fsl_pq_mdio_init(bis, &info);
685
Mingkai Hue0653bf2011-01-27 12:52:46 +0800686 return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
687}