1 /* 2 * Copyright (C) 2005-2006 Atmel Corporation 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 #include <common.h> 19 20 /* 21 * The u-boot networking stack is a little weird. It seems like the 22 * networking core allocates receive buffers up front without any 23 * regard to the hardware that's supposed to actually receive those 24 * packets. 25 * 26 * The MACB receives packets into 128-byte receive buffers, so the 27 * buffers allocated by the core isn't very practical to use. We'll 28 * allocate our own, but we need one such buffer in case a packet 29 * wraps around the DMA ring so that we have to copy it. 30 * 31 * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific 32 * configuration header. This way, the core allocates one RX buffer 33 * and one TX buffer, each of which can hold a ethernet packet of 34 * maximum size. 35 * 36 * For some reason, the networking core unconditionally specifies a 37 * 32-byte packet "alignment" (which really should be called 38 * "padding"). MACB shouldn't need that, but we'll refrain from any 39 * core modifications here... 40 */ 41 42 #include <net.h> 43 #include <netdev.h> 44 #include <malloc.h> 45 #include <miiphy.h> 46 47 #include <linux/mii.h> 48 #include <asm/io.h> 49 #include <asm/dma-mapping.h> 50 #include <asm/arch/clk.h> 51 52 #include "macb.h" 53 54 #define CONFIG_SYS_MACB_RX_BUFFER_SIZE 4096 55 #define CONFIG_SYS_MACB_RX_RING_SIZE (CONFIG_SYS_MACB_RX_BUFFER_SIZE / 128) 56 #define CONFIG_SYS_MACB_TX_RING_SIZE 16 57 #define CONFIG_SYS_MACB_TX_TIMEOUT 1000 58 #define CONFIG_SYS_MACB_AUTONEG_TIMEOUT 5000000 59 60 struct macb_dma_desc { 61 u32 addr; 62 u32 ctrl; 63 }; 64 65 #define RXADDR_USED 0x00000001 66 #define RXADDR_WRAP 0x00000002 67 68 #define RXBUF_FRMLEN_MASK 0x00000fff 69 #define RXBUF_FRAME_START 0x00004000 70 #define RXBUF_FRAME_END 0x00008000 71 #define RXBUF_TYPEID_MATCH 0x00400000 72 #define RXBUF_ADDR4_MATCH 0x00800000 73 #define RXBUF_ADDR3_MATCH 0x01000000 74 #define RXBUF_ADDR2_MATCH 0x02000000 75 #define RXBUF_ADDR1_MATCH 0x04000000 76 #define RXBUF_BROADCAST 0x80000000 77 78 #define TXBUF_FRMLEN_MASK 0x000007ff 79 #define TXBUF_FRAME_END 0x00008000 80 #define TXBUF_NOCRC 0x00010000 81 #define TXBUF_EXHAUSTED 0x08000000 82 #define TXBUF_UNDERRUN 0x10000000 83 #define TXBUF_MAXRETRY 0x20000000 84 #define TXBUF_WRAP 0x40000000 85 #define TXBUF_USED 0x80000000 86 87 struct macb_device { 88 void *regs; 89 90 unsigned int rx_tail; 91 unsigned int tx_head; 92 unsigned int tx_tail; 93 94 void *rx_buffer; 95 void *tx_buffer; 96 struct macb_dma_desc *rx_ring; 97 struct macb_dma_desc *tx_ring; 98 99 unsigned long rx_buffer_dma; 100 unsigned long rx_ring_dma; 101 unsigned long tx_ring_dma; 102 103 const struct device *dev; 104 struct eth_device netdev; 105 unsigned short phy_addr; 106 struct mii_dev *bus; 107 }; 108 #define to_macb(_nd) container_of(_nd, struct macb_device, netdev) 109 110 static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value) 111 { 112 unsigned long netctl; 113 unsigned long netstat; 114 unsigned long frame; 115 116 netctl = macb_readl(macb, NCR); 117 netctl |= MACB_BIT(MPE); 118 macb_writel(macb, NCR, netctl); 119 120 frame = (MACB_BF(SOF, 1) 121 | MACB_BF(RW, 1) 122 | MACB_BF(PHYA, macb->phy_addr) 123 | MACB_BF(REGA, reg) 124 | MACB_BF(CODE, 2) 125 | MACB_BF(DATA, value)); 126 macb_writel(macb, MAN, frame); 127 128 do { 129 netstat = macb_readl(macb, NSR); 130 } while (!(netstat & MACB_BIT(IDLE))); 131 132 netctl = macb_readl(macb, NCR); 133 netctl &= ~MACB_BIT(MPE); 134 macb_writel(macb, NCR, netctl); 135 } 136 137 static u16 macb_mdio_read(struct macb_device *macb, u8 reg) 138 { 139 unsigned long netctl; 140 unsigned long netstat; 141 unsigned long frame; 142 143 netctl = macb_readl(macb, NCR); 144 netctl |= MACB_BIT(MPE); 145 macb_writel(macb, NCR, netctl); 146 147 frame = (MACB_BF(SOF, 1) 148 | MACB_BF(RW, 2) 149 | MACB_BF(PHYA, macb->phy_addr) 150 | MACB_BF(REGA, reg) 151 | MACB_BF(CODE, 2)); 152 macb_writel(macb, MAN, frame); 153 154 do { 155 netstat = macb_readl(macb, NSR); 156 } while (!(netstat & MACB_BIT(IDLE))); 157 158 frame = macb_readl(macb, MAN); 159 160 netctl = macb_readl(macb, NCR); 161 netctl &= ~MACB_BIT(MPE); 162 macb_writel(macb, NCR, netctl); 163 164 return MACB_BFEXT(DATA, frame); 165 } 166 167 void __weak arch_get_mdio_control(const char *name) 168 { 169 return; 170 } 171 172 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) 173 174 int macb_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value) 175 { 176 struct eth_device *dev = eth_get_dev_by_name(devname); 177 struct macb_device *macb = to_macb(dev); 178 179 if ( macb->phy_addr != phy_adr ) 180 return -1; 181 182 arch_get_mdio_control(devname); 183 *value = macb_mdio_read(macb, reg); 184 185 return 0; 186 } 187 188 int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value) 189 { 190 struct eth_device *dev = eth_get_dev_by_name(devname); 191 struct macb_device *macb = to_macb(dev); 192 193 if ( macb->phy_addr != phy_adr ) 194 return -1; 195 196 arch_get_mdio_control(devname); 197 macb_mdio_write(macb, reg, value); 198 199 return 0; 200 } 201 #endif 202 203 204 #if defined(CONFIG_CMD_NET) 205 206 static int macb_send(struct eth_device *netdev, void *packet, int length) 207 { 208 struct macb_device *macb = to_macb(netdev); 209 unsigned long paddr, ctrl; 210 unsigned int tx_head = macb->tx_head; 211 int i; 212 213 paddr = dma_map_single(packet, length, DMA_TO_DEVICE); 214 215 ctrl = length & TXBUF_FRMLEN_MASK; 216 ctrl |= TXBUF_FRAME_END; 217 if (tx_head == (CONFIG_SYS_MACB_TX_RING_SIZE - 1)) { 218 ctrl |= TXBUF_WRAP; 219 macb->tx_head = 0; 220 } else 221 macb->tx_head++; 222 223 macb->tx_ring[tx_head].ctrl = ctrl; 224 macb->tx_ring[tx_head].addr = paddr; 225 barrier(); 226 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART)); 227 228 /* 229 * I guess this is necessary because the networking core may 230 * re-use the transmit buffer as soon as we return... 231 */ 232 for (i = 0; i <= CONFIG_SYS_MACB_TX_TIMEOUT; i++) { 233 barrier(); 234 ctrl = macb->tx_ring[tx_head].ctrl; 235 if (ctrl & TXBUF_USED) 236 break; 237 udelay(1); 238 } 239 240 dma_unmap_single(packet, length, paddr); 241 242 if (i <= CONFIG_SYS_MACB_TX_TIMEOUT) { 243 if (ctrl & TXBUF_UNDERRUN) 244 printf("%s: TX underrun\n", netdev->name); 245 if (ctrl & TXBUF_EXHAUSTED) 246 printf("%s: TX buffers exhausted in mid frame\n", 247 netdev->name); 248 } else { 249 printf("%s: TX timeout\n", netdev->name); 250 } 251 252 /* No one cares anyway */ 253 return 0; 254 } 255 256 static void reclaim_rx_buffers(struct macb_device *macb, 257 unsigned int new_tail) 258 { 259 unsigned int i; 260 261 i = macb->rx_tail; 262 while (i > new_tail) { 263 macb->rx_ring[i].addr &= ~RXADDR_USED; 264 i++; 265 if (i > CONFIG_SYS_MACB_RX_RING_SIZE) 266 i = 0; 267 } 268 269 while (i < new_tail) { 270 macb->rx_ring[i].addr &= ~RXADDR_USED; 271 i++; 272 } 273 274 barrier(); 275 macb->rx_tail = new_tail; 276 } 277 278 static int macb_recv(struct eth_device *netdev) 279 { 280 struct macb_device *macb = to_macb(netdev); 281 unsigned int rx_tail = macb->rx_tail; 282 void *buffer; 283 int length; 284 int wrapped = 0; 285 u32 status; 286 287 for (;;) { 288 if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED)) 289 return -1; 290 291 status = macb->rx_ring[rx_tail].ctrl; 292 if (status & RXBUF_FRAME_START) { 293 if (rx_tail != macb->rx_tail) 294 reclaim_rx_buffers(macb, rx_tail); 295 wrapped = 0; 296 } 297 298 if (status & RXBUF_FRAME_END) { 299 buffer = macb->rx_buffer + 128 * macb->rx_tail; 300 length = status & RXBUF_FRMLEN_MASK; 301 if (wrapped) { 302 unsigned int headlen, taillen; 303 304 headlen = 128 * (CONFIG_SYS_MACB_RX_RING_SIZE 305 - macb->rx_tail); 306 taillen = length - headlen; 307 memcpy((void *)NetRxPackets[0], 308 buffer, headlen); 309 memcpy((void *)NetRxPackets[0] + headlen, 310 macb->rx_buffer, taillen); 311 buffer = (void *)NetRxPackets[0]; 312 } 313 314 NetReceive(buffer, length); 315 if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE) 316 rx_tail = 0; 317 reclaim_rx_buffers(macb, rx_tail); 318 } else { 319 if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE) { 320 wrapped = 1; 321 rx_tail = 0; 322 } 323 } 324 barrier(); 325 } 326 327 return 0; 328 } 329 330 static void macb_phy_reset(struct macb_device *macb) 331 { 332 struct eth_device *netdev = &macb->netdev; 333 int i; 334 u16 status, adv; 335 336 adv = ADVERTISE_CSMA | ADVERTISE_ALL; 337 macb_mdio_write(macb, MII_ADVERTISE, adv); 338 printf("%s: Starting autonegotiation...\n", netdev->name); 339 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE 340 | BMCR_ANRESTART)); 341 342 for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) { 343 status = macb_mdio_read(macb, MII_BMSR); 344 if (status & BMSR_ANEGCOMPLETE) 345 break; 346 udelay(100); 347 } 348 349 if (status & BMSR_ANEGCOMPLETE) 350 printf("%s: Autonegotiation complete\n", netdev->name); 351 else 352 printf("%s: Autonegotiation timed out (status=0x%04x)\n", 353 netdev->name, status); 354 } 355 356 #ifdef CONFIG_MACB_SEARCH_PHY 357 static int macb_phy_find(struct macb_device *macb) 358 { 359 int i; 360 u16 phy_id; 361 362 /* Search for PHY... */ 363 for (i = 0; i < 32; i++) { 364 macb->phy_addr = i; 365 phy_id = macb_mdio_read(macb, MII_PHYSID1); 366 if (phy_id != 0xffff) { 367 printf("%s: PHY present at %d\n", macb->netdev.name, i); 368 return 1; 369 } 370 } 371 372 /* PHY isn't up to snuff */ 373 printf("%s: PHY not found\n", macb->netdev.name); 374 375 return 0; 376 } 377 #endif /* CONFIG_MACB_SEARCH_PHY */ 378 379 380 static int macb_phy_init(struct macb_device *macb) 381 { 382 struct eth_device *netdev = &macb->netdev; 383 #ifdef CONFIG_PHYLIB 384 struct phy_device *phydev; 385 #endif 386 u32 ncfgr; 387 u16 phy_id, status, adv, lpa; 388 int media, speed, duplex; 389 int i; 390 391 arch_get_mdio_control(netdev->name); 392 #ifdef CONFIG_MACB_SEARCH_PHY 393 /* Auto-detect phy_addr */ 394 if (!macb_phy_find(macb)) { 395 return 0; 396 } 397 #endif /* CONFIG_MACB_SEARCH_PHY */ 398 399 /* Check if the PHY is up to snuff... */ 400 phy_id = macb_mdio_read(macb, MII_PHYSID1); 401 if (phy_id == 0xffff) { 402 printf("%s: No PHY present\n", netdev->name); 403 return 0; 404 } 405 406 #ifdef CONFIG_PHYLIB 407 phydev->bus = macb->bus; 408 phydev->dev = netdev; 409 phydev->addr = macb->phy_addr; 410 phy_config(phydev); 411 #endif 412 413 status = macb_mdio_read(macb, MII_BMSR); 414 if (!(status & BMSR_LSTATUS)) { 415 /* Try to re-negotiate if we don't have link already. */ 416 macb_phy_reset(macb); 417 418 for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) { 419 status = macb_mdio_read(macb, MII_BMSR); 420 if (status & BMSR_LSTATUS) 421 break; 422 udelay(100); 423 } 424 } 425 426 if (!(status & BMSR_LSTATUS)) { 427 printf("%s: link down (status: 0x%04x)\n", 428 netdev->name, status); 429 return 0; 430 } else { 431 adv = macb_mdio_read(macb, MII_ADVERTISE); 432 lpa = macb_mdio_read(macb, MII_LPA); 433 media = mii_nway_result(lpa & adv); 434 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) 435 ? 1 : 0); 436 duplex = (media & ADVERTISE_FULL) ? 1 : 0; 437 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n", 438 netdev->name, 439 speed ? "100" : "10", 440 duplex ? "full" : "half", 441 lpa); 442 443 ncfgr = macb_readl(macb, NCFGR); 444 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD)); 445 if (speed) 446 ncfgr |= MACB_BIT(SPD); 447 if (duplex) 448 ncfgr |= MACB_BIT(FD); 449 macb_writel(macb, NCFGR, ncfgr); 450 return 1; 451 } 452 } 453 454 static int macb_init(struct eth_device *netdev, bd_t *bd) 455 { 456 struct macb_device *macb = to_macb(netdev); 457 unsigned long paddr; 458 int i; 459 460 /* 461 * macb_halt should have been called at some point before now, 462 * so we'll assume the controller is idle. 463 */ 464 465 /* initialize DMA descriptors */ 466 paddr = macb->rx_buffer_dma; 467 for (i = 0; i < CONFIG_SYS_MACB_RX_RING_SIZE; i++) { 468 if (i == (CONFIG_SYS_MACB_RX_RING_SIZE - 1)) 469 paddr |= RXADDR_WRAP; 470 macb->rx_ring[i].addr = paddr; 471 macb->rx_ring[i].ctrl = 0; 472 paddr += 128; 473 } 474 for (i = 0; i < CONFIG_SYS_MACB_TX_RING_SIZE; i++) { 475 macb->tx_ring[i].addr = 0; 476 if (i == (CONFIG_SYS_MACB_TX_RING_SIZE - 1)) 477 macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP; 478 else 479 macb->tx_ring[i].ctrl = TXBUF_USED; 480 } 481 macb->rx_tail = macb->tx_head = macb->tx_tail = 0; 482 483 macb_writel(macb, RBQP, macb->rx_ring_dma); 484 macb_writel(macb, TBQP, macb->tx_ring_dma); 485 486 /* choose RMII or MII mode. This depends on the board */ 487 #ifdef CONFIG_RMII 488 #ifdef CONFIG_AT91FAMILY 489 macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN)); 490 #else 491 macb_writel(macb, USRIO, 0); 492 #endif 493 #else 494 #ifdef CONFIG_AT91FAMILY 495 macb_writel(macb, USRIO, MACB_BIT(CLKEN)); 496 #else 497 macb_writel(macb, USRIO, MACB_BIT(MII)); 498 #endif 499 #endif /* CONFIG_RMII */ 500 501 if (!macb_phy_init(macb)) 502 return -1; 503 504 /* Enable TX and RX */ 505 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE)); 506 507 return 0; 508 } 509 510 static void macb_halt(struct eth_device *netdev) 511 { 512 struct macb_device *macb = to_macb(netdev); 513 u32 ncr, tsr; 514 515 /* Halt the controller and wait for any ongoing transmission to end. */ 516 ncr = macb_readl(macb, NCR); 517 ncr |= MACB_BIT(THALT); 518 macb_writel(macb, NCR, ncr); 519 520 do { 521 tsr = macb_readl(macb, TSR); 522 } while (tsr & MACB_BIT(TGO)); 523 524 /* Disable TX and RX, and clear statistics */ 525 macb_writel(macb, NCR, MACB_BIT(CLRSTAT)); 526 } 527 528 static int macb_write_hwaddr(struct eth_device *dev) 529 { 530 struct macb_device *macb = to_macb(dev); 531 u32 hwaddr_bottom; 532 u16 hwaddr_top; 533 534 /* set hardware address */ 535 hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 | 536 dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24; 537 macb_writel(macb, SA1B, hwaddr_bottom); 538 hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8; 539 macb_writel(macb, SA1T, hwaddr_top); 540 return 0; 541 } 542 543 int macb_eth_initialize(int id, void *regs, unsigned int phy_addr) 544 { 545 struct macb_device *macb; 546 struct eth_device *netdev; 547 unsigned long macb_hz; 548 u32 ncfgr; 549 550 macb = malloc(sizeof(struct macb_device)); 551 if (!macb) { 552 printf("Error: Failed to allocate memory for MACB%d\n", id); 553 return -1; 554 } 555 memset(macb, 0, sizeof(struct macb_device)); 556 557 netdev = &macb->netdev; 558 559 macb->rx_buffer = dma_alloc_coherent(CONFIG_SYS_MACB_RX_BUFFER_SIZE, 560 &macb->rx_buffer_dma); 561 macb->rx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_RX_RING_SIZE 562 * sizeof(struct macb_dma_desc), 563 &macb->rx_ring_dma); 564 macb->tx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_TX_RING_SIZE 565 * sizeof(struct macb_dma_desc), 566 &macb->tx_ring_dma); 567 568 macb->regs = regs; 569 macb->phy_addr = phy_addr; 570 571 sprintf(netdev->name, "macb%d", id); 572 netdev->init = macb_init; 573 netdev->halt = macb_halt; 574 netdev->send = macb_send; 575 netdev->recv = macb_recv; 576 netdev->write_hwaddr = macb_write_hwaddr; 577 578 /* 579 * Do some basic initialization so that we at least can talk 580 * to the PHY 581 */ 582 macb_hz = get_macb_pclk_rate(id); 583 if (macb_hz < 20000000) 584 ncfgr = MACB_BF(CLK, MACB_CLK_DIV8); 585 else if (macb_hz < 40000000) 586 ncfgr = MACB_BF(CLK, MACB_CLK_DIV16); 587 else if (macb_hz < 80000000) 588 ncfgr = MACB_BF(CLK, MACB_CLK_DIV32); 589 else 590 ncfgr = MACB_BF(CLK, MACB_CLK_DIV64); 591 592 macb_writel(macb, NCFGR, ncfgr); 593 594 eth_register(netdev); 595 596 #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) 597 miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write); 598 macb->bus = miiphy_get_dev_by_name(netdev->name); 599 #endif 600 return 0; 601 } 602 603 #endif 604