Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Christophe Leroy | 069fa83 | 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 | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 8 | #include <hang.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <net.h> |
Christophe Leroy | 4a4750b | 2017-07-13 15:10:08 +0200 | [diff] [blame] | 11 | #include <netdev.h> |
Christophe Leroy | 10ff63a | 2018-03-16 17:20:43 +0100 | [diff] [blame] | 12 | #include <asm/cpm_8xx.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 16 | |
| 17 | #include <phy.h> |
Simon Glass | caefa32 | 2019-11-14 12:57:31 -0700 | [diff] [blame] | 18 | #include <linux/mii.h> |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Christophe Leroy | 069fa83 | 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 |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 43 | static int mii_discover_phy(struct udevice *dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 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 | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 63 | CONFIG_FEC1_PHY, |
Christophe Leroy | 069fa83 | 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 | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 73 | CONFIG_FEC2_PHY, |
Christophe Leroy | 069fa83 | 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 | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 92 | static char txbuf[DBUF_LENGTH] __aligned(8); |
Christophe Leroy | 069fa83 | 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 | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 106 | struct common_buf_desc { |
Christophe Leroy | 48f896d | 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 | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 109 | }; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 110 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 111 | static struct common_buf_desc __iomem *rtx; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 112 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 113 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 114 | static void __mii_init(void); |
| 115 | #endif |
| 116 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 117 | static int fec_probe(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 118 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 119 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
| 120 | int index = dev_get_driver_data(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 124 | if (ether_fcc_info[i].ether_index != index) |
| 125 | continue; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 126 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 127 | memcpy(efis, ðer_fcc_info[i], sizeof(*efis)); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 128 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 129 | efis->actual_phy_addr = -1; |
| 130 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 131 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 132 | int retval; |
| 133 | struct mii_dev *mdiodev = mdio_alloc(); |
| 134 | if (!mdiodev) |
| 135 | return -ENOMEM; |
Vladimir Oltean | 1e70f9f | 2021-09-27 14:21:53 +0300 | [diff] [blame] | 136 | strlcpy(mdiodev->name, dev->name, MDIO_NAME_LEN); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 137 | mdiodev->read = fec8xx_miiphy_read; |
| 138 | mdiodev->write = fec8xx_miiphy_write; |
| 139 | |
| 140 | retval = mdio_register(mdiodev); |
| 141 | if (retval < 0) |
| 142 | return retval; |
| 143 | #endif |
| 144 | } |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 145 | return 0; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 148 | static int fec_send(struct udevice *dev, void *packet, int length) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 149 | { |
| 150 | int j, rc; |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 151 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 152 | fec_t __iomem *fecp = |
| 153 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 154 | |
| 155 | /* section 16.9.23.3 |
| 156 | * Wait for ready |
| 157 | */ |
| 158 | j = 0; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 159 | while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) && |
| 160 | (j < TOUT_LOOP)) { |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 161 | udelay(1); |
| 162 | j++; |
| 163 | } |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 164 | if (j >= TOUT_LOOP) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 165 | printf("TX not ready\n"); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 166 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 167 | out_be32(&rtx->txbd[txIdx].cbd_bufaddr, (uint)packet); |
| 168 | out_be16(&rtx->txbd[txIdx].cbd_datlen, length); |
| 169 | setbits_be16(&rtx->txbd[txIdx].cbd_sc, |
| 170 | BD_ENET_TX_READY | BD_ENET_TX_LAST); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 171 | |
| 172 | /* Activate transmit Buffer Descriptor polling */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 173 | /* Descriptor polling active */ |
| 174 | out_be32(&fecp->fec_x_des_active, 0x01000000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 175 | |
| 176 | j = 0; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 177 | while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) && |
| 178 | (j < TOUT_LOOP)) { |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 179 | udelay(1); |
| 180 | j++; |
| 181 | } |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 182 | if (j >= TOUT_LOOP) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 183 | printf("TX timeout\n"); |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 184 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 185 | /* return only status bits */; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 186 | rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 187 | |
| 188 | txIdx = (txIdx + 1) % TX_BUF_CNT; |
| 189 | |
| 190 | return rc; |
| 191 | } |
| 192 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 193 | static int fec_recv(struct udevice *dev, int flags, uchar **packetp) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 194 | { |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 195 | int length; |
| 196 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 197 | /* section 16.9.23.2 */ |
| 198 | if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) |
| 199 | return -EAGAIN; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 200 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 201 | length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 202 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 203 | if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) { |
| 204 | uchar *rx = net_rx_packets[rxIdx]; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 205 | |
| 206 | #if defined(CONFIG_CMD_CDP) |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 207 | if ((rx[0] & 1) != 0 && |
| 208 | memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 && |
| 209 | !is_cdp_packet((uchar *)rx)) |
| 210 | return 0; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 211 | #endif |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 212 | *packetp = rx; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 213 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 214 | return length - 4; |
| 215 | } else { |
| 216 | return 0; |
| 217 | } |
| 218 | } |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 219 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 220 | static int fec_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 221 | { |
| 222 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
| 223 | fec_t __iomem *fecp = |
| 224 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
| 225 | |
| 226 | /* Give the buffer back to the FEC. */ |
| 227 | out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 228 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 229 | /* wrap around buffer index when necessary */ |
| 230 | if ((rxIdx + 1) >= PKTBUFSRX) { |
| 231 | out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, |
| 232 | BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY); |
| 233 | rxIdx = 0; |
| 234 | } else { |
| 235 | out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY); |
| 236 | rxIdx++; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 239 | /* Try to fill Buffer Descriptors */ |
| 240 | /* Descriptor polling active */ |
| 241 | out_be32(&fecp->fec_r_des_active, 0x01000000); |
| 242 | |
| 243 | return 0; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /************************************************************** |
| 247 | * |
| 248 | * FEC Ethernet Initialization Routine |
| 249 | * |
| 250 | *************************************************************/ |
| 251 | |
| 252 | #define FEC_ECNTRL_PINMUX 0x00000004 |
| 253 | #define FEC_ECNTRL_ETHER_EN 0x00000002 |
| 254 | #define FEC_ECNTRL_RESET 0x00000001 |
| 255 | |
| 256 | #define FEC_RCNTRL_BC_REJ 0x00000010 |
| 257 | #define FEC_RCNTRL_PROM 0x00000008 |
| 258 | #define FEC_RCNTRL_MII_MODE 0x00000004 |
| 259 | #define FEC_RCNTRL_DRT 0x00000002 |
| 260 | #define FEC_RCNTRL_LOOP 0x00000001 |
| 261 | |
| 262 | #define FEC_TCNTRL_FDEN 0x00000004 |
| 263 | #define FEC_TCNTRL_HBC 0x00000002 |
| 264 | #define FEC_TCNTRL_GTS 0x00000001 |
| 265 | |
| 266 | #define FEC_RESET_DELAY 50 |
| 267 | |
| 268 | #if defined(CONFIG_RMII) |
| 269 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 270 | static inline void fec_10Mbps(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 271 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 272 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 273 | int fecidx = efis->ether_index; |
| 274 | uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 275 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 276 | |
| 277 | if ((unsigned int)fecidx >= 2) |
| 278 | hang(); |
| 279 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 280 | setbits_be32(&immr->im_cpm.cp_cptr, mask); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 281 | } |
| 282 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 283 | static inline void fec_100Mbps(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 284 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 285 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 286 | int fecidx = efis->ether_index; |
| 287 | uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 288 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 289 | |
| 290 | if ((unsigned int)fecidx >= 2) |
| 291 | hang(); |
| 292 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 293 | clrbits_be32(&immr->im_cpm.cp_cptr, mask); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | #endif |
| 297 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 298 | static inline void fec_full_duplex(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 299 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 300 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 301 | fec_t __iomem *fecp = |
| 302 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 303 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 304 | clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT); |
| 305 | setbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 308 | static inline void fec_half_duplex(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 309 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 310 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 311 | fec_t __iomem *fecp = |
| 312 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 313 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 314 | setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT); |
| 315 | clrbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void fec_pin_init(int fecidx) |
| 319 | { |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 320 | struct bd_info *bd = gd->bd; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 321 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 322 | |
| 323 | /* |
| 324 | * Set MII speed to 2.5 MHz or slightly below. |
| 325 | * |
| 326 | * According to the MPC860T (Rev. D) Fast ethernet controller user |
| 327 | * manual (6.2.14), |
| 328 | * the MII management interface clock must be less than or equal |
| 329 | * to 2.5 MHz. |
| 330 | * This MDC frequency is equal to system clock / (2 * MII_SPEED). |
| 331 | * Then MII_SPEED = system_clock / 2 * 2,5 MHz. |
| 332 | * |
| 333 | * All MII configuration is done via FEC1 registers: |
| 334 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 335 | out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed, |
| 336 | ((bd->bi_intfreq + 4999999) / 5000000) << 1); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 337 | |
Christophe Leroy | 23da373 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 338 | #if defined(CONFIG_MPC885) && defined(WANT_MII) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 339 | /* use MDC for MII */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 340 | setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080); |
| 341 | clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 342 | #endif |
| 343 | |
| 344 | if (fecidx == 0) { |
| 345 | #if defined(CONFIG_ETHER_ON_FEC1) |
| 346 | |
Christophe Leroy | 23da373 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 347 | #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 348 | |
| 349 | #if !defined(CONFIG_RMII) |
| 350 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 351 | setbits_be16(&immr->im_ioport.iop_papar, 0xf830); |
| 352 | setbits_be16(&immr->im_ioport.iop_padir, 0x0830); |
| 353 | clrbits_be16(&immr->im_ioport.iop_padir, 0xf000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 354 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 355 | setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001); |
| 356 | clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 357 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 358 | setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c); |
| 359 | clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 360 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 361 | setbits_be32(&immr->im_cpm.cp_pepar, 0x00000003); |
| 362 | setbits_be32(&immr->im_cpm.cp_pedir, 0x00000003); |
| 363 | clrbits_be32(&immr->im_cpm.cp_peso, 0x00000003); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 364 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 365 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 366 | |
| 367 | #else |
| 368 | |
| 369 | #if !defined(CONFIG_FEC1_PHY_NORXERR) |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 370 | setbits_be16(&immr->im_ioport.iop_papar, 0x1000); |
| 371 | clrbits_be16(&immr->im_ioport.iop_padir, 0x1000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 372 | #endif |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 373 | setbits_be16(&immr->im_ioport.iop_papar, 0xe810); |
| 374 | setbits_be16(&immr->im_ioport.iop_padir, 0x0810); |
| 375 | clrbits_be16(&immr->im_ioport.iop_padir, 0xe000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 376 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 377 | setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001); |
| 378 | clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 379 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 380 | setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100); |
| 381 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 382 | |
| 383 | #endif /* !CONFIG_RMII */ |
| 384 | |
| 385 | #else |
| 386 | /* |
| 387 | * Configure all of port D for MII. |
| 388 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 389 | out_be16(&immr->im_ioport.iop_pdpar, 0x1fff); |
| 390 | out_be16(&immr->im_ioport.iop_pddir, 0x1fff); |
Christophe Leroy | 3598295 | 2017-07-07 10:16:42 +0200 | [diff] [blame] | 391 | |
| 392 | #if defined(CONFIG_TARGET_MCR3000) |
| 393 | out_be16(&immr->im_ioport.iop_papar, 0xBBFF); |
| 394 | out_be16(&immr->im_ioport.iop_padir, 0x04F0); |
| 395 | out_be16(&immr->im_ioport.iop_paodr, 0x0000); |
| 396 | |
| 397 | out_be32(&immr->im_cpm.cp_pbpar, 0x000133FF); |
| 398 | out_be32(&immr->im_cpm.cp_pbdir, 0x0003BF0F); |
| 399 | out_be16(&immr->im_cpm.cp_pbodr, 0x0000); |
| 400 | |
| 401 | out_be16(&immr->im_ioport.iop_pcpar, 0x0400); |
| 402 | out_be16(&immr->im_ioport.iop_pcdir, 0x0080); |
| 403 | out_be16(&immr->im_ioport.iop_pcso , 0x0D53); |
| 404 | out_be16(&immr->im_ioport.iop_pcint, 0x0000); |
| 405 | |
| 406 | out_be16(&immr->im_ioport.iop_pdpar, 0x03FE); |
| 407 | out_be16(&immr->im_ioport.iop_pddir, 0x1C09); |
| 408 | |
| 409 | setbits_be32(&immr->im_ioport.utmode, 0x80); |
| 410 | #endif |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 411 | #endif |
| 412 | |
| 413 | #endif /* CONFIG_ETHER_ON_FEC1 */ |
| 414 | } else if (fecidx == 1) { |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 415 | #if defined(CONFIG_ETHER_ON_FEC2) |
| 416 | |
Christophe Leroy | 23da373 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 417 | #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 418 | |
| 419 | #if !defined(CONFIG_RMII) |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 420 | setbits_be32(&immr->im_cpm.cp_pepar, 0x0003fffc); |
| 421 | setbits_be32(&immr->im_cpm.cp_pedir, 0x0003fffc); |
| 422 | clrbits_be32(&immr->im_cpm.cp_peso, 0x000087fc); |
| 423 | setbits_be32(&immr->im_cpm.cp_peso, 0x00037800); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 424 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 425 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 426 | #else |
| 427 | |
| 428 | #if !defined(CONFIG_FEC2_PHY_NORXERR) |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 429 | setbits_be32(&immr->im_cpm.cp_pepar, 0x00000010); |
| 430 | setbits_be32(&immr->im_cpm.cp_pedir, 0x00000010); |
| 431 | clrbits_be32(&immr->im_cpm.cp_peso, 0x00000010); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 432 | #endif |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 433 | setbits_be32(&immr->im_cpm.cp_pepar, 0x00039620); |
| 434 | setbits_be32(&immr->im_cpm.cp_pedir, 0x00039620); |
| 435 | setbits_be32(&immr->im_cpm.cp_peso, 0x00031000); |
| 436 | clrbits_be32(&immr->im_cpm.cp_peso, 0x00008620); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 437 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 438 | setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080); |
| 439 | clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 440 | #endif /* CONFIG_RMII */ |
| 441 | |
Christophe Leroy | 23da373 | 2017-07-06 10:33:21 +0200 | [diff] [blame] | 442 | #endif /* CONFIG_MPC885 */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 443 | |
| 444 | #endif /* CONFIG_ETHER_ON_FEC2 */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 448 | static int fec_reset(fec_t __iomem *fecp) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 449 | { |
| 450 | int i; |
| 451 | |
| 452 | /* Whack a reset. |
| 453 | * A delay is required between a reset of the FEC block and |
| 454 | * initialization of other FEC registers because the reset takes |
| 455 | * some time to complete. If you don't delay, subsequent writes |
| 456 | * to FEC registers might get killed by the reset routine which is |
| 457 | * still in progress. |
| 458 | */ |
| 459 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 460 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET); |
| 461 | for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) && |
| 462 | (i < FEC_RESET_DELAY); ++i) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 463 | udelay(1); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 464 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 465 | if (i == FEC_RESET_DELAY) |
| 466 | return -1; |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 471 | static int fec_start(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 472 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 473 | struct eth_pdata *plat = dev_get_plat(dev); |
| 474 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 475 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 476 | fec_t __iomem *fecp = |
| 477 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 478 | int i; |
| 479 | |
| 480 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 481 | /* the MII interface is connected to FEC1 |
| 482 | * so for the miiphy_xxx function to work we must |
| 483 | * call mii_init since fec_halt messes the thing up |
| 484 | */ |
| 485 | if (efis->ether_index != 0) |
| 486 | __mii_init(); |
| 487 | #endif |
| 488 | |
| 489 | if (fec_reset(fecp) < 0) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 490 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 491 | |
| 492 | /* We use strictly polling mode only |
| 493 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 494 | out_be32(&fecp->fec_imask, 0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 495 | |
| 496 | /* Clear any pending interrupt |
| 497 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 498 | out_be32(&fecp->fec_ievent, 0xffc0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 499 | |
| 500 | /* No need to set the IVEC register */ |
| 501 | |
| 502 | /* Set station address |
| 503 | */ |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 504 | #define ea plat->enetaddr |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 505 | out_be32(&fecp->fec_addr_low, (ea[0] << 24) | (ea[1] << 16) | |
| 506 | (ea[2] << 8) | ea[3]); |
| 507 | out_be16(&fecp->fec_addr_high, (ea[4] << 8) | ea[5]); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 508 | #undef ea |
| 509 | |
| 510 | #if defined(CONFIG_CMD_CDP) |
| 511 | /* |
| 512 | * Turn on multicast address hash table |
| 513 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 514 | out_be32(&fecp->fec_hash_table_high, 0xffffffff); |
| 515 | out_be32(&fecp->fec_hash_table_low, 0xffffffff); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 516 | #else |
| 517 | /* Clear multicast address hash table |
| 518 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 519 | out_be32(&fecp->fec_hash_table_high, 0); |
| 520 | out_be32(&fecp->fec_hash_table_low, 0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 521 | #endif |
| 522 | |
| 523 | /* Set maximum receive buffer size. |
| 524 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 525 | out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 526 | |
| 527 | /* Set maximum frame length |
| 528 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 529 | out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 530 | |
| 531 | /* |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 532 | * Setup Buffers and Buffer Descriptors |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 533 | */ |
| 534 | rxIdx = 0; |
| 535 | txIdx = 0; |
| 536 | |
| 537 | if (!rtx) |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 538 | rtx = (struct common_buf_desc __iomem *) |
| 539 | (immr->im_cpm.cp_dpmem + CPM_FEC_BASE); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 540 | /* |
| 541 | * Setup Receiver Buffer Descriptors (13.14.24.18) |
| 542 | * Settings: |
| 543 | * Empty, Wrap |
| 544 | */ |
| 545 | for (i = 0; i < PKTBUFSRX; i++) { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 546 | out_be16(&rtx->rxbd[i].cbd_sc, BD_ENET_RX_EMPTY); |
| 547 | out_be16(&rtx->rxbd[i].cbd_datlen, 0); /* Reset */ |
| 548 | out_be32(&rtx->rxbd[i].cbd_bufaddr, (uint)net_rx_packets[i]); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 549 | } |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 550 | setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 551 | |
| 552 | /* |
| 553 | * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19) |
| 554 | * Settings: |
| 555 | * Last, Tx CRC |
| 556 | */ |
| 557 | for (i = 0; i < TX_BUF_CNT; i++) { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 558 | out_be16(&rtx->txbd[i].cbd_sc, BD_ENET_TX_LAST | BD_ENET_TX_TC); |
| 559 | out_be16(&rtx->txbd[i].cbd_datlen, 0); /* Reset */ |
| 560 | out_be32(&rtx->txbd[i].cbd_bufaddr, (uint)txbuf); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 561 | } |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 562 | setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 563 | |
| 564 | /* Set receive and transmit descriptor base |
| 565 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 566 | out_be32(&fecp->fec_r_des_start, (__force unsigned int)rtx->rxbd); |
| 567 | out_be32(&fecp->fec_x_des_start, (__force unsigned int)rtx->txbd); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 568 | |
| 569 | /* Enable MII mode |
| 570 | */ |
| 571 | /* Half duplex mode */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 572 | out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT); |
| 573 | out_be32(&fecp->fec_x_cntrl, 0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 574 | |
| 575 | /* Enable big endian and don't care about SDMA FC. |
| 576 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 577 | out_be32(&fecp->fec_fun_code, 0x78000000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * Setup the pin configuration of the FEC |
| 581 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 582 | fec_pin_init(efis->ether_index); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 583 | |
| 584 | rxIdx = 0; |
| 585 | txIdx = 0; |
| 586 | |
| 587 | /* |
| 588 | * Now enable the transmit and receive processing |
| 589 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 590 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 591 | |
| 592 | if (efis->phy_addr == -1) { |
| 593 | #ifdef CONFIG_SYS_DISCOVER_PHY |
| 594 | /* |
| 595 | * wait for the PHY to wake up after reset |
| 596 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 597 | efis->actual_phy_addr = mii_discover_phy(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 598 | |
| 599 | if (efis->actual_phy_addr == -1) { |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 600 | printf("Unable to discover phy!\n"); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 601 | return -1; |
| 602 | } |
| 603 | #else |
| 604 | efis->actual_phy_addr = -1; |
| 605 | #endif |
| 606 | } else { |
| 607 | efis->actual_phy_addr = efis->phy_addr; |
| 608 | } |
| 609 | |
| 610 | #if defined(CONFIG_MII) && defined(CONFIG_RMII) |
| 611 | /* |
| 612 | * adapt the RMII speed to the speed of the phy |
| 613 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 614 | if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET) |
| 615 | fec_100Mbps(dev); |
| 616 | else |
| 617 | fec_10Mbps(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 618 | #endif |
| 619 | |
| 620 | #if defined(CONFIG_MII) |
| 621 | /* |
| 622 | * adapt to the half/full speed settings |
| 623 | */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 624 | if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL) |
| 625 | fec_full_duplex(dev); |
| 626 | else |
| 627 | fec_half_duplex(dev); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 628 | #endif |
| 629 | |
| 630 | /* And last, try to fill Rx Buffer Descriptors */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 631 | /* Descriptor polling active */ |
| 632 | out_be32(&fecp->fec_r_des_active, 0x01000000); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 633 | |
| 634 | efis->initialized = 1; |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 639 | static void fec_stop(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 640 | { |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 641 | struct ether_fcc_info_s *efis = dev_get_priv(dev); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 642 | fec_t __iomem *fecp = |
| 643 | (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 644 | int i; |
| 645 | |
| 646 | /* avoid halt if initialized; mii gets stuck otherwise */ |
| 647 | if (!efis->initialized) |
| 648 | return; |
| 649 | |
| 650 | /* Whack a reset. |
| 651 | * A delay is required between a reset of the FEC block and |
| 652 | * initialization of other FEC registers because the reset takes |
| 653 | * some time to complete. If you don't delay, subsequent writes |
| 654 | * to FEC registers might get killed by the reset routine which is |
| 655 | * still in progress. |
| 656 | */ |
| 657 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 658 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET); |
| 659 | for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) && |
| 660 | (i < FEC_RESET_DELAY); ++i) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 661 | udelay(1); |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 662 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 663 | if (i == FEC_RESET_DELAY) { |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 664 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 665 | return; |
| 666 | } |
| 667 | |
| 668 | efis->initialized = 0; |
| 669 | } |
| 670 | |
| 671 | #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 672 | |
| 673 | /* Make MII read/write commands for the FEC. |
| 674 | */ |
| 675 | |
| 676 | #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \ |
| 677 | (REG & 0x1f) << 18)) |
| 678 | |
| 679 | #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \ |
| 680 | (REG & 0x1f) << 18) | \ |
| 681 | (VAL & 0xffff)) |
| 682 | |
| 683 | /* Interrupt events/masks. |
| 684 | */ |
| 685 | #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */ |
| 686 | #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */ |
| 687 | #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */ |
| 688 | #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */ |
| 689 | #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */ |
| 690 | #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */ |
| 691 | #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */ |
| 692 | #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */ |
| 693 | #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */ |
| 694 | #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */ |
| 695 | |
| 696 | /* send command to phy using mii, wait for result */ |
| 697 | static uint |
| 698 | mii_send(uint mii_cmd) |
| 699 | { |
| 700 | uint mii_reply; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 701 | fec_t __iomem *ep; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 702 | int cnt; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 703 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 704 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 705 | ep = &immr->im_cpm.cp_fec; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 706 | |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 707 | out_be32(&ep->fec_mii_data, mii_cmd); /* command to phy */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 708 | |
| 709 | /* wait for mii complete */ |
| 710 | cnt = 0; |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 711 | while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) { |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 712 | if (++cnt > 1000) { |
| 713 | printf("mii_send STUCK!\n"); |
| 714 | break; |
| 715 | } |
| 716 | } |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 717 | mii_reply = in_be32(&ep->fec_mii_data); /* result from phy */ |
| 718 | out_be32(&ep->fec_ievent, FEC_ENET_MII); /* clear MII complete */ |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 719 | return mii_reply & 0xffff; /* data read from phy */ |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 720 | } |
| 721 | #endif |
| 722 | |
| 723 | #if defined(CONFIG_SYS_DISCOVER_PHY) |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 724 | static int mii_discover_phy(struct udevice *dev) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 725 | { |
| 726 | #define MAX_PHY_PASSES 11 |
| 727 | uint phyno; |
| 728 | int pass; |
| 729 | uint phytype; |
| 730 | int phyaddr; |
| 731 | |
| 732 | phyaddr = -1; /* didn't find a PHY yet */ |
| 733 | for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) { |
| 734 | if (pass > 1) { |
| 735 | /* PHY may need more time to recover from reset. |
| 736 | * The LXT970 needs 50ms typical, no maximum is |
| 737 | * specified, so wait 10ms before try again. |
| 738 | * With 11 passes this gives it 100ms to wake up. |
| 739 | */ |
| 740 | udelay(10000); /* wait 10ms */ |
| 741 | } |
| 742 | for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) { |
| 743 | phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2)); |
| 744 | if (phytype != 0xffff) { |
| 745 | phyaddr = phyno; |
| 746 | phytype |= mii_send(mk_mii_read(phyno, |
| 747 | MII_PHYSID1)) << 16; |
| 748 | } |
| 749 | } |
| 750 | } |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 751 | if (phyaddr < 0) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 752 | printf("No PHY device found.\n"); |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 753 | |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 754 | return phyaddr; |
| 755 | } |
| 756 | #endif /* CONFIG_SYS_DISCOVER_PHY */ |
| 757 | |
| 758 | #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII) |
| 759 | |
| 760 | /**************************************************************************** |
| 761 | * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet |
| 762 | * This function is a subset of eth_init |
| 763 | **************************************************************************** |
| 764 | */ |
| 765 | static void __mii_init(void) |
| 766 | { |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 767 | immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR; |
| 768 | fec_t __iomem *fecp = &immr->im_cpm.cp_fec; |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 769 | |
| 770 | if (fec_reset(fecp) < 0) |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 771 | printf("FEC_RESET_DELAY timeout\n"); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 772 | |
| 773 | /* We use strictly polling mode only |
| 774 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 775 | out_be32(&fecp->fec_imask, 0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 776 | |
| 777 | /* Clear any pending interrupt |
| 778 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 779 | out_be32(&fecp->fec_ievent, 0xffc0); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 780 | |
| 781 | /* Now enable the transmit and receive processing |
| 782 | */ |
Christophe Leroy | 394f9b3 | 2017-07-06 10:33:13 +0200 | [diff] [blame] | 783 | out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 784 | } |
| 785 | |
Christophe Leroy | 48f896d | 2017-07-06 10:33:17 +0200 | [diff] [blame] | 786 | void mii_init(void) |
Christophe Leroy | 069fa83 | 2017-07-06 10:23:22 +0200 | [diff] [blame] | 787 | { |
| 788 | int i; |
| 789 | |
| 790 | __mii_init(); |
| 791 | |
| 792 | /* Setup the pin configuration of the FEC(s) |
| 793 | */ |
| 794 | for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) |
| 795 | fec_pin_init(ether_fcc_info[i].ether_index); |
| 796 | } |
| 797 | |
| 798 | /***************************************************************************** |
| 799 | * Read and write a MII PHY register, routines used by MII Utilities |
| 800 | * |
| 801 | * FIXME: These routines are expected to return 0 on success, but mii_send |
| 802 | * does _not_ return an error code. Maybe 0xFFFF means error, i.e. |
| 803 | * no PHY connected... |
| 804 | * For now always return 0. |
| 805 | * FIXME: These routines only work after calling eth_init() at least once! |
| 806 | * Otherwise they hang in mii_send() !!! Sorry! |
| 807 | *****************************************************************************/ |
| 808 | |
| 809 | int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 810 | { |
| 811 | unsigned short value = 0; |
| 812 | short rdreg; /* register working value */ |
| 813 | |
| 814 | rdreg = mii_send(mk_mii_read(addr, reg)); |
| 815 | |
| 816 | value = rdreg; |
| 817 | return value; |
| 818 | } |
| 819 | |
| 820 | int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 821 | u16 value) |
| 822 | { |
| 823 | (void)mii_send(mk_mii_write(addr, reg, value)); |
| 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | #endif |
Christophe Leroy | 69ef96d | 2022-05-12 15:48:51 +0200 | [diff] [blame] | 828 | |
| 829 | static const struct eth_ops fec_ops = { |
| 830 | .start = fec_start, |
| 831 | .send = fec_send, |
| 832 | .recv = fec_recv, |
| 833 | .stop = fec_stop, |
| 834 | .free_pkt = fec_free_pkt, |
| 835 | }; |
| 836 | |
| 837 | static const struct udevice_id fec_ids[] = { |
| 838 | #ifdef CONFIG_ETHER_ON_FEC1 |
| 839 | { |
| 840 | .compatible = "fsl,pq1-fec1", |
| 841 | .data = 0, |
| 842 | }, |
| 843 | #endif |
| 844 | #ifdef CONFIG_ETHER_ON_FEC2 |
| 845 | { |
| 846 | .compatible = "fsl,pq1-fec2", |
| 847 | .data = 1, |
| 848 | }, |
| 849 | #endif |
| 850 | { } |
| 851 | }; |
| 852 | |
| 853 | U_BOOT_DRIVER(fec) = { |
| 854 | .name = "fec", |
| 855 | .id = UCLASS_ETH, |
| 856 | .of_match = fec_ids, |
| 857 | .probe = fec_probe, |
| 858 | .ops = &fec_ops, |
| 859 | .priv_auto = sizeof(struct ether_fcc_info_s), |
| 860 | .plat_auto = sizeof(struct eth_pdata), |
| 861 | }; |