Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Simon Glass | db41d65 | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 9 | #include <hang.h> |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <net.h> |
Christophe Leroy | 08dd988 | 2017-07-13 15:10:08 +0200 | [diff] [blame] | 12 | #include <netdev.h> |
Christophe Leroy | 18f8d4c | 2018-03-16 17:20:43 +0100 | [diff] [blame] | 13 | #include <asm/cpm_8xx.h> |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 16 | |
| 17 | #include <phy.h> |
Simon Glass | 68a6aa8 | 2019-11-14 12:57:31 -0700 | [diff] [blame] | 18 | #include <linux/mii.h> |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 22 | /* 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 |
| 43 | static int mii_discover_phy(struct eth_device *dev); |
| 44 | #endif |
| 45 | |
| 46 | int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg); |
| 47 | int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 48 | u16 value); |
| 49 | |
| 50 | static 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 63 | CONFIG_FEC1_PHY, |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 64 | -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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 73 | CONFIG_FEC2_PHY, |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 74 | -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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 92 | static char txbuf[DBUF_LENGTH] __aligned(8); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 93 | #else |
| 94 | #error txbuf must be aligned. |
| 95 | #endif |
| 96 | |
| 97 | static uint rxIdx; /* index of the current RX buffer */ |
| 98 | static 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 106 | struct common_buf_desc { |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 107 | cbd_t rxbd[PKTBUFSRX]; /* Rx BD */ |
| 108 | cbd_t txbd[TX_BUF_CNT]; /* Tx BD */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 109 | }; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 110 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 111 | static struct common_buf_desc __iomem *rtx; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 112 | |
| 113 | static int fec_send(struct eth_device *dev, void *packet, int length); |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 114 | static int fec_recv(struct eth_device *dev); |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 115 | static int fec_init(struct eth_device *dev, struct bd_info *bd); |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 116 | static void fec_halt(struct eth_device *dev); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 117 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 118 | static void __mii_init(void); |
| 119 | #endif |
| 120 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 121 | int fec_initialize(struct bd_info *bis) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 122 | { |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 123 | struct eth_device *dev; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 124 | struct ether_fcc_info_s *efis; |
| 125 | int i; |
| 126 | |
| 127 | for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 128 | 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 136 | if (i == 0) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 137 | strcpy(dev->name, "FEC"); |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 138 | else |
| 139 | sprintf(dev->name, "FEC%d", |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 140 | ether_fcc_info[i].ether_index + 1); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 141 | |
| 142 | efis = ðer_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 | |
| 174 | static 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 178 | fec_t __iomem *fecp = |
| 179 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 180 | |
| 181 | /* section 16.9.23.3 |
| 182 | * Wait for ready |
| 183 | */ |
| 184 | j = 0; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 185 | while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) && |
| 186 | (j < TOUT_LOOP)) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 187 | udelay(1); |
| 188 | j++; |
| 189 | } |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 190 | if (j >= TOUT_LOOP) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 191 | printf("TX not ready\n"); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 192 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 193 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 197 | |
| 198 | /* Activate transmit Buffer Descriptor polling */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 199 | /* Descriptor polling active */ |
| 200 | out_be32(&fecp->fec_x_des_active, 0x01000000); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 201 | |
| 202 | j = 0; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 203 | while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) && |
| 204 | (j < TOUT_LOOP)) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 205 | udelay(1); |
| 206 | j++; |
| 207 | } |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 208 | if (j >= TOUT_LOOP) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 209 | printf("TX timeout\n"); |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 210 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 211 | /* return only status bits */; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 212 | rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 213 | |
| 214 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 215 | |
| 216 | return rc; |
| 217 | } |
| 218 | |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 219 | static int fec_recv(struct eth_device *dev) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 220 | { |
| 221 | struct ether_fcc_info_s *efis = dev->priv; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 222 | fec_t __iomem *fecp = |
| 223 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 224 | int length; |
| 225 | |
| 226 | for (;;) { |
| 227 | /* section 16.9.23.2 */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 228 | if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 229 | length = -1; |
| 230 | break; /* nothing received - leave for() loop */ |
| 231 | } |
| 232 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 233 | length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 234 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 235 | if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 236 | 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 254 | out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 255 | |
| 256 | /* wrap around buffer index when necessary */ |
| 257 | if ((rxIdx + 1) >= PKTBUFSRX) { |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 258 | out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, |
| 259 | BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 260 | rxIdx = 0; |
| 261 | } else { |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 262 | out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 263 | rxIdx++; |
| 264 | } |
| 265 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 266 | /* Try to fill Buffer Descriptors */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 267 | /* Descriptor polling active */ |
| 268 | out_be32(&fecp->fec_r_des_active, 0x01000000); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 269 | } |
| 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 | |
| 298 | static 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 303 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 304 | |
| 305 | if ((unsigned int)fecidx >= 2) |
| 306 | hang(); |
| 307 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 308 | setbits_be32(&immr->im_cpm.cp_cptr, mask); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 316 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 317 | |
| 318 | if ((unsigned int)fecidx >= 2) |
| 319 | hang(); |
| 320 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 321 | clrbits_be32(&immr->im_cpm.cp_cptr, mask); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | #endif |
| 325 | |
| 326 | static inline void fec_full_duplex(struct eth_device *dev) |
| 327 | { |
| 328 | struct ether_fcc_info_s *efis = dev->priv; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 329 | fec_t __iomem *fecp = |
| 330 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 331 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 332 | clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT); |
| 333 | setbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static inline void fec_half_duplex(struct eth_device *dev) |
| 337 | { |
| 338 | struct ether_fcc_info_s *efis = dev->priv; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 339 | fec_t __iomem *fecp = |
| 340 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 341 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 342 | setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT); |
| 343 | clrbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void fec_pin_init(int fecidx) |
| 347 | { |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 348 | struct bd_info *bd = gd->bd; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 349 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 350 | |
| 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 363 | out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed, |
| 364 | ((bd->bi_intfreq + 4999999) / 5000000) << 1); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 365 | |
Christophe Leroy | b1e41d1 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 366 | #if defined(CONFIG_MPC885) && defined(WANT_MII) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 367 | /* use MDC for MII */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 368 | setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080); |
| 369 | clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
| 372 | if (fecidx == 0) { |
| 373 | #if defined(CONFIG_ETHER_ON_FEC1) |
| 374 | |
Christophe Leroy | b1e41d1 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 375 | #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 376 | |
| 377 | #if !defined(CONFIG_RMII) |
| 378 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 379 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 382 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 383 | setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001); |
| 384 | clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 385 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 386 | setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c); |
| 387 | clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 388 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 389 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 392 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 393 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 394 | |
| 395 | #else |
| 396 | |
| 397 | #if !defined(CONFIG_FEC1_PHY_NORXERR) |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 398 | setbits_be16(&immr->im_ioport.iop_papar, 0x1000); |
| 399 | clrbits_be16(&immr->im_ioport.iop_padir, 0x1000); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 400 | #endif |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 401 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 404 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 405 | setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001); |
| 406 | clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 407 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 408 | setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100); |
| 409 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 410 | |
| 411 | #endif /* !CONFIG_RMII */ |
| 412 | |
| 413 | #else |
| 414 | /* |
| 415 | * Configure all of port D for MII. |
| 416 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 417 | out_be16(&immr->im_ioport.iop_pdpar, 0x1fff); |
| 418 | out_be16(&immr->im_ioport.iop_pddir, 0x1fff); |
Christophe Leroy | 53193a4 | 2017-07-07 10:16:42 +0200 | [diff] [blame] | 419 | |
| 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 439 | #endif |
| 440 | |
| 441 | #endif /* CONFIG_ETHER_ON_FEC1 */ |
| 442 | } else if (fecidx == 1) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 443 | #if defined(CONFIG_ETHER_ON_FEC2) |
| 444 | |
Christophe Leroy | b1e41d1 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 445 | #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 446 | |
| 447 | #if !defined(CONFIG_RMII) |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 448 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 452 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 453 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 454 | #else |
| 455 | |
| 456 | #if !defined(CONFIG_FEC2_PHY_NORXERR) |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 457 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 460 | #endif |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 461 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 465 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 466 | setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080); |
| 467 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 468 | #endif /* CONFIG_RMII */ |
| 469 | |
Christophe Leroy | b1e41d1 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 470 | #endif /* CONFIG_MPC885 */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 471 | |
| 472 | #endif /* CONFIG_ETHER_ON_FEC2 */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 476 | static int fec_reset(fec_t __iomem *fecp) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 477 | { |
| 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 488 | 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 491 | udelay(1); |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 492 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 493 | if (i == FEC_RESET_DELAY) |
| 494 | return -1; |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 499 | static int fec_init(struct eth_device *dev, struct bd_info *bd) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 500 | { |
| 501 | struct ether_fcc_info_s *efis = dev->priv; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 502 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 505 | 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 517 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 518 | |
| 519 | /* We use strictly polling mode only |
| 520 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 521 | out_be32(&fecp->fec_imask, 0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 522 | |
| 523 | /* Clear any pending interrupt |
| 524 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 525 | out_be32(&fecp->fec_ievent, 0xffc0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 526 | |
| 527 | /* No need to set the IVEC register */ |
| 528 | |
| 529 | /* Set station address |
| 530 | */ |
| 531 | #define ea dev->enetaddr |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 532 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 535 | #undef ea |
| 536 | |
| 537 | #if defined(CONFIG_CMD_CDP) |
| 538 | /* |
| 539 | * Turn on multicast address hash table |
| 540 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 541 | out_be32(&fecp->fec_hash_table_high, 0xffffffff); |
| 542 | out_be32(&fecp->fec_hash_table_low, 0xffffffff); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 543 | #else |
| 544 | /* Clear multicast address hash table |
| 545 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 546 | out_be32(&fecp->fec_hash_table_high, 0); |
| 547 | out_be32(&fecp->fec_hash_table_low, 0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 548 | #endif |
| 549 | |
| 550 | /* Set maximum receive buffer size. |
| 551 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 552 | out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 553 | |
| 554 | /* Set maximum frame length |
| 555 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 556 | out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 557 | |
| 558 | /* |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 559 | * Setup Buffers and Buffer Descriptors |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 560 | */ |
| 561 | rxIdx = 0; |
| 562 | txIdx = 0; |
| 563 | |
| 564 | if (!rtx) |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 565 | rtx = (struct common_buf_desc __iomem *) |
| 566 | (immr->im_cpm.cp_dpmem + CPM_FEC_BASE); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 567 | /* |
| 568 | * Setup Receiver Buffer Descriptors (13.14.24.18) |
| 569 | * Settings: |
| 570 | * Empty, Wrap |
| 571 | */ |
| 572 | for (i = 0; i < PKTBUFSRX; i++) { |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 573 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 576 | } |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 577 | setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 578 | |
| 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 585 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 588 | } |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 589 | setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 590 | |
| 591 | /* Set receive and transmit descriptor base |
| 592 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 593 | 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 Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 595 | |
| 596 | /* Enable MII mode |
| 597 | */ |
| 598 | /* Half duplex mode */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 599 | out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT); |
| 600 | out_be32(&fecp->fec_x_cntrl, 0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 601 | |
| 602 | /* Enable big endian and don't care about SDMA FC. |
| 603 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 604 | out_be32(&fecp->fec_fun_code, 0x78000000); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Setup the pin configuration of the FEC |
| 608 | */ |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 609 | fec_pin_init(efis->ether_index); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 610 | |
| 611 | rxIdx = 0; |
| 612 | txIdx = 0; |
| 613 | |
| 614 | /* |
| 615 | * Now enable the transmit and receive processing |
| 616 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 617 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 618 | |
| 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 624 | efis->actual_phy_addr = mii_discover_phy(dev); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 625 | |
| 626 | if (efis->actual_phy_addr == -1) { |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 627 | printf("Unable to discover phy!\n"); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 628 | 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 641 | if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET) |
| 642 | fec_100Mbps(dev); |
| 643 | else |
| 644 | fec_10Mbps(dev); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 645 | #endif |
| 646 | |
| 647 | #if defined(CONFIG_MII) |
| 648 | /* |
| 649 | * adapt to the half/full speed settings |
| 650 | */ |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 651 | if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL) |
| 652 | fec_full_duplex(dev); |
| 653 | else |
| 654 | fec_half_duplex(dev); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 655 | #endif |
| 656 | |
| 657 | /* And last, try to fill Rx Buffer Descriptors */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 658 | /* Descriptor polling active */ |
| 659 | out_be32(&fecp->fec_r_des_active, 0x01000000); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 660 | |
| 661 | efis->initialized = 1; |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 667 | static void fec_halt(struct eth_device *dev) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 668 | { |
| 669 | struct ether_fcc_info_s *efis = dev->priv; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 670 | fec_t __iomem *fecp = |
| 671 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 672 | 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 Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 686 | 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 689 | udelay(1); |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 690 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 691 | if (i == FEC_RESET_DELAY) { |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 692 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 693 | 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 */ |
| 725 | static uint |
| 726 | mii_send(uint mii_cmd) |
| 727 | { |
| 728 | uint mii_reply; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 729 | fec_t __iomem *ep; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 730 | int cnt; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 731 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 732 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 733 | ep = &immr->im_cpm.cp_fec; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 734 | |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 735 | out_be32(&ep->fec_mii_data, mii_cmd); /* command to phy */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 736 | |
| 737 | /* wait for mii complete */ |
| 738 | cnt = 0; |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 739 | while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) { |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 740 | if (++cnt > 1000) { |
| 741 | printf("mii_send STUCK!\n"); |
| 742 | break; |
| 743 | } |
| 744 | } |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 745 | mii_reply = in_be32(&ep->fec_mii_data); /* result from phy */ |
| 746 | out_be32(&ep->fec_ievent, FEC_ENET_MII); /* clear MII complete */ |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 747 | return mii_reply & 0xffff; /* data read from phy */ |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 748 | } |
| 749 | #endif |
| 750 | |
| 751 | #if defined(CONFIG_SYS_DISCOVER_PHY) |
| 752 | static 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 Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 779 | if (phyaddr < 0) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 780 | printf("No PHY device found.\n"); |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 781 | |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 782 | 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 | */ |
| 793 | static void __mii_init(void) |
| 794 | { |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 795 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 796 | fec_t __iomem *fecp = &immr->im_cpm.cp_fec; |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 797 | |
| 798 | if (fec_reset(fecp) < 0) |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 799 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 800 | |
| 801 | /* We use strictly polling mode only |
| 802 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 803 | out_be32(&fecp->fec_imask, 0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 804 | |
| 805 | /* Clear any pending interrupt |
| 806 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 807 | out_be32(&fecp->fec_ievent, 0xffc0); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 808 | |
| 809 | /* Now enable the transmit and receive processing |
| 810 | */ |
Christophe Leroy | ba3da73 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 811 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 812 | } |
| 813 | |
Christophe Leroy | 70fd071 | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 814 | void mii_init(void) |
Christophe Leroy | 907208c | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 815 | { |
| 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 | |
| 837 | int 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 | |
| 848 | int 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 |