2 * Faraday FTGMAC100 Ethernet
4 * (C) Copyright 2009 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
7 * (C) Copyright 2010 Andes Technology
8 * Macpaul Lin <macpaul@andestech.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/mii.h>
32 #include "ftgmac100.h"
36 #define mdelay(n) ({unsigned long msec = (n); while (msec--) udelay(1000); })
38 /* RBSR - hw default init value is also 0x640 */
39 #define RBSR_DEFAULT_VALUE 0x640
41 /* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
42 #define PKTBUFSTX 4 /* must be power of 2 */
44 struct ftgmac100_data {
45 struct ftgmac100_txdes txdes[PKTBUFSTX];
46 struct ftgmac100_rxdes rxdes[PKTBUFSRX];
53 * struct mii_bus functions
55 static int ftgmac100_mdiobus_read(struct eth_device *dev, int phy_addr,
58 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
62 phycr = readl(&ftgmac100->phycr);
64 /* preserve MDC cycle threshold */
65 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
67 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr)
68 | FTGMAC100_PHYCR_REGAD(regnum)
69 | FTGMAC100_PHYCR_MIIRD;
71 writel(phycr, &ftgmac100->phycr);
73 for (i = 0; i < 10; i++) {
74 phycr = readl(&ftgmac100->phycr);
76 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
79 data = readl(&ftgmac100->phydata);
80 return FTGMAC100_PHYDATA_MIIRDATA(data);
86 debug("mdio read timed out\n");
90 static int ftgmac100_mdiobus_write(struct eth_device *dev, int phy_addr,
91 int regnum, u16 value)
93 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
98 phycr = readl(&ftgmac100->phycr);
100 /* preserve MDC cycle threshold */
101 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
103 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr)
104 | FTGMAC100_PHYCR_REGAD(regnum)
105 | FTGMAC100_PHYCR_MIIWR;
107 data = FTGMAC100_PHYDATA_MIIWDATA(value);
109 writel(data, &ftgmac100->phydata);
110 writel(phycr, &ftgmac100->phycr);
112 for (i = 0; i < 10; i++) {
113 phycr = readl(&ftgmac100->phycr);
115 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0) {
116 debug("(phycr & FTGMAC100_PHYCR_MIIWR) == 0: " \
117 "phy_addr: %x\n", phy_addr);
124 debug("mdio write timed out\n");
128 int ftgmac100_phy_read(struct eth_device *dev, int addr, int reg, u16 *value)
130 *value = ftgmac100_mdiobus_read(dev , addr, reg);
138 int ftgmac100_phy_write(struct eth_device *dev, int addr, int reg, u16 value)
140 if (ftgmac100_mdiobus_write(dev, addr, reg, value) == -1)
146 static int ftgmac100_phy_reset(struct eth_device *dev)
148 struct ftgmac100_data *priv = dev->priv;
152 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
154 ftgmac100_phy_write(dev, priv->phy_addr, MII_ADVERTISE, adv);
156 printf("%s: Starting autonegotiation...\n", dev->name);
158 ftgmac100_phy_write(dev, priv->phy_addr,
159 MII_BMCR, (BMCR_ANENABLE | BMCR_ANRESTART));
161 for (i = 0; i < 100000 / 100; i++) {
162 ftgmac100_phy_read(dev, priv->phy_addr, MII_BMSR, &status);
164 if (status & BMSR_ANEGCOMPLETE)
169 if (status & BMSR_ANEGCOMPLETE) {
170 printf("%s: Autonegotiation complete\n", dev->name);
172 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
180 static int ftgmac100_phy_init(struct eth_device *dev)
182 struct ftgmac100_data *priv = dev->priv;
185 u16 phy_id, status, adv, lpa, stat_ge;
186 int media, speed, duplex;
189 /* Check if the PHY is up to snuff... */
190 for (phy_addr = 0; phy_addr < CONFIG_PHY_MAX_ADDR; phy_addr++) {
192 ftgmac100_phy_read(dev, phy_addr, MII_PHYSID1, &phy_id);
195 * When it is unable to found PHY,
196 * the interface usually return 0xffff or 0x0000
198 if (phy_id != 0xffff && phy_id != 0x0) {
199 printf("%s: found PHY at 0x%02x\n",
200 dev->name, phy_addr);
201 priv->phy_addr = phy_addr;
206 if (phy_id == 0xffff || phy_id == 0x0) {
207 printf("%s: no PHY present\n", dev->name);
211 ftgmac100_phy_read(dev, priv->phy_addr, MII_BMSR, &status);
213 if (!(status & BMSR_LSTATUS)) {
214 /* Try to re-negotiate if we don't have link already. */
215 ftgmac100_phy_reset(dev);
217 for (i = 0; i < 100000 / 100; i++) {
218 ftgmac100_phy_read(dev, priv->phy_addr,
220 if (status & BMSR_LSTATUS)
226 if (!(status & BMSR_LSTATUS)) {
227 printf("%s: link down\n", dev->name);
231 #ifdef CONFIG_FTGMAC100_EGIGA
232 /* 1000 Base-T Status Register */
233 ftgmac100_phy_read(dev, priv->phy_addr,
234 MII_STAT1000, &stat_ge);
236 speed = (stat_ge & (LPA_1000FULL | LPA_1000HALF)
239 duplex = ((stat_ge & LPA_1000FULL)
242 if (speed) { /* Speed is 1000 */
243 printf("%s: link up, 1000bps %s-duplex\n",
244 dev->name, duplex ? "full" : "half");
249 ftgmac100_phy_read(dev, priv->phy_addr, MII_ADVERTISE, &adv);
250 ftgmac100_phy_read(dev, priv->phy_addr, MII_LPA, &lpa);
252 media = mii_nway_result(lpa & adv);
253 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 1 : 0);
254 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
256 printf("%s: link up, %sMbps %s-duplex\n",
257 dev->name, speed ? "100" : "10", duplex ? "full" : "half");
262 static int ftgmac100_update_link_speed(struct eth_device *dev)
264 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
265 struct ftgmac100_data *priv = dev->priv;
267 unsigned short stat_fe;
268 unsigned short stat_ge;
271 #ifdef CONFIG_FTGMAC100_EGIGA
272 /* 1000 Base-T Status Register */
273 ftgmac100_phy_read(dev, priv->phy_addr, MII_STAT1000, &stat_ge);
276 ftgmac100_phy_read(dev, priv->phy_addr, MII_BMSR, &stat_fe);
278 if (!(stat_fe & BMSR_LSTATUS)) /* link status up? */
281 /* read MAC control register and clear related bits */
282 maccr = readl(&ftgmac100->maccr) &
283 ~(FTGMAC100_MACCR_GIGA_MODE |
284 FTGMAC100_MACCR_FAST_MODE |
285 FTGMAC100_MACCR_FULLDUP);
287 #ifdef CONFIG_FTGMAC100_EGIGA
288 if (stat_ge & LPA_1000FULL) {
289 /* set gmac for 1000BaseTX and Full Duplex */
290 maccr |= FTGMAC100_MACCR_GIGA_MODE | FTGMAC100_MACCR_FULLDUP;
293 if (stat_ge & LPA_1000HALF) {
294 /* set gmac for 1000BaseTX and Half Duplex */
295 maccr |= FTGMAC100_MACCR_GIGA_MODE;
299 if (stat_fe & BMSR_100FULL) {
300 /* set MII for 100BaseTX and Full Duplex */
301 maccr |= FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_FULLDUP;
304 if (stat_fe & BMSR_10FULL) {
305 /* set MII for 10BaseT and Full Duplex */
306 maccr |= FTGMAC100_MACCR_FULLDUP;
309 if (stat_fe & BMSR_100HALF) {
310 /* set MII for 100BaseTX and Half Duplex */
311 maccr |= FTGMAC100_MACCR_FAST_MODE;
314 if (stat_fe & BMSR_10HALF) {
315 /* set MII for 10BaseT and Half Duplex */
316 /* we have already clear these bits, do nothing */
320 /* update MII config into maccr */
321 writel(maccr, &ftgmac100->maccr);
329 static void ftgmac100_reset(struct eth_device *dev)
331 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
333 debug("%s()\n", __func__);
335 writel(FTGMAC100_MACCR_SW_RST, &ftgmac100->maccr);
337 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
344 static void ftgmac100_set_mac(struct eth_device *dev,
345 const unsigned char *mac)
347 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
348 unsigned int maddr = mac[0] << 8 | mac[1];
349 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
351 debug("%s(%x %x)\n", __func__, maddr, laddr);
353 writel(maddr, &ftgmac100->mac_madr);
354 writel(laddr, &ftgmac100->mac_ladr);
357 static void ftgmac100_set_mac_from_env(struct eth_device *dev)
359 eth_getenv_enetaddr("ethaddr", dev->enetaddr);
361 ftgmac100_set_mac(dev, dev->enetaddr);
365 * disable transmitter, receiver
367 static void ftgmac100_halt(struct eth_device *dev)
369 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
371 debug("%s()\n", __func__);
373 writel(0, &ftgmac100->maccr);
376 static int ftgmac100_init(struct eth_device *dev, bd_t *bd)
378 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
379 struct ftgmac100_data *priv = dev->priv;
380 struct ftgmac100_txdes *txdes = priv->txdes;
381 struct ftgmac100_rxdes *rxdes = priv->rxdes;
385 debug("%s()\n", __func__);
387 ftgmac100_reset(dev);
389 /* set the ethernet address */
390 ftgmac100_set_mac_from_env(dev);
392 /* disable all interrupts */
393 writel(0, &ftgmac100->ier);
395 /* initialize descriptors */
399 txdes[PKTBUFSTX - 1].txdes0 = FTGMAC100_TXDES0_EDOTR;
400 rxdes[PKTBUFSRX - 1].rxdes0 = FTGMAC100_RXDES0_EDORR;
402 for (i = 0; i < PKTBUFSTX; i++) {
408 for (i = 0; i < PKTBUFSRX; i++) {
410 rxdes[i].rxdes3 = (unsigned int)NetRxPackets[i];
411 rxdes[i].rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
415 writel((unsigned int)txdes, &ftgmac100->txr_badr);
418 writel((unsigned int)rxdes, &ftgmac100->rxr_badr);
420 /* poll receive descriptor automatically */
421 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
423 /* config receive buffer size register */
424 writel(FTGMAC100_RBSR_SIZE(RBSR_DEFAULT_VALUE), &ftgmac100->rbsr);
426 /* enable transmitter, receiver */
427 maccr = FTGMAC100_MACCR_TXMAC_EN |
428 FTGMAC100_MACCR_RXMAC_EN |
429 FTGMAC100_MACCR_TXDMA_EN |
430 FTGMAC100_MACCR_RXDMA_EN |
431 FTGMAC100_MACCR_CRC_APD |
432 FTGMAC100_MACCR_FULLDUP |
433 FTGMAC100_MACCR_RX_RUNT |
434 FTGMAC100_MACCR_RX_BROADPKT;
436 writel(maccr, &ftgmac100->maccr);
438 if (!ftgmac100_phy_init(dev)) {
439 if (!ftgmac100_update_link_speed(dev))
447 * Get a data block via Ethernet
449 static int ftgmac100_recv(struct eth_device *dev)
451 struct ftgmac100_data *priv = dev->priv;
452 struct ftgmac100_rxdes *curr_des;
453 unsigned short rxlen;
455 curr_des = &priv->rxdes[priv->rx_index];
457 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
460 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
461 FTGMAC100_RXDES0_CRC_ERR |
462 FTGMAC100_RXDES0_FTL |
463 FTGMAC100_RXDES0_RUNT |
464 FTGMAC100_RXDES0_RX_ODD_NB)) {
468 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
470 debug("%s(): RX buffer %d, %x received\n",
471 __func__, priv->rx_index, rxlen);
473 /* pass the packet up to the protocol layers. */
474 NetReceive((void *)curr_des->rxdes3, rxlen);
476 /* release buffer to DMA */
477 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
479 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
485 * Send a data block via Ethernet
488 ftgmac100_send(struct eth_device *dev, void *packet, int length)
490 struct ftgmac100 *ftgmac100 = (struct ftgmac100 *)dev->iobase;
491 struct ftgmac100_data *priv = dev->priv;
492 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
495 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
496 debug("%s(): no TX descriptor available\n", __func__);
500 debug("%s(%x, %x)\n", __func__, (int)packet, length);
502 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
504 /* initiate a transmit sequence */
505 curr_des->txdes3 = (unsigned int)packet; /* TXBUF_BADR */
507 /* only one descriptor on TXBUF */
508 curr_des->txdes0 &= FTGMAC100_TXDES0_EDOTR;
509 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
510 FTGMAC100_TXDES0_LTS |
511 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
512 FTGMAC100_TXDES0_TXDMA_OWN ;
515 writel(1, &ftgmac100->txpd);
517 /* wait for transfer to succeed */
518 start = get_timer(0);
519 while (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
520 if (get_timer(0) >= 5) {
521 debug("%s(): timed out\n", __func__);
526 debug("%s(): packet sent\n", __func__);
528 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
533 int ftgmac100_initialize(bd_t *bd)
535 struct eth_device *dev;
536 struct ftgmac100_data *priv;
538 dev = malloc(sizeof *dev);
540 printf("%s(): failed to allocate dev\n", __func__);
544 /* Transmit and receive descriptors should align to 16 bytes */
545 priv = memalign(16, sizeof(struct ftgmac100_data));
547 printf("%s(): failed to allocate priv\n", __func__);
551 memset(dev, 0, sizeof(*dev));
552 memset(priv, 0, sizeof(*priv));
554 sprintf(dev->name, "FTGMAC100");
555 dev->iobase = CONFIG_FTGMAC100_BASE;
556 dev->init = ftgmac100_init;
557 dev->halt = ftgmac100_halt;
558 dev->send = ftgmac100_send;
559 dev->recv = ftgmac100_recv;