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