9
0
Fork 0
barebox/drivers/net/macb.c

467 lines
11 KiB
C
Raw Normal View History

2008-06-05 16:10:41 +00:00
/*
* Copyright (C) 2005-2006 Atmel Corporation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <common.h>
/*
* The barebox networking stack is a little weird. It seems like the
2008-06-05 16:10:41 +00:00
* networking core allocates receive buffers up front without any
* regard to the hardware that's supposed to actually receive those
* packets.
*
* The MACB receives packets into 128-byte receive buffers, so the
* buffers allocated by the core isn't very practical to use. We'll
* allocate our own, but we need one such buffer in case a packet
* wraps around the DMA ring so that we have to copy it.
*
* Therefore, define CFG_RX_ETH_BUFFER to 1 in the board-specific
* configuration header. This way, the core allocates one RX buffer
* and one TX buffer, each of which can hold a ethernet packet of
* maximum size.
*
* For some reason, the networking core unconditionally specifies a
* 32-byte packet "alignment" (which really should be called
* "padding"). MACB shouldn't need that, but we'll refrain from any
* core modifications here...
*/
#include <net.h>
#include <clock.h>
2008-06-05 16:10:41 +00:00
#include <malloc.h>
#include <xfuncs.h>
#include <init.h>
#include <errno.h>
#include <io.h>
#include <mach/board.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <asm/mmu.h>
#include <linux/phy.h>
2008-06-05 16:10:41 +00:00
#include "macb.h"
#define MACB_RX_BUFFER_SIZE 128
#define RX_BUFFER_MULTIPLE 64 /* bytes */
#define RX_RING_SIZE 32 /* must be power of 2 */
#define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
#define TX_RING_BYTES (sizeof(struct macb_dma_desc))
2008-06-05 16:10:41 +00:00
struct macb_device {
void __iomem *regs;
2008-06-05 16:10:41 +00:00
unsigned int rx_tail;
unsigned int tx_tail;
void *rx_buffer;
void *tx_buffer;
struct macb_dma_desc *rx_ring;
struct macb_dma_desc *tx_ring;
int phy_addr;
const struct device_d *dev;
2008-06-05 16:10:41 +00:00
struct eth_device netdev;
phy_interface_t interface;
struct mii_bus miibus;
unsigned int phy_flags;
2008-06-05 16:10:41 +00:00
};
static int macb_send(struct eth_device *edev, void *packet,
int length)
{
struct macb_device *macb = edev->priv;
unsigned long ctrl;
int ret;
2008-06-05 16:10:41 +00:00
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
ctrl = MACB_BF(TX_FRMLEN, length);
ctrl |= MACB_BIT(TX_LAST) | MACB_BIT(TX_WRAP);
2008-06-05 16:10:41 +00:00
macb->tx_ring[0].ctrl = ctrl;
macb->tx_ring[0].addr = (ulong)packet;
barrier();
dma_flush_range((ulong) packet, (ulong)packet + length);
macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
2008-06-05 16:10:41 +00:00
ret = wait_on_timeout(100 * MSECOND,
!(macb->tx_ring[0].ctrl & MACB_BIT(TX_USED)));
ctrl = macb->tx_ring[0].ctrl;
if (ctrl & MACB_BIT(TX_UNDERRUN))
dev_err(macb->dev, "TX underrun\n");
if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
dev_err(macb->dev, "TX buffers exhausted in mid frame\n");
if (ret)
dev_err(macb->dev,"TX timeout\n");
2008-06-05 16:10:41 +00:00
return ret;
2008-06-05 16:10:41 +00:00
}
static void reclaim_rx_buffers(struct macb_device *macb,
unsigned int new_tail)
{
unsigned int i;
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
i = macb->rx_tail;
while (i > new_tail) {
macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
2008-06-05 16:10:41 +00:00
i++;
if (i > RX_RING_SIZE)
2008-06-05 16:10:41 +00:00
i = 0;
}
while (i < new_tail) {
macb->rx_ring[i].addr &= ~MACB_BIT(RX_USED);
2008-06-05 16:10:41 +00:00
i++;
}
barrier();
macb->rx_tail = new_tail;
}
static int macb_recv(struct eth_device *edev)
{
struct macb_device *macb = edev->priv;
unsigned int rx_tail = macb->rx_tail;
void *buffer;
int length;
int wrapped = 0;
u32 status;
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
for (;;) {
if (!(macb->rx_ring[rx_tail].addr & MACB_BIT(RX_USED)))
2008-06-05 16:10:41 +00:00
return -1;
status = macb->rx_ring[rx_tail].ctrl;
if (status & MACB_BIT(RX_SOF)) {
2008-06-05 16:10:41 +00:00
if (rx_tail != macb->rx_tail)
reclaim_rx_buffers(macb, rx_tail);
wrapped = 0;
}
if (status & MACB_BIT(RX_EOF)) {
buffer = macb->rx_buffer + MACB_RX_BUFFER_SIZE * macb->rx_tail;
length = MACB_BFEXT(RX_FRMLEN, status);
2008-06-05 16:10:41 +00:00
if (wrapped) {
unsigned int headlen, taillen;
headlen = MACB_RX_BUFFER_SIZE * (RX_RING_SIZE
2008-06-05 16:10:41 +00:00
- macb->rx_tail);
taillen = length - headlen;
memcpy((void *)NetRxPackets[0],
buffer, headlen);
memcpy((void *)NetRxPackets[0] + headlen,
macb->rx_buffer, taillen);
buffer = (void *)NetRxPackets[0];
}
net_receive(buffer, length);
if (++rx_tail >= RX_RING_SIZE)
2008-06-05 16:10:41 +00:00
rx_tail = 0;
reclaim_rx_buffers(macb, rx_tail);
} else {
if (++rx_tail >= RX_RING_SIZE) {
2008-06-05 16:10:41 +00:00
wrapped = 1;
rx_tail = 0;
}
}
barrier();
}
return 0;
}
static void macb_adjust_link(struct eth_device *edev)
2008-06-05 16:10:41 +00:00
{
struct macb_device *macb = edev->priv;
u32 reg;
2008-06-05 16:10:41 +00:00
reg = macb_readl(macb, NCFGR);
reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
2008-06-05 16:10:41 +00:00
if (edev->phydev->duplex)
reg |= MACB_BIT(FD);
if (edev->phydev->speed == SPEED_100)
reg |= MACB_BIT(SPD);
2008-06-05 16:10:41 +00:00
macb_writel(macb, NCFGR, reg);
}
2008-06-05 16:10:41 +00:00
static int macb_open(struct eth_device *edev)
{
struct macb_device *macb = edev->priv;
dev_dbg(macb->dev, "%s\n", __func__);
/* Obtain the PHY's address/id */
return phy_device_connect(edev, &macb->miibus, macb->phy_addr,
macb_adjust_link, macb->phy_flags,
macb->interface);
2008-06-05 16:10:41 +00:00
}
static void macb_init(struct macb_device *macb)
2008-06-05 16:10:41 +00:00
{
unsigned long paddr, val = 0;
2008-06-05 16:10:41 +00:00
int i;
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
/*
* macb_halt should have been called at some point before now,
* so we'll assume the controller is idle.
*/
/* initialize DMA descriptors */
paddr = (ulong)macb->rx_buffer;
for (i = 0; i < RX_RING_SIZE; i++) {
2008-06-05 16:10:41 +00:00
macb->rx_ring[i].addr = paddr;
macb->rx_ring[i].ctrl = 0;
paddr += MACB_RX_BUFFER_SIZE;
2008-06-05 16:10:41 +00:00
}
macb->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
2008-06-05 16:10:41 +00:00
macb->tx_ring[0].addr = 0;
macb->tx_ring[0].ctrl = MACB_BIT(TX_USED) | MACB_BIT(TX_WRAP);
2008-06-05 16:10:41 +00:00
macb->rx_tail = macb->tx_tail = 0;
macb_writel(macb, RBQP, (ulong)macb->rx_ring);
macb_writel(macb, TBQP, (ulong)macb->tx_ring);
2008-06-05 16:10:41 +00:00
if (macb->interface == PHY_INTERFACE_MODE_RMII)
val |= MACB_BIT(RMII);
else
val &= ~MACB_BIT(RMII);
2008-06-05 16:10:41 +00:00
#if defined(CONFIG_ARCH_AT91)
val |= MACB_BIT(CLKEN);
2008-06-05 16:10:41 +00:00
#endif
macb_writel(macb, USRIO, val);
2008-06-05 16:10:41 +00:00
/* Enable TX and RX */
macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
2008-06-05 16:10:41 +00:00
}
static void macb_halt(struct eth_device *edev)
{
struct macb_device *macb = edev->priv;
u32 ncr, tsr;
/* Halt the controller and wait for any ongoing transmission to end. */
ncr = macb_readl(macb, NCR);
2008-06-05 16:10:41 +00:00
ncr |= MACB_BIT(THALT);
macb_writel(macb, NCR, ncr);
2008-06-05 16:10:41 +00:00
do {
tsr = macb_readl(macb, TSR);
2008-06-05 16:10:41 +00:00
} while (tsr & MACB_BIT(TGO));
/* Disable TX and RX, and clear statistics */
macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
2008-06-05 16:10:41 +00:00
}
static int macb_phy_read(struct mii_bus *bus, int addr, int reg)
2008-06-05 16:10:41 +00:00
{
struct macb_device *macb = bus->priv;
2008-06-05 16:10:41 +00:00
unsigned long netctl;
unsigned long frame;
int value;
uint64_t start;
2008-06-05 16:10:41 +00:00
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
netctl = macb_readl(macb, NCR);
2008-06-05 16:10:41 +00:00
netctl |= MACB_BIT(MPE);
macb_writel(macb, NCR, netctl);
2008-06-05 16:10:41 +00:00
frame = (MACB_BF(SOF, MACB_MAN_SOF)
| MACB_BF(RW, MACB_MAN_READ)
2008-06-05 16:10:41 +00:00
| MACB_BF(PHYA, addr)
| MACB_BF(REGA, reg)
| MACB_BF(CODE, MACB_MAN_CODE));
macb_writel(macb, MAN, frame);
2008-06-05 16:10:41 +00:00
start = get_time_ns();
2008-06-05 16:10:41 +00:00
do {
if (is_timeout(start, SECOND)) {
dev_err(macb->dev, "phy read timed out\n");
return -1;
}
} while (!MACB_BFEXT(IDLE, macb_readl(macb, NSR)));
2008-06-05 16:10:41 +00:00
frame = macb_readl(macb, MAN);
value = MACB_BFEXT(DATA, frame);
2008-06-05 16:10:41 +00:00
netctl = macb_readl(macb, NCR);
2008-06-05 16:10:41 +00:00
netctl &= ~MACB_BIT(MPE);
macb_writel(macb, NCR, netctl);
2008-06-05 16:10:41 +00:00
return value;
2008-06-05 16:10:41 +00:00
}
static int macb_phy_write(struct mii_bus *bus, int addr, int reg, u16 value)
2008-06-05 16:10:41 +00:00
{
struct macb_device *macb = bus->priv;
2008-06-05 16:10:41 +00:00
unsigned long netctl;
unsigned long frame;
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
netctl = macb_readl(macb, NCR);
2008-06-05 16:10:41 +00:00
netctl |= MACB_BIT(MPE);
macb_writel(macb, NCR, netctl);
2008-06-05 16:10:41 +00:00
frame = (MACB_BF(SOF, MACB_MAN_SOF)
| MACB_BF(RW, MACB_MAN_WRITE)
2008-06-05 16:10:41 +00:00
| MACB_BF(PHYA, addr)
| MACB_BF(REGA, reg)
| MACB_BF(CODE, MACB_MAN_CODE)
2008-06-05 16:10:41 +00:00
| MACB_BF(DATA, value));
macb_writel(macb, MAN, frame);
2008-06-05 16:10:41 +00:00
while (!MACB_BFEXT(IDLE, macb_readl(macb, NSR)))
;
2008-06-05 16:10:41 +00:00
netctl = macb_readl(macb, NCR);
2008-06-05 16:10:41 +00:00
netctl &= ~MACB_BIT(MPE);
macb_writel(macb, NCR, netctl);
2008-06-05 16:10:41 +00:00
return 0;
}
static int macb_get_ethaddr(struct eth_device *edev, unsigned char *adr)
{
struct macb_device *macb = edev->priv;
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
return -1;
}
static int macb_set_ethaddr(struct eth_device *edev, unsigned char *adr)
{
struct macb_device *macb = edev->priv;
2008-06-05 16:10:41 +00:00
dev_dbg(macb->dev, "%s\n", __func__);
2008-06-05 16:10:41 +00:00
/* set hardware address */
macb_writel(macb, SA1B, adr[0] | adr[1] << 8 | adr[2] << 16 | adr[3] << 24);
macb_writel(macb, SA1T, adr[4] | adr[5] << 8);
2008-06-05 16:10:41 +00:00
return 0;
}
static int macb_probe(struct device_d *dev)
{
struct eth_device *edev;
struct macb_device *macb;
unsigned long macb_hz;
u32 ncfgr;
struct at91_ether_platform_data *pdata;
struct clk *pclk;
if (!dev->platform_data) {
dev_err(dev, "macb: no platform_data\n");
return -ENODEV;
}
pdata = dev->platform_data;
2008-06-05 16:10:41 +00:00
edev = xzalloc(sizeof(struct eth_device) + sizeof(struct macb_device));
edev->priv = (struct macb_device *)(edev + 1);
macb = edev->priv;
macb->dev = dev;
2008-06-05 16:10:41 +00:00
edev->open = macb_open;
edev->send = macb_send;
edev->recv = macb_recv;
edev->halt = macb_halt;
edev->get_ethaddr = pdata->get_ethaddr ? pdata->get_ethaddr : macb_get_ethaddr;
2008-06-05 16:10:41 +00:00
edev->set_ethaddr = macb_set_ethaddr;
edev->parent = dev;
2008-06-05 16:10:41 +00:00
macb->miibus.read = macb_phy_read;
macb->miibus.write = macb_phy_write;
macb->phy_addr = pdata->phy_addr;
macb->miibus.priv = macb;
macb->miibus.parent = dev;
if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
macb->interface = PHY_INTERFACE_MODE_MII;
else
macb->interface = pdata->phy_interface;
macb->phy_flags = pdata->phy_flags;
2008-06-05 16:10:41 +00:00
macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE * RX_RING_SIZE);
macb->rx_ring = dma_alloc_coherent(RX_RING_BYTES);
macb->tx_ring = dma_alloc_coherent(TX_RING_BYTES);
2008-06-05 16:10:41 +00:00
macb->regs = dev_request_mem_region(dev, 0);
2008-06-05 16:10:41 +00:00
/*
* Do some basic initialization so that we at least can talk
* to the PHY
*/
pclk = clk_get(dev, "macb_clk");
if (IS_ERR(pclk)) {
dev_err(dev, "no macb_clk\n");
return PTR_ERR(pclk);
}
clk_enable(pclk);
macb_hz = clk_get_rate(pclk);
2008-06-05 16:10:41 +00:00
if (macb_hz < 20000000)
ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
else if (macb_hz < 40000000)
ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
else if (macb_hz < 80000000)
ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
else
ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
macb_writel(macb, NCFGR, ncfgr);
2008-06-05 16:10:41 +00:00
macb_init(macb);
mdiobus_register(&macb->miibus);
2008-06-05 16:10:41 +00:00
eth_register(edev);
return 0;
}
static struct driver_d macb_driver = {
.name = "macb",
.probe = macb_probe,
2008-06-05 16:10:41 +00:00
};
static int macb_driver_init(void)
{
debug("%s\n", __func__);
platform_driver_register(&macb_driver);
return 0;
2008-06-05 16:10:41 +00:00
}
device_initcall(macb_driver_init);