common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / net / tsec.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale Three Speed Ethernet Controller driver
4  *
5  * Copyright 2004-2011, 2013 Freescale Semiconductor, Inc.
6  * (C) Copyright 2003, Motorola, Inc.
7  * author Andy Fleming
8  */
9
10 #include <config.h>
11 #include <common.h>
12 #include <dm.h>
13 #include <malloc.h>
14 #include <net.h>
15 #include <command.h>
16 #include <tsec.h>
17 #include <fsl_mdio.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <asm/processor.h>
21 #include <asm/io.h>
22
23 #ifndef CONFIG_DM_ETH
24 /* Default initializations for TSEC controllers. */
25
26 static struct tsec_info_struct tsec_info[] = {
27 #ifdef CONFIG_TSEC1
28         STD_TSEC_INFO(1),       /* TSEC1 */
29 #endif
30 #ifdef CONFIG_TSEC2
31         STD_TSEC_INFO(2),       /* TSEC2 */
32 #endif
33 #ifdef CONFIG_MPC85XX_FEC
34         {
35                 .regs = TSEC_GET_REGS(2, 0x2000),
36                 .devname = CONFIG_MPC85XX_FEC_NAME,
37                 .phyaddr = FEC_PHY_ADDR,
38                 .flags = FEC_FLAGS,
39                 .mii_devname = DEFAULT_MII_NAME
40         },                      /* FEC */
41 #endif
42 #ifdef CONFIG_TSEC3
43         STD_TSEC_INFO(3),       /* TSEC3 */
44 #endif
45 #ifdef CONFIG_TSEC4
46         STD_TSEC_INFO(4),       /* TSEC4 */
47 #endif
48 };
49 #endif /* CONFIG_DM_ETH */
50
51 #define TBIANA_SETTINGS ( \
52                 TBIANA_ASYMMETRIC_PAUSE \
53                 | TBIANA_SYMMETRIC_PAUSE \
54                 | TBIANA_FULL_DUPLEX \
55                 )
56
57 /* By default force the TBI PHY into 1000Mbps full duplex when in SGMII mode */
58 #ifndef CONFIG_TSEC_TBICR_SETTINGS
59 #define CONFIG_TSEC_TBICR_SETTINGS ( \
60                 TBICR_PHY_RESET \
61                 | TBICR_ANEG_ENABLE \
62                 | TBICR_FULL_DUPLEX \
63                 | TBICR_SPEED1_SET \
64                 )
65 #endif /* CONFIG_TSEC_TBICR_SETTINGS */
66
67 /* Configure the TBI for SGMII operation */
68 static void tsec_configure_serdes(struct tsec_private *priv)
69 {
70         /*
71          * Access TBI PHY registers at given TSEC register offset as opposed
72          * to the register offset used for external PHY accesses
73          */
74         tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
75                               0, TBI_ANA, TBIANA_SETTINGS);
76         tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
77                               0, TBI_TBICON, TBICON_CLK_SELECT);
78         tsec_local_mdio_write(priv->phyregs_sgmii, in_be32(&priv->regs->tbipa),
79                               0, TBI_CR, CONFIG_TSEC_TBICR_SETTINGS);
80 }
81
82 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
83  * and this is the ethernet-crc method needed for TSEC -- and perhaps
84  * some other adapter -- hash tables
85  */
86 #define CRCPOLY_LE 0xedb88320
87 static u32 ether_crc(size_t len, unsigned char const *p)
88 {
89         int i;
90         u32 crc;
91
92         crc = ~0;
93         while (len--) {
94                 crc ^= *p++;
95                 for (i = 0; i < 8; i++)
96                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
97         }
98         /* an reverse the bits, cuz of way they arrive -- last-first */
99         crc = (crc >> 16) | (crc << 16);
100         crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
101         crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
102         crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
103         crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
104         return crc;
105 }
106
107 /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
108
109 /* Set the appropriate hash bit for the given addr */
110
111 /*
112  * The algorithm works like so:
113  * 1) Take the Destination Address (ie the multicast address), and
114  * do a CRC on it (little endian), and reverse the bits of the
115  * result.
116  * 2) Use the 8 most significant bits as a hash into a 256-entry
117  * table.  The table is controlled through 8 32-bit registers:
118  * gaddr0-7.  gaddr0's MSB is entry 0, and gaddr7's LSB is entry
119  * 255.  This means that the 3 most significant bits in the
120  * hash index which gaddr register to use, and the 5 other bits
121  * indicate which bit (assuming an IBM numbering scheme, which
122  * for PowerPC (tm) is usually the case) in the register holds
123  * the entry.
124  */
125 #ifndef CONFIG_DM_ETH
126 static int tsec_mcast_addr(struct eth_device *dev, const u8 *mcast_mac,
127                            int join)
128 #else
129 static int tsec_mcast_addr(struct udevice *dev, const u8 *mcast_mac, int join)
130 #endif
131 {
132         struct tsec_private *priv = (struct tsec_private *)dev->priv;
133         struct tsec __iomem *regs = priv->regs;
134         u32 result, value;
135         u8 whichbit, whichreg;
136
137         result = ether_crc(MAC_ADDR_LEN, mcast_mac);
138         whichbit = (result >> 24) & 0x1f; /* the 5 LSB = which bit to set */
139         whichreg = result >> 29; /* the 3 MSB = which reg to set it in */
140
141         value = BIT(31 - whichbit);
142
143         if (join)
144                 setbits_be32(&regs->hash.gaddr0 + whichreg, value);
145         else
146                 clrbits_be32(&regs->hash.gaddr0 + whichreg, value);
147
148         return 0;
149 }
150
151 /*
152  * Initialized required registers to appropriate values, zeroing
153  * those we don't care about (unless zero is bad, in which case,
154  * choose a more appropriate value)
155  */
156 static void init_registers(struct tsec __iomem *regs)
157 {
158         /* Clear IEVENT */
159         out_be32(&regs->ievent, IEVENT_INIT_CLEAR);
160
161         out_be32(&regs->imask, IMASK_INIT_CLEAR);
162
163         out_be32(&regs->hash.iaddr0, 0);
164         out_be32(&regs->hash.iaddr1, 0);
165         out_be32(&regs->hash.iaddr2, 0);
166         out_be32(&regs->hash.iaddr3, 0);
167         out_be32(&regs->hash.iaddr4, 0);
168         out_be32(&regs->hash.iaddr5, 0);
169         out_be32(&regs->hash.iaddr6, 0);
170         out_be32(&regs->hash.iaddr7, 0);
171
172         out_be32(&regs->hash.gaddr0, 0);
173         out_be32(&regs->hash.gaddr1, 0);
174         out_be32(&regs->hash.gaddr2, 0);
175         out_be32(&regs->hash.gaddr3, 0);
176         out_be32(&regs->hash.gaddr4, 0);
177         out_be32(&regs->hash.gaddr5, 0);
178         out_be32(&regs->hash.gaddr6, 0);
179         out_be32(&regs->hash.gaddr7, 0);
180
181         out_be32(&regs->rctrl, 0x00000000);
182
183         /* Init RMON mib registers */
184         memset((void *)&regs->rmon, 0, sizeof(regs->rmon));
185
186         out_be32(&regs->rmon.cam1, 0xffffffff);
187         out_be32(&regs->rmon.cam2, 0xffffffff);
188
189         out_be32(&regs->mrblr, MRBLR_INIT_SETTINGS);
190
191         out_be32(&regs->minflr, MINFLR_INIT_SETTINGS);
192
193         out_be32(&regs->attr, ATTR_INIT_SETTINGS);
194         out_be32(&regs->attreli, ATTRELI_INIT_SETTINGS);
195 }
196
197 /*
198  * Configure maccfg2 based on negotiated speed and duplex
199  * reported by PHY handling code
200  */
201 static void adjust_link(struct tsec_private *priv, struct phy_device *phydev)
202 {
203         struct tsec __iomem *regs = priv->regs;
204         u32 ecntrl, maccfg2;
205
206         if (!phydev->link) {
207                 printf("%s: No link.\n", phydev->dev->name);
208                 return;
209         }
210
211         /* clear all bits relative with interface mode */
212         ecntrl = in_be32(&regs->ecntrl);
213         ecntrl &= ~ECNTRL_R100;
214
215         maccfg2 = in_be32(&regs->maccfg2);
216         maccfg2 &= ~(MACCFG2_IF | MACCFG2_FULL_DUPLEX);
217
218         if (phydev->duplex)
219                 maccfg2 |= MACCFG2_FULL_DUPLEX;
220
221         switch (phydev->speed) {
222         case 1000:
223                 maccfg2 |= MACCFG2_GMII;
224                 break;
225         case 100:
226         case 10:
227                 maccfg2 |= MACCFG2_MII;
228
229                 /*
230                  * Set R100 bit in all modes although
231                  * it is only used in RGMII mode
232                  */
233                 if (phydev->speed == 100)
234                         ecntrl |= ECNTRL_R100;
235                 break;
236         default:
237                 printf("%s: Speed was bad\n", phydev->dev->name);
238                 break;
239         }
240
241         out_be32(&regs->ecntrl, ecntrl);
242         out_be32(&regs->maccfg2, maccfg2);
243
244         printf("Speed: %d, %s duplex%s\n", phydev->speed,
245                (phydev->duplex) ? "full" : "half",
246                (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
247 }
248
249 /*
250  * This returns the status bits of the device. The return value
251  * is never checked, and this is what the 8260 driver did, so we
252  * do the same. Presumably, this would be zero if there were no
253  * errors
254  */
255 #ifndef CONFIG_DM_ETH
256 static int tsec_send(struct eth_device *dev, void *packet, int length)
257 #else
258 static int tsec_send(struct udevice *dev, void *packet, int length)
259 #endif
260 {
261         struct tsec_private *priv = (struct tsec_private *)dev->priv;
262         struct tsec __iomem *regs = priv->regs;
263         int result = 0;
264         u16 status;
265         int i;
266
267         /* Find an empty buffer descriptor */
268         for (i = 0;
269              in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
270              i++) {
271                 if (i >= TOUT_LOOP) {
272                         printf("%s: tsec: tx buffers full\n", dev->name);
273                         return result;
274                 }
275         }
276
277         out_be32(&priv->txbd[priv->tx_idx].bufptr, (u32)packet);
278         out_be16(&priv->txbd[priv->tx_idx].length, length);
279         status = in_be16(&priv->txbd[priv->tx_idx].status);
280         out_be16(&priv->txbd[priv->tx_idx].status, status |
281                 (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT));
282
283         /* Tell the DMA to go */
284         out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
285
286         /* Wait for buffer to be transmitted */
287         for (i = 0;
288              in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_READY;
289              i++) {
290                 if (i >= TOUT_LOOP) {
291                         printf("%s: tsec: tx error\n", dev->name);
292                         return result;
293                 }
294         }
295
296         priv->tx_idx = (priv->tx_idx + 1) % TX_BUF_CNT;
297         result = in_be16(&priv->txbd[priv->tx_idx].status) & TXBD_STATS;
298
299         return result;
300 }
301
302 #ifndef CONFIG_DM_ETH
303 static int tsec_recv(struct eth_device *dev)
304 {
305         struct tsec_private *priv = (struct tsec_private *)dev->priv;
306         struct tsec __iomem *regs = priv->regs;
307
308         while (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
309                 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
310                 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
311                 uchar *packet = net_rx_packets[priv->rx_idx];
312
313                 /* Send the packet up if there were no errors */
314                 if (!(status & RXBD_STATS))
315                         net_process_received_packet(packet, length - 4);
316                 else
317                         printf("Got error %x\n", (status & RXBD_STATS));
318
319                 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
320
321                 status = RXBD_EMPTY;
322                 /* Set the wrap bit if this is the last element in the list */
323                 if ((priv->rx_idx + 1) == PKTBUFSRX)
324                         status |= RXBD_WRAP;
325                 out_be16(&priv->rxbd[priv->rx_idx].status, status);
326
327                 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
328         }
329
330         if (in_be32(&regs->ievent) & IEVENT_BSY) {
331                 out_be32(&regs->ievent, IEVENT_BSY);
332                 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
333         }
334
335         return -1;
336 }
337 #else
338 static int tsec_recv(struct udevice *dev, int flags, uchar **packetp)
339 {
340         struct tsec_private *priv = (struct tsec_private *)dev->priv;
341         struct tsec __iomem *regs = priv->regs;
342         int ret = -1;
343
344         if (!(in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY)) {
345                 int length = in_be16(&priv->rxbd[priv->rx_idx].length);
346                 u16 status = in_be16(&priv->rxbd[priv->rx_idx].status);
347                 u32 buf;
348
349                 /* Send the packet up if there were no errors */
350                 if (!(status & RXBD_STATS)) {
351                         buf = in_be32(&priv->rxbd[priv->rx_idx].bufptr);
352                         *packetp = (uchar *)buf;
353                         ret = length - 4;
354                 } else {
355                         printf("Got error %x\n", (status & RXBD_STATS));
356                 }
357         }
358
359         if (in_be32(&regs->ievent) & IEVENT_BSY) {
360                 out_be32(&regs->ievent, IEVENT_BSY);
361                 out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
362         }
363
364         return ret;
365 }
366
367 static int tsec_free_pkt(struct udevice *dev, uchar *packet, int length)
368 {
369         struct tsec_private *priv = (struct tsec_private *)dev->priv;
370         u16 status;
371
372         out_be16(&priv->rxbd[priv->rx_idx].length, 0);
373
374         status = RXBD_EMPTY;
375         /* Set the wrap bit if this is the last element in the list */
376         if ((priv->rx_idx + 1) == PKTBUFSRX)
377                 status |= RXBD_WRAP;
378         out_be16(&priv->rxbd[priv->rx_idx].status, status);
379
380         priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
381
382         return 0;
383 }
384 #endif
385
386 /* Stop the interface */
387 #ifndef CONFIG_DM_ETH
388 static void tsec_halt(struct eth_device *dev)
389 #else
390 static void tsec_halt(struct udevice *dev)
391 #endif
392 {
393         struct tsec_private *priv = (struct tsec_private *)dev->priv;
394         struct tsec __iomem *regs = priv->regs;
395
396         clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
397         setbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
398
399         while ((in_be32(&regs->ievent) & (IEVENT_GRSC | IEVENT_GTSC))
400                         != (IEVENT_GRSC | IEVENT_GTSC))
401                 ;
402
403         clrbits_be32(&regs->maccfg1, MACCFG1_TX_EN | MACCFG1_RX_EN);
404
405         /* Shut down the PHY, as needed */
406         phy_shutdown(priv->phydev);
407 }
408
409 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
410 /*
411  * When MACCFG1[Rx_EN] is enabled during system boot as part
412  * of the eTSEC port initialization sequence,
413  * the eTSEC Rx logic may not be properly initialized.
414  */
415 void redundant_init(struct tsec_private *priv)
416 {
417         struct tsec __iomem *regs = priv->regs;
418         uint t, count = 0;
419         int fail = 1;
420         static const u8 pkt[] = {
421                 0x00, 0x1e, 0x4f, 0x12, 0xcb, 0x2c, 0x00, 0x25,
422                 0x64, 0xbb, 0xd1, 0xab, 0x08, 0x00, 0x45, 0x00,
423                 0x00, 0x5c, 0xdd, 0x22, 0x00, 0x00, 0x80, 0x01,
424                 0x1f, 0x71, 0x0a, 0xc1, 0x14, 0x22, 0x0a, 0xc1,
425                 0x14, 0x6a, 0x08, 0x00, 0xef, 0x7e, 0x02, 0x00,
426                 0x94, 0x05, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
427                 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
428                 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
429                 0x77, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
430                 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
431                 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
432                 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
433                 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
434                 0x71, 0x72};
435
436         /* Enable promiscuous mode */
437         setbits_be32(&regs->rctrl, 0x8);
438         /* Enable loopback mode */
439         setbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
440         /* Enable transmit and receive */
441         setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
442
443         /* Tell the DMA it is clear to go */
444         setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
445         out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
446         out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
447         clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
448
449         do {
450                 u16 status;
451
452                 tsec_send(priv->dev, (void *)pkt, sizeof(pkt));
453
454                 /* Wait for buffer to be received */
455                 for (t = 0;
456                      in_be16(&priv->rxbd[priv->rx_idx].status) & RXBD_EMPTY;
457                      t++) {
458                         if (t >= 10 * TOUT_LOOP) {
459                                 printf("%s: tsec: rx error\n", priv->dev->name);
460                                 break;
461                         }
462                 }
463
464                 if (!memcmp(pkt, net_rx_packets[priv->rx_idx], sizeof(pkt)))
465                         fail = 0;
466
467                 out_be16(&priv->rxbd[priv->rx_idx].length, 0);
468                 status = RXBD_EMPTY;
469                 if ((priv->rx_idx + 1) == PKTBUFSRX)
470                         status |= RXBD_WRAP;
471                 out_be16(&priv->rxbd[priv->rx_idx].status, status);
472                 priv->rx_idx = (priv->rx_idx + 1) % PKTBUFSRX;
473
474                 if (in_be32(&regs->ievent) & IEVENT_BSY) {
475                         out_be32(&regs->ievent, IEVENT_BSY);
476                         out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
477                 }
478                 if (fail) {
479                         printf("loopback recv packet error!\n");
480                         clrbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
481                         udelay(1000);
482                         setbits_be32(&regs->maccfg1, MACCFG1_RX_EN);
483                 }
484         } while ((count++ < 4) && (fail == 1));
485
486         if (fail)
487                 panic("eTSEC init fail!\n");
488         /* Disable promiscuous mode */
489         clrbits_be32(&regs->rctrl, 0x8);
490         /* Disable loopback mode */
491         clrbits_be32(&regs->maccfg1, MACCFG1_LOOPBACK);
492 }
493 #endif
494
495 /*
496  * Set up the buffers and their descriptors, and bring up the
497  * interface
498  */
499 static void startup_tsec(struct tsec_private *priv)
500 {
501         struct tsec __iomem *regs = priv->regs;
502         u16 status;
503         int i;
504
505         /* reset the indices to zero */
506         priv->rx_idx = 0;
507         priv->tx_idx = 0;
508 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
509         uint svr;
510 #endif
511
512         /* Point to the buffer descriptors */
513         out_be32(&regs->tbase, (u32)&priv->txbd[0]);
514         out_be32(&regs->rbase, (u32)&priv->rxbd[0]);
515
516         /* Initialize the Rx Buffer descriptors */
517         for (i = 0; i < PKTBUFSRX; i++) {
518                 out_be16(&priv->rxbd[i].status, RXBD_EMPTY);
519                 out_be16(&priv->rxbd[i].length, 0);
520                 out_be32(&priv->rxbd[i].bufptr, (u32)net_rx_packets[i]);
521         }
522         status = in_be16(&priv->rxbd[PKTBUFSRX - 1].status);
523         out_be16(&priv->rxbd[PKTBUFSRX - 1].status, status | RXBD_WRAP);
524
525         /* Initialize the TX Buffer Descriptors */
526         for (i = 0; i < TX_BUF_CNT; i++) {
527                 out_be16(&priv->txbd[i].status, 0);
528                 out_be16(&priv->txbd[i].length, 0);
529                 out_be32(&priv->txbd[i].bufptr, 0);
530         }
531         status = in_be16(&priv->txbd[TX_BUF_CNT - 1].status);
532         out_be16(&priv->txbd[TX_BUF_CNT - 1].status, status | TXBD_WRAP);
533
534 #ifdef CONFIG_SYS_FSL_ERRATUM_NMG_ETSEC129
535         svr = get_svr();
536         if ((SVR_MAJ(svr) == 1) || IS_SVR_REV(svr, 2, 0))
537                 redundant_init(priv);
538 #endif
539         /* Enable Transmit and Receive */
540         setbits_be32(&regs->maccfg1, MACCFG1_RX_EN | MACCFG1_TX_EN);
541
542         /* Tell the DMA it is clear to go */
543         setbits_be32(&regs->dmactrl, DMACTRL_INIT_SETTINGS);
544         out_be32(&regs->tstat, TSTAT_CLEAR_THALT);
545         out_be32(&regs->rstat, RSTAT_CLEAR_RHALT);
546         clrbits_be32(&regs->dmactrl, DMACTRL_GRS | DMACTRL_GTS);
547 }
548
549 /*
550  * Initializes data structures and registers for the controller,
551  * and brings the interface up. Returns the link status, meaning
552  * that it returns success if the link is up, failure otherwise.
553  * This allows U-Boot to find the first active controller.
554  */
555 #ifndef CONFIG_DM_ETH
556 static int tsec_init(struct eth_device *dev, bd_t *bd)
557 #else
558 static int tsec_init(struct udevice *dev)
559 #endif
560 {
561         struct tsec_private *priv = (struct tsec_private *)dev->priv;
562 #ifdef CONFIG_DM_ETH
563         struct eth_pdata *pdata = dev_get_platdata(dev);
564 #else
565         struct eth_device *pdata = dev;
566 #endif
567         struct tsec __iomem *regs = priv->regs;
568         u32 tempval;
569         int ret;
570
571         /* Make sure the controller is stopped */
572         tsec_halt(dev);
573
574         /* Init MACCFG2.  Defaults to GMII */
575         out_be32(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
576
577         /* Init ECNTRL */
578         out_be32(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
579
580         /*
581          * Copy the station address into the address registers.
582          * For a station address of 0x12345678ABCD in transmission
583          * order (BE), MACnADDR1 is set to 0xCDAB7856 and
584          * MACnADDR2 is set to 0x34120000.
585          */
586         tempval = (pdata->enetaddr[5] << 24) | (pdata->enetaddr[4] << 16) |
587                   (pdata->enetaddr[3] << 8)  |  pdata->enetaddr[2];
588
589         out_be32(&regs->macstnaddr1, tempval);
590
591         tempval = (pdata->enetaddr[1] << 24) | (pdata->enetaddr[0] << 16);
592
593         out_be32(&regs->macstnaddr2, tempval);
594
595         /* Clear out (for the most part) the other registers */
596         init_registers(regs);
597
598         /* Ready the device for tx/rx */
599         startup_tsec(priv);
600
601         /* Start up the PHY */
602         ret = phy_startup(priv->phydev);
603         if (ret) {
604                 printf("Could not initialize PHY %s\n",
605                        priv->phydev->dev->name);
606                 return ret;
607         }
608
609         adjust_link(priv, priv->phydev);
610
611         /* If there's no link, fail */
612         return priv->phydev->link ? 0 : -1;
613 }
614
615 static phy_interface_t tsec_get_interface(struct tsec_private *priv)
616 {
617         struct tsec __iomem *regs = priv->regs;
618         u32 ecntrl;
619
620         ecntrl = in_be32(&regs->ecntrl);
621
622         if (ecntrl & ECNTRL_SGMII_MODE)
623                 return PHY_INTERFACE_MODE_SGMII;
624
625         if (ecntrl & ECNTRL_TBI_MODE) {
626                 if (ecntrl & ECNTRL_REDUCED_MODE)
627                         return PHY_INTERFACE_MODE_RTBI;
628                 else
629                         return PHY_INTERFACE_MODE_TBI;
630         }
631
632         if (ecntrl & ECNTRL_REDUCED_MODE) {
633                 phy_interface_t interface;
634
635                 if (ecntrl & ECNTRL_REDUCED_MII_MODE)
636                         return PHY_INTERFACE_MODE_RMII;
637
638                 interface = priv->interface;
639
640                 /*
641                  * This isn't autodetected, so it must
642                  * be set by the platform code.
643                  */
644                 if (interface == PHY_INTERFACE_MODE_RGMII_ID ||
645                     interface == PHY_INTERFACE_MODE_RGMII_TXID ||
646                     interface == PHY_INTERFACE_MODE_RGMII_RXID)
647                         return interface;
648
649                 return PHY_INTERFACE_MODE_RGMII;
650         }
651
652         if (priv->flags & TSEC_GIGABIT)
653                 return PHY_INTERFACE_MODE_GMII;
654
655         return PHY_INTERFACE_MODE_MII;
656 }
657
658 /*
659  * Discover which PHY is attached to the device, and configure it
660  * properly.  If the PHY is not recognized, then return 0
661  * (failure).  Otherwise, return 1
662  */
663 static int init_phy(struct tsec_private *priv)
664 {
665         struct phy_device *phydev;
666         struct tsec __iomem *regs = priv->regs;
667         u32 supported = (SUPPORTED_10baseT_Half |
668                         SUPPORTED_10baseT_Full |
669                         SUPPORTED_100baseT_Half |
670                         SUPPORTED_100baseT_Full);
671
672         if (priv->flags & TSEC_GIGABIT)
673                 supported |= SUPPORTED_1000baseT_Full;
674
675         /* Assign a Physical address to the TBI */
676         out_be32(&regs->tbipa, priv->tbiaddr);
677
678         priv->interface = tsec_get_interface(priv);
679
680         if (priv->interface == PHY_INTERFACE_MODE_SGMII)
681                 tsec_configure_serdes(priv);
682
683         phydev = phy_connect(priv->bus, priv->phyaddr, priv->dev,
684                              priv->interface);
685         if (!phydev)
686                 return 0;
687
688         phydev->supported &= supported;
689         phydev->advertising = phydev->supported;
690
691         priv->phydev = phydev;
692
693         phy_config(phydev);
694
695         return 1;
696 }
697
698 #ifndef CONFIG_DM_ETH
699 /*
700  * Initialize device structure. Returns success if PHY
701  * initialization succeeded (i.e. if it recognizes the PHY)
702  */
703 static int tsec_initialize(bd_t *bis, struct tsec_info_struct *tsec_info)
704 {
705         struct tsec_private *priv;
706         struct eth_device *dev;
707         int i;
708
709         dev = (struct eth_device *)malloc(sizeof(*dev));
710
711         if (!dev)
712                 return 0;
713
714         memset(dev, 0, sizeof(*dev));
715
716         priv = (struct tsec_private *)malloc(sizeof(*priv));
717
718         if (!priv) {
719                 free(dev);
720                 return 0;
721         }
722
723         priv->regs = tsec_info->regs;
724         priv->phyregs_sgmii = tsec_info->miiregs_sgmii;
725
726         priv->phyaddr = tsec_info->phyaddr;
727         priv->tbiaddr = CONFIG_SYS_TBIPA_VALUE;
728         priv->flags = tsec_info->flags;
729
730         strcpy(dev->name, tsec_info->devname);
731         priv->interface = tsec_info->interface;
732         priv->bus = miiphy_get_dev_by_name(tsec_info->mii_devname);
733         priv->dev = dev;
734         dev->iobase = 0;
735         dev->priv = priv;
736         dev->init = tsec_init;
737         dev->halt = tsec_halt;
738         dev->send = tsec_send;
739         dev->recv = tsec_recv;
740         dev->mcast = tsec_mcast_addr;
741
742         /* Tell U-Boot to get the addr from the env */
743         for (i = 0; i < 6; i++)
744                 dev->enetaddr[i] = 0;
745
746         eth_register(dev);
747
748         /* Reset the MAC */
749         setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
750         udelay(2);  /* Soft Reset must be asserted for 3 TX clocks */
751         clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
752
753         /* Try to initialize PHY here, and return */
754         return init_phy(priv);
755 }
756
757 /*
758  * Initialize all the TSEC devices
759  *
760  * Returns the number of TSEC devices that were initialized
761  */
762 int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
763 {
764         int i;
765         int count = 0;
766
767         for (i = 0; i < num; i++) {
768                 int ret = tsec_initialize(bis, &tsecs[i]);
769
770                 if (ret > 0)
771                         count += ret;
772         }
773
774         return count;
775 }
776
777 int tsec_standard_init(bd_t *bis)
778 {
779         struct fsl_pq_mdio_info info;
780
781         info.regs = TSEC_GET_MDIO_REGS_BASE(1);
782         info.name = DEFAULT_MII_NAME;
783
784         fsl_pq_mdio_init(bis, &info);
785
786         return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
787 }
788 #else /* CONFIG_DM_ETH */
789 int tsec_probe(struct udevice *dev)
790 {
791         struct eth_pdata *pdata = dev_get_platdata(dev);
792         struct tsec_private *priv = dev_get_priv(dev);
793         struct ofnode_phandle_args phandle_args;
794         u32 tbiaddr = CONFIG_SYS_TBIPA_VALUE;
795         struct fsl_pq_mdio_info mdio_info;
796         const char *phy_mode;
797         fdt_addr_t reg;
798         ofnode parent;
799         int ret;
800
801         pdata->iobase = (phys_addr_t)dev_read_addr(dev);
802         priv->regs = (struct tsec *)pdata->iobase;
803
804         if (dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
805                                        &phandle_args)) {
806                 printf("phy-handle does not exist under tsec %s\n", dev->name);
807                 return -ENOENT;
808         } else {
809                 int reg = ofnode_read_u32_default(phandle_args.node, "reg", 0);
810
811                 priv->phyaddr = reg;
812         }
813
814         parent = ofnode_get_parent(phandle_args.node);
815         if (!ofnode_valid(parent)) {
816                 printf("No parent node for PHY?\n");
817                 return -ENOENT;
818         }
819
820         reg = ofnode_get_addr_index(parent, 0);
821         priv->phyregs_sgmii = (struct tsec_mii_mng *)
822                         (reg + TSEC_MDIO_REGS_OFFSET);
823
824         ret = dev_read_phandle_with_args(dev, "tbi-handle", NULL, 0, 0,
825                                          &phandle_args);
826         if (ret == 0)
827                 ofnode_read_u32(phandle_args.node, "reg", &tbiaddr);
828
829         priv->tbiaddr = tbiaddr;
830
831         phy_mode = dev_read_prop(dev, "phy-connection-type", NULL);
832         if (phy_mode)
833                 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
834         if (pdata->phy_interface == -1) {
835                 printf("Invalid PHY interface '%s'\n", phy_mode);
836                 return -EINVAL;
837         }
838         priv->interface = pdata->phy_interface;
839
840         /* Initialize flags */
841         priv->flags = TSEC_GIGABIT;
842         if (priv->interface == PHY_INTERFACE_MODE_SGMII)
843                 priv->flags |= TSEC_SGMII;
844
845         mdio_info.regs = priv->phyregs_sgmii;
846         mdio_info.name = (char *)dev->name;
847         ret = fsl_pq_mdio_init(NULL, &mdio_info);
848         if (ret)
849                 return ret;
850
851         /* Reset the MAC */
852         setbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
853         udelay(2);  /* Soft Reset must be asserted for 3 TX clocks */
854         clrbits_be32(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
855
856         priv->dev = dev;
857         priv->bus = miiphy_get_dev_by_name(dev->name);
858
859         /* Try to initialize PHY here, and return */
860         return !init_phy(priv);
861 }
862
863 int tsec_remove(struct udevice *dev)
864 {
865         struct tsec_private *priv = dev->priv;
866
867         free(priv->phydev);
868         mdio_unregister(priv->bus);
869         mdio_free(priv->bus);
870
871         return 0;
872 }
873
874 static const struct eth_ops tsec_ops = {
875         .start = tsec_init,
876         .send = tsec_send,
877         .recv = tsec_recv,
878         .free_pkt = tsec_free_pkt,
879         .stop = tsec_halt,
880         .mcast = tsec_mcast_addr,
881 };
882
883 static const struct udevice_id tsec_ids[] = {
884         { .compatible = "fsl,etsec2" },
885         { }
886 };
887
888 U_BOOT_DRIVER(eth_tsec) = {
889         .name = "tsec",
890         .id = UCLASS_ETH,
891         .of_match = tsec_ids,
892         .probe = tsec_probe,
893         .remove = tsec_remove,
894         .ops = &tsec_ops,
895         .priv_auto_alloc_size = sizeof(struct tsec_private),
896         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
897         .flags = DM_FLAG_ALLOC_PRIV_DMA,
898 };
899 #endif /* CONFIG_DM_ETH */