blob: 12be584638fadf7d8d905940a4ef166b95e564be [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy069fa832017-07-06 10:23:22 +02002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Christophe Leroy069fa832017-07-06 10:23:22 +02005 */
6
7#include <common.h>
8#include <command.h>
Simon Glassf11478f2019-12-28 10:45:07 -07009#include <hang.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020010#include <malloc.h>
11#include <net.h>
Christophe Leroy4a4750b2017-07-13 15:10:08 +020012#include <netdev.h>
Christophe Leroy10ff63a2018-03-16 17:20:43 +010013#include <asm/cpm_8xx.h>
Christophe Leroy394f9b32017-07-06 10:33:13 +020014#include <asm/io.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020016
17#include <phy.h>
Simon Glasscaefa322019-11-14 12:57:31 -070018#include <linux/mii.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020019
20DECLARE_GLOBAL_DATA_PTR;
21
Christophe Leroy069fa832017-07-06 10:23:22 +020022/* define WANT_MII when MII support is required */
23#if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
24#define WANT_MII
25#else
26#undef WANT_MII
27#endif
28
29#if defined(WANT_MII)
30#include <miiphy.h>
31
32#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
33#error "CONFIG_MII has to be defined!"
34#endif
35
36#endif
37
38#if defined(CONFIG_RMII) && !defined(WANT_MII)
39#error RMII support is unusable without a working PHY.
40#endif
41
42#ifdef CONFIG_SYS_DISCOVER_PHY
43static int mii_discover_phy(struct eth_device *dev);
44#endif
45
46int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg);
47int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
48 u16 value);
49
50static struct ether_fcc_info_s
51{
52 int ether_index;
53 int fecp_offset;
54 int phy_addr;
55 int actual_phy_addr;
56 int initialized;
57}
58 ether_fcc_info[] = {
59#if defined(CONFIG_ETHER_ON_FEC1)
60 {
61 0,
62 offsetof(immap_t, im_cpm.cp_fec1),
Christophe Leroy069fa832017-07-06 10:23:22 +020063 CONFIG_FEC1_PHY,
Christophe Leroy069fa832017-07-06 10:23:22 +020064 -1,
65 0,
66
67 },
68#endif
69#if defined(CONFIG_ETHER_ON_FEC2)
70 {
71 1,
72 offsetof(immap_t, im_cpm.cp_fec2),
Christophe Leroy069fa832017-07-06 10:23:22 +020073 CONFIG_FEC2_PHY,
Christophe Leroy069fa832017-07-06 10:23:22 +020074 -1,
75 0,
76 },
77#endif
78};
79
80/* Ethernet Transmit and Receive Buffers */
81#define DBUF_LENGTH 1520
82
83#define TX_BUF_CNT 2
84
85#define TOUT_LOOP 100
86
87#define PKT_MAXBUF_SIZE 1518
88#define PKT_MINBUF_SIZE 64
89#define PKT_MAXBLR_SIZE 1520
90
91#ifdef __GNUC__
Christophe Leroy48f896d2017-07-06 10:33:17 +020092static char txbuf[DBUF_LENGTH] __aligned(8);
Christophe Leroy069fa832017-07-06 10:23:22 +020093#else
94#error txbuf must be aligned.
95#endif
96
97static uint rxIdx; /* index of the current RX buffer */
98static uint txIdx; /* index of the current TX buffer */
99
100/*
101 * FEC Ethernet Tx and Rx buffer descriptors allocated at the
102 * immr->udata_bd address on Dual-Port RAM
103 * Provide for Double Buffering
104 */
105
Christophe Leroy394f9b32017-07-06 10:33:13 +0200106struct common_buf_desc {
Christophe Leroy48f896d2017-07-06 10:33:17 +0200107 cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
108 cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200109};
Christophe Leroy069fa832017-07-06 10:23:22 +0200110
Christophe Leroy394f9b32017-07-06 10:33:13 +0200111static struct common_buf_desc __iomem *rtx;
Christophe Leroy069fa832017-07-06 10:23:22 +0200112
113static int fec_send(struct eth_device *dev, void *packet, int length);
Christophe Leroy48f896d2017-07-06 10:33:17 +0200114static int fec_recv(struct eth_device *dev);
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900115static int fec_init(struct eth_device *dev, struct bd_info *bd);
Christophe Leroy48f896d2017-07-06 10:33:17 +0200116static void fec_halt(struct eth_device *dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200117#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
118static void __mii_init(void);
119#endif
120
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900121int fec_initialize(struct bd_info *bis)
Christophe Leroy069fa832017-07-06 10:23:22 +0200122{
Christophe Leroy48f896d2017-07-06 10:33:17 +0200123 struct eth_device *dev;
Christophe Leroy069fa832017-07-06 10:23:22 +0200124 struct ether_fcc_info_s *efis;
125 int i;
126
127 for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200128 dev = malloc(sizeof(*dev));
129 if (dev == NULL)
130 hang();
131
132 memset(dev, 0, sizeof(*dev));
133
134 /* for FEC1 make sure that the name of the interface is the same
135 as the old one for compatibility reasons */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200136 if (i == 0)
Christophe Leroy069fa832017-07-06 10:23:22 +0200137 strcpy(dev->name, "FEC");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200138 else
139 sprintf(dev->name, "FEC%d",
Christophe Leroy069fa832017-07-06 10:23:22 +0200140 ether_fcc_info[i].ether_index + 1);
Christophe Leroy069fa832017-07-06 10:23:22 +0200141
142 efis = &ether_fcc_info[i];
143
144 /*
145 * reset actual phy addr
146 */
147 efis->actual_phy_addr = -1;
148
149 dev->priv = efis;
150 dev->init = fec_init;
151 dev->halt = fec_halt;
152 dev->send = fec_send;
153 dev->recv = fec_recv;
154
155 eth_register(dev);
156
157#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
158 int retval;
159 struct mii_dev *mdiodev = mdio_alloc();
160 if (!mdiodev)
161 return -ENOMEM;
162 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
163 mdiodev->read = fec8xx_miiphy_read;
164 mdiodev->write = fec8xx_miiphy_write;
165
166 retval = mdio_register(mdiodev);
167 if (retval < 0)
168 return retval;
169#endif
170 }
171 return 1;
172}
173
174static int fec_send(struct eth_device *dev, void *packet, int length)
175{
176 int j, rc;
177 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200178 fec_t __iomem *fecp =
179 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200180
181 /* section 16.9.23.3
182 * Wait for ready
183 */
184 j = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200185 while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
186 (j < TOUT_LOOP)) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200187 udelay(1);
188 j++;
189 }
Christophe Leroy48f896d2017-07-06 10:33:17 +0200190 if (j >= TOUT_LOOP)
Christophe Leroy069fa832017-07-06 10:23:22 +0200191 printf("TX not ready\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200192
Christophe Leroy394f9b32017-07-06 10:33:13 +0200193 out_be32(&rtx->txbd[txIdx].cbd_bufaddr, (uint)packet);
194 out_be16(&rtx->txbd[txIdx].cbd_datlen, length);
195 setbits_be16(&rtx->txbd[txIdx].cbd_sc,
196 BD_ENET_TX_READY | BD_ENET_TX_LAST);
Christophe Leroy069fa832017-07-06 10:23:22 +0200197
198 /* Activate transmit Buffer Descriptor polling */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200199 /* Descriptor polling active */
200 out_be32(&fecp->fec_x_des_active, 0x01000000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200201
202 j = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200203 while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
204 (j < TOUT_LOOP)) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200205 udelay(1);
206 j++;
207 }
Christophe Leroy48f896d2017-07-06 10:33:17 +0200208 if (j >= TOUT_LOOP)
Christophe Leroy069fa832017-07-06 10:23:22 +0200209 printf("TX timeout\n");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200210
Christophe Leroy069fa832017-07-06 10:23:22 +0200211 /* return only status bits */;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200212 rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS;
Christophe Leroy069fa832017-07-06 10:23:22 +0200213
214 txIdx = (txIdx + 1) % TX_BUF_CNT;
215
216 return rc;
217}
218
Christophe Leroy48f896d2017-07-06 10:33:17 +0200219static int fec_recv(struct eth_device *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +0200220{
221 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200222 fec_t __iomem *fecp =
223 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200224 int length;
225
226 for (;;) {
227 /* section 16.9.23.2 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200228 if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200229 length = -1;
230 break; /* nothing received - leave for() loop */
231 }
232
Christophe Leroy394f9b32017-07-06 10:33:13 +0200233 length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen);
Christophe Leroy069fa832017-07-06 10:23:22 +0200234
Christophe Leroy394f9b32017-07-06 10:33:13 +0200235 if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200236 uchar *rx = net_rx_packets[rxIdx];
237
238 length -= 4;
239
240#if defined(CONFIG_CMD_CDP)
241 if ((rx[0] & 1) != 0 &&
242 memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 &&
243 !is_cdp_packet((uchar *)rx))
244 rx = NULL;
245#endif
246 /*
247 * Pass the packet up to the protocol layers.
248 */
249 if (rx != NULL)
250 net_process_received_packet(rx, length);
251 }
252
253 /* Give the buffer back to the FEC. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200254 out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200255
256 /* wrap around buffer index when necessary */
257 if ((rxIdx + 1) >= PKTBUFSRX) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200258 out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc,
259 BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200260 rxIdx = 0;
261 } else {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200262 out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200263 rxIdx++;
264 }
265
Christophe Leroy069fa832017-07-06 10:23:22 +0200266 /* Try to fill Buffer Descriptors */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200267 /* Descriptor polling active */
268 out_be32(&fecp->fec_r_des_active, 0x01000000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200269 }
270
271 return length;
272}
273
274/**************************************************************
275 *
276 * FEC Ethernet Initialization Routine
277 *
278 *************************************************************/
279
280#define FEC_ECNTRL_PINMUX 0x00000004
281#define FEC_ECNTRL_ETHER_EN 0x00000002
282#define FEC_ECNTRL_RESET 0x00000001
283
284#define FEC_RCNTRL_BC_REJ 0x00000010
285#define FEC_RCNTRL_PROM 0x00000008
286#define FEC_RCNTRL_MII_MODE 0x00000004
287#define FEC_RCNTRL_DRT 0x00000002
288#define FEC_RCNTRL_LOOP 0x00000001
289
290#define FEC_TCNTRL_FDEN 0x00000004
291#define FEC_TCNTRL_HBC 0x00000002
292#define FEC_TCNTRL_GTS 0x00000001
293
294#define FEC_RESET_DELAY 50
295
296#if defined(CONFIG_RMII)
297
298static inline void fec_10Mbps(struct eth_device *dev)
299{
300 struct ether_fcc_info_s *efis = dev->priv;
301 int fecidx = efis->ether_index;
302 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200303 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200304
305 if ((unsigned int)fecidx >= 2)
306 hang();
307
Christophe Leroy394f9b32017-07-06 10:33:13 +0200308 setbits_be32(&immr->im_cpm.cp_cptr, mask);
Christophe Leroy069fa832017-07-06 10:23:22 +0200309}
310
311static inline void fec_100Mbps(struct eth_device *dev)
312{
313 struct ether_fcc_info_s *efis = dev->priv;
314 int fecidx = efis->ether_index;
315 uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200316 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200317
318 if ((unsigned int)fecidx >= 2)
319 hang();
320
Christophe Leroy394f9b32017-07-06 10:33:13 +0200321 clrbits_be32(&immr->im_cpm.cp_cptr, mask);
Christophe Leroy069fa832017-07-06 10:23:22 +0200322}
323
324#endif
325
326static inline void fec_full_duplex(struct eth_device *dev)
327{
328 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200329 fec_t __iomem *fecp =
330 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200331
Christophe Leroy394f9b32017-07-06 10:33:13 +0200332 clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
333 setbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
Christophe Leroy069fa832017-07-06 10:23:22 +0200334}
335
336static inline void fec_half_duplex(struct eth_device *dev)
337{
338 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200339 fec_t __iomem *fecp =
340 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200341
Christophe Leroy394f9b32017-07-06 10:33:13 +0200342 setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
343 clrbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
Christophe Leroy069fa832017-07-06 10:23:22 +0200344}
345
346static void fec_pin_init(int fecidx)
347{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900348 struct bd_info *bd = gd->bd;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200349 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200350
351 /*
352 * Set MII speed to 2.5 MHz or slightly below.
353 *
354 * According to the MPC860T (Rev. D) Fast ethernet controller user
355 * manual (6.2.14),
356 * the MII management interface clock must be less than or equal
357 * to 2.5 MHz.
358 * This MDC frequency is equal to system clock / (2 * MII_SPEED).
359 * Then MII_SPEED = system_clock / 2 * 2,5 MHz.
360 *
361 * All MII configuration is done via FEC1 registers:
362 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200363 out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed,
364 ((bd->bi_intfreq + 4999999) / 5000000) << 1);
Christophe Leroy069fa832017-07-06 10:23:22 +0200365
Christophe Leroy23da3732017-07-06 10:33:21 +0200366#if defined(CONFIG_MPC885) && defined(WANT_MII)
Christophe Leroy069fa832017-07-06 10:23:22 +0200367 /* use MDC for MII */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200368 setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080);
369 clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080);
Christophe Leroy069fa832017-07-06 10:23:22 +0200370#endif
371
372 if (fecidx == 0) {
373#if defined(CONFIG_ETHER_ON_FEC1)
374
Christophe Leroy23da3732017-07-06 10:33:21 +0200375#if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
Christophe Leroy069fa832017-07-06 10:23:22 +0200376
377#if !defined(CONFIG_RMII)
378
Christophe Leroy394f9b32017-07-06 10:33:13 +0200379 setbits_be16(&immr->im_ioport.iop_papar, 0xf830);
380 setbits_be16(&immr->im_ioport.iop_padir, 0x0830);
381 clrbits_be16(&immr->im_ioport.iop_padir, 0xf000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200382
Christophe Leroy394f9b32017-07-06 10:33:13 +0200383 setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001);
384 clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200385
Christophe Leroy394f9b32017-07-06 10:33:13 +0200386 setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c);
387 clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c);
Christophe Leroy069fa832017-07-06 10:23:22 +0200388
Christophe Leroy394f9b32017-07-06 10:33:13 +0200389 setbits_be32(&immr->im_cpm.cp_pepar, 0x00000003);
390 setbits_be32(&immr->im_cpm.cp_pedir, 0x00000003);
391 clrbits_be32(&immr->im_cpm.cp_peso, 0x00000003);
Christophe Leroy069fa832017-07-06 10:23:22 +0200392
Christophe Leroy394f9b32017-07-06 10:33:13 +0200393 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
Christophe Leroy069fa832017-07-06 10:23:22 +0200394
395#else
396
397#if !defined(CONFIG_FEC1_PHY_NORXERR)
Christophe Leroy394f9b32017-07-06 10:33:13 +0200398 setbits_be16(&immr->im_ioport.iop_papar, 0x1000);
399 clrbits_be16(&immr->im_ioport.iop_padir, 0x1000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200400#endif
Christophe Leroy394f9b32017-07-06 10:33:13 +0200401 setbits_be16(&immr->im_ioport.iop_papar, 0xe810);
402 setbits_be16(&immr->im_ioport.iop_padir, 0x0810);
403 clrbits_be16(&immr->im_ioport.iop_padir, 0xe000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200404
Christophe Leroy394f9b32017-07-06 10:33:13 +0200405 setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001);
406 clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200407
Christophe Leroy394f9b32017-07-06 10:33:13 +0200408 setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
409 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050);
Christophe Leroy069fa832017-07-06 10:23:22 +0200410
411#endif /* !CONFIG_RMII */
412
413#else
414 /*
415 * Configure all of port D for MII.
416 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200417 out_be16(&immr->im_ioport.iop_pdpar, 0x1fff);
418 out_be16(&immr->im_ioport.iop_pddir, 0x1fff);
Christophe Leroy35982952017-07-07 10:16:42 +0200419
420#if defined(CONFIG_TARGET_MCR3000)
421 out_be16(&immr->im_ioport.iop_papar, 0xBBFF);
422 out_be16(&immr->im_ioport.iop_padir, 0x04F0);
423 out_be16(&immr->im_ioport.iop_paodr, 0x0000);
424
425 out_be32(&immr->im_cpm.cp_pbpar, 0x000133FF);
426 out_be32(&immr->im_cpm.cp_pbdir, 0x0003BF0F);
427 out_be16(&immr->im_cpm.cp_pbodr, 0x0000);
428
429 out_be16(&immr->im_ioport.iop_pcpar, 0x0400);
430 out_be16(&immr->im_ioport.iop_pcdir, 0x0080);
431 out_be16(&immr->im_ioport.iop_pcso , 0x0D53);
432 out_be16(&immr->im_ioport.iop_pcint, 0x0000);
433
434 out_be16(&immr->im_ioport.iop_pdpar, 0x03FE);
435 out_be16(&immr->im_ioport.iop_pddir, 0x1C09);
436
437 setbits_be32(&immr->im_ioport.utmode, 0x80);
438#endif
Christophe Leroy069fa832017-07-06 10:23:22 +0200439#endif
440
441#endif /* CONFIG_ETHER_ON_FEC1 */
442 } else if (fecidx == 1) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200443#if defined(CONFIG_ETHER_ON_FEC2)
444
Christophe Leroy23da3732017-07-06 10:33:21 +0200445#if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
Christophe Leroy069fa832017-07-06 10:23:22 +0200446
447#if !defined(CONFIG_RMII)
Christophe Leroy394f9b32017-07-06 10:33:13 +0200448 setbits_be32(&immr->im_cpm.cp_pepar, 0x0003fffc);
449 setbits_be32(&immr->im_cpm.cp_pedir, 0x0003fffc);
450 clrbits_be32(&immr->im_cpm.cp_peso, 0x000087fc);
451 setbits_be32(&immr->im_cpm.cp_peso, 0x00037800);
Christophe Leroy069fa832017-07-06 10:23:22 +0200452
Christophe Leroy394f9b32017-07-06 10:33:13 +0200453 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
Christophe Leroy069fa832017-07-06 10:23:22 +0200454#else
455
456#if !defined(CONFIG_FEC2_PHY_NORXERR)
Christophe Leroy394f9b32017-07-06 10:33:13 +0200457 setbits_be32(&immr->im_cpm.cp_pepar, 0x00000010);
458 setbits_be32(&immr->im_cpm.cp_pedir, 0x00000010);
459 clrbits_be32(&immr->im_cpm.cp_peso, 0x00000010);
Christophe Leroy069fa832017-07-06 10:23:22 +0200460#endif
Christophe Leroy394f9b32017-07-06 10:33:13 +0200461 setbits_be32(&immr->im_cpm.cp_pepar, 0x00039620);
462 setbits_be32(&immr->im_cpm.cp_pedir, 0x00039620);
463 setbits_be32(&immr->im_cpm.cp_peso, 0x00031000);
464 clrbits_be32(&immr->im_cpm.cp_peso, 0x00008620);
Christophe Leroy069fa832017-07-06 10:23:22 +0200465
Christophe Leroy394f9b32017-07-06 10:33:13 +0200466 setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
467 clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028);
Christophe Leroy069fa832017-07-06 10:23:22 +0200468#endif /* CONFIG_RMII */
469
Christophe Leroy23da3732017-07-06 10:33:21 +0200470#endif /* CONFIG_MPC885 */
Christophe Leroy069fa832017-07-06 10:23:22 +0200471
472#endif /* CONFIG_ETHER_ON_FEC2 */
Christophe Leroy069fa832017-07-06 10:23:22 +0200473 }
474}
475
Christophe Leroy394f9b32017-07-06 10:33:13 +0200476static int fec_reset(fec_t __iomem *fecp)
Christophe Leroy069fa832017-07-06 10:23:22 +0200477{
478 int i;
479
480 /* Whack a reset.
481 * A delay is required between a reset of the FEC block and
482 * initialization of other FEC registers because the reset takes
483 * some time to complete. If you don't delay, subsequent writes
484 * to FEC registers might get killed by the reset routine which is
485 * still in progress.
486 */
487
Christophe Leroy394f9b32017-07-06 10:33:13 +0200488 out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
489 for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
490 (i < FEC_RESET_DELAY); ++i)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200491 udelay(1);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200492
Christophe Leroy069fa832017-07-06 10:23:22 +0200493 if (i == FEC_RESET_DELAY)
494 return -1;
495
496 return 0;
497}
498
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900499static int fec_init(struct eth_device *dev, struct bd_info *bd)
Christophe Leroy069fa832017-07-06 10:23:22 +0200500{
501 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200502 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
503 fec_t __iomem *fecp =
504 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200505 int i;
506
507#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
508 /* the MII interface is connected to FEC1
509 * so for the miiphy_xxx function to work we must
510 * call mii_init since fec_halt messes the thing up
511 */
512 if (efis->ether_index != 0)
513 __mii_init();
514#endif
515
516 if (fec_reset(fecp) < 0)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200517 printf("FEC_RESET_DELAY timeout\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200518
519 /* We use strictly polling mode only
520 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200521 out_be32(&fecp->fec_imask, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200522
523 /* Clear any pending interrupt
524 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200525 out_be32(&fecp->fec_ievent, 0xffc0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200526
527 /* No need to set the IVEC register */
528
529 /* Set station address
530 */
531#define ea dev->enetaddr
Christophe Leroy394f9b32017-07-06 10:33:13 +0200532 out_be32(&fecp->fec_addr_low, (ea[0] << 24) | (ea[1] << 16) |
533 (ea[2] << 8) | ea[3]);
534 out_be16(&fecp->fec_addr_high, (ea[4] << 8) | ea[5]);
Christophe Leroy069fa832017-07-06 10:23:22 +0200535#undef ea
536
537#if defined(CONFIG_CMD_CDP)
538 /*
539 * Turn on multicast address hash table
540 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200541 out_be32(&fecp->fec_hash_table_high, 0xffffffff);
542 out_be32(&fecp->fec_hash_table_low, 0xffffffff);
Christophe Leroy069fa832017-07-06 10:23:22 +0200543#else
544 /* Clear multicast address hash table
545 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200546 out_be32(&fecp->fec_hash_table_high, 0);
547 out_be32(&fecp->fec_hash_table_low, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200548#endif
549
550 /* Set maximum receive buffer size.
551 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200552 out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200553
554 /* Set maximum frame length
555 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200556 out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200557
558 /*
Christophe Leroy48f896d2017-07-06 10:33:17 +0200559 * Setup Buffers and Buffer Descriptors
Christophe Leroy069fa832017-07-06 10:23:22 +0200560 */
561 rxIdx = 0;
562 txIdx = 0;
563
564 if (!rtx)
Christophe Leroy394f9b32017-07-06 10:33:13 +0200565 rtx = (struct common_buf_desc __iomem *)
566 (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
Christophe Leroy069fa832017-07-06 10:23:22 +0200567 /*
568 * Setup Receiver Buffer Descriptors (13.14.24.18)
569 * Settings:
570 * Empty, Wrap
571 */
572 for (i = 0; i < PKTBUFSRX; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200573 out_be16(&rtx->rxbd[i].cbd_sc, BD_ENET_RX_EMPTY);
574 out_be16(&rtx->rxbd[i].cbd_datlen, 0); /* Reset */
575 out_be32(&rtx->rxbd[i].cbd_bufaddr, (uint)net_rx_packets[i]);
Christophe Leroy069fa832017-07-06 10:23:22 +0200576 }
Christophe Leroy394f9b32017-07-06 10:33:13 +0200577 setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP);
Christophe Leroy069fa832017-07-06 10:23:22 +0200578
579 /*
580 * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
581 * Settings:
582 * Last, Tx CRC
583 */
584 for (i = 0; i < TX_BUF_CNT; i++) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200585 out_be16(&rtx->txbd[i].cbd_sc, BD_ENET_TX_LAST | BD_ENET_TX_TC);
586 out_be16(&rtx->txbd[i].cbd_datlen, 0); /* Reset */
587 out_be32(&rtx->txbd[i].cbd_bufaddr, (uint)txbuf);
Christophe Leroy069fa832017-07-06 10:23:22 +0200588 }
Christophe Leroy394f9b32017-07-06 10:33:13 +0200589 setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP);
Christophe Leroy069fa832017-07-06 10:23:22 +0200590
591 /* Set receive and transmit descriptor base
592 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200593 out_be32(&fecp->fec_r_des_start, (__force unsigned int)rtx->rxbd);
594 out_be32(&fecp->fec_x_des_start, (__force unsigned int)rtx->txbd);
Christophe Leroy069fa832017-07-06 10:23:22 +0200595
596 /* Enable MII mode
597 */
598 /* Half duplex mode */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200599 out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT);
600 out_be32(&fecp->fec_x_cntrl, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200601
602 /* Enable big endian and don't care about SDMA FC.
603 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200604 out_be32(&fecp->fec_fun_code, 0x78000000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200605
606 /*
607 * Setup the pin configuration of the FEC
608 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200609 fec_pin_init(efis->ether_index);
Christophe Leroy069fa832017-07-06 10:23:22 +0200610
611 rxIdx = 0;
612 txIdx = 0;
613
614 /*
615 * Now enable the transmit and receive processing
616 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200617 out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200618
619 if (efis->phy_addr == -1) {
620#ifdef CONFIG_SYS_DISCOVER_PHY
621 /*
622 * wait for the PHY to wake up after reset
623 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200624 efis->actual_phy_addr = mii_discover_phy(dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200625
626 if (efis->actual_phy_addr == -1) {
Christophe Leroy48f896d2017-07-06 10:33:17 +0200627 printf("Unable to discover phy!\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200628 return -1;
629 }
630#else
631 efis->actual_phy_addr = -1;
632#endif
633 } else {
634 efis->actual_phy_addr = efis->phy_addr;
635 }
636
637#if defined(CONFIG_MII) && defined(CONFIG_RMII)
638 /*
639 * adapt the RMII speed to the speed of the phy
640 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200641 if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET)
642 fec_100Mbps(dev);
643 else
644 fec_10Mbps(dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200645#endif
646
647#if defined(CONFIG_MII)
648 /*
649 * adapt to the half/full speed settings
650 */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200651 if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL)
652 fec_full_duplex(dev);
653 else
654 fec_half_duplex(dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200655#endif
656
657 /* And last, try to fill Rx Buffer Descriptors */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200658 /* Descriptor polling active */
659 out_be32(&fecp->fec_r_des_active, 0x01000000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200660
661 efis->initialized = 1;
662
663 return 0;
664}
665
666
Christophe Leroy48f896d2017-07-06 10:33:17 +0200667static void fec_halt(struct eth_device *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +0200668{
669 struct ether_fcc_info_s *efis = dev->priv;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200670 fec_t __iomem *fecp =
671 (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
Christophe Leroy069fa832017-07-06 10:23:22 +0200672 int i;
673
674 /* avoid halt if initialized; mii gets stuck otherwise */
675 if (!efis->initialized)
676 return;
677
678 /* Whack a reset.
679 * A delay is required between a reset of the FEC block and
680 * initialization of other FEC registers because the reset takes
681 * some time to complete. If you don't delay, subsequent writes
682 * to FEC registers might get killed by the reset routine which is
683 * still in progress.
684 */
685
Christophe Leroy394f9b32017-07-06 10:33:13 +0200686 out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
687 for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
688 (i < FEC_RESET_DELAY); ++i)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200689 udelay(1);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200690
Christophe Leroy069fa832017-07-06 10:23:22 +0200691 if (i == FEC_RESET_DELAY) {
Christophe Leroy48f896d2017-07-06 10:33:17 +0200692 printf("FEC_RESET_DELAY timeout\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200693 return;
694 }
695
696 efis->initialized = 0;
697}
698
699#if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
700
701/* Make MII read/write commands for the FEC.
702*/
703
704#define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
705 (REG & 0x1f) << 18))
706
707#define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
708 (REG & 0x1f) << 18) | \
709 (VAL & 0xffff))
710
711/* Interrupt events/masks.
712*/
713#define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
714#define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
715#define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
716#define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
717#define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
718#define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
719#define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
720#define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
721#define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
722#define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
723
724/* send command to phy using mii, wait for result */
725static uint
726mii_send(uint mii_cmd)
727{
728 uint mii_reply;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200729 fec_t __iomem *ep;
Christophe Leroy069fa832017-07-06 10:23:22 +0200730 int cnt;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200731 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
Christophe Leroy069fa832017-07-06 10:23:22 +0200732
Christophe Leroy394f9b32017-07-06 10:33:13 +0200733 ep = &immr->im_cpm.cp_fec;
Christophe Leroy069fa832017-07-06 10:23:22 +0200734
Christophe Leroy394f9b32017-07-06 10:33:13 +0200735 out_be32(&ep->fec_mii_data, mii_cmd); /* command to phy */
Christophe Leroy069fa832017-07-06 10:23:22 +0200736
737 /* wait for mii complete */
738 cnt = 0;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200739 while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) {
Christophe Leroy069fa832017-07-06 10:23:22 +0200740 if (++cnt > 1000) {
741 printf("mii_send STUCK!\n");
742 break;
743 }
744 }
Christophe Leroy394f9b32017-07-06 10:33:13 +0200745 mii_reply = in_be32(&ep->fec_mii_data); /* result from phy */
746 out_be32(&ep->fec_ievent, FEC_ENET_MII); /* clear MII complete */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200747 return mii_reply & 0xffff; /* data read from phy */
Christophe Leroy069fa832017-07-06 10:23:22 +0200748}
749#endif
750
751#if defined(CONFIG_SYS_DISCOVER_PHY)
752static int mii_discover_phy(struct eth_device *dev)
753{
754#define MAX_PHY_PASSES 11
755 uint phyno;
756 int pass;
757 uint phytype;
758 int phyaddr;
759
760 phyaddr = -1; /* didn't find a PHY yet */
761 for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
762 if (pass > 1) {
763 /* PHY may need more time to recover from reset.
764 * The LXT970 needs 50ms typical, no maximum is
765 * specified, so wait 10ms before try again.
766 * With 11 passes this gives it 100ms to wake up.
767 */
768 udelay(10000); /* wait 10ms */
769 }
770 for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
771 phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
772 if (phytype != 0xffff) {
773 phyaddr = phyno;
774 phytype |= mii_send(mk_mii_read(phyno,
775 MII_PHYSID1)) << 16;
776 }
777 }
778 }
Christophe Leroy48f896d2017-07-06 10:33:17 +0200779 if (phyaddr < 0)
Christophe Leroy069fa832017-07-06 10:23:22 +0200780 printf("No PHY device found.\n");
Christophe Leroy48f896d2017-07-06 10:33:17 +0200781
Christophe Leroy069fa832017-07-06 10:23:22 +0200782 return phyaddr;
783}
784#endif /* CONFIG_SYS_DISCOVER_PHY */
785
786#if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
787
788/****************************************************************************
789 * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
790 * This function is a subset of eth_init
791 ****************************************************************************
792 */
793static void __mii_init(void)
794{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200795 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
796 fec_t __iomem *fecp = &immr->im_cpm.cp_fec;
Christophe Leroy069fa832017-07-06 10:23:22 +0200797
798 if (fec_reset(fecp) < 0)
Christophe Leroy48f896d2017-07-06 10:33:17 +0200799 printf("FEC_RESET_DELAY timeout\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200800
801 /* We use strictly polling mode only
802 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200803 out_be32(&fecp->fec_imask, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200804
805 /* Clear any pending interrupt
806 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200807 out_be32(&fecp->fec_ievent, 0xffc0);
Christophe Leroy069fa832017-07-06 10:23:22 +0200808
809 /* Now enable the transmit and receive processing
810 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200811 out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
Christophe Leroy069fa832017-07-06 10:23:22 +0200812}
813
Christophe Leroy48f896d2017-07-06 10:33:17 +0200814void mii_init(void)
Christophe Leroy069fa832017-07-06 10:23:22 +0200815{
816 int i;
817
818 __mii_init();
819
820 /* Setup the pin configuration of the FEC(s)
821 */
822 for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++)
823 fec_pin_init(ether_fcc_info[i].ether_index);
824}
825
826/*****************************************************************************
827 * Read and write a MII PHY register, routines used by MII Utilities
828 *
829 * FIXME: These routines are expected to return 0 on success, but mii_send
830 * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
831 * no PHY connected...
832 * For now always return 0.
833 * FIXME: These routines only work after calling eth_init() at least once!
834 * Otherwise they hang in mii_send() !!! Sorry!
835 *****************************************************************************/
836
837int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
838{
839 unsigned short value = 0;
840 short rdreg; /* register working value */
841
842 rdreg = mii_send(mk_mii_read(addr, reg));
843
844 value = rdreg;
845 return value;
846}
847
848int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
849 u16 value)
850{
851 (void)mii_send(mk_mii_write(addr, reg, value));
852
853 return 0;
854}
855#endif