/*
* Faraday FTMAC100 10/100 Ethernet
*
* (C) Copyright 2009-2011 Faraday Technology
* Po-Yu Chuang <ratbert@faraday-tech.com>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/dma-mapping.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/netdevice.h>
#include <linux/platform_device.h>
#include "ftmac100.h"
#define DRV_NAME "ftmac100"
#define DRV_VERSION "0.2"
#define RX_QUEUE_ENTRIES 128 /* must be power of 2 */
#define TX_QUEUE_ENTRIES 16 /* must be power of 2 */
#define MAX_PKT_SIZE 1518
#define RX_BUF_SIZE 2044 /* must be smaller than 0x7ff */
#if MAX_PKT_SIZE > 0x7ff
#error invalid MAX_PKT_SIZE
#endif
#if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
#error invalid RX_BUF_SIZE
#endif
/******************************************************************************
* private data
*****************************************************************************/
struct ftmac100_descs {
struct ftmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
struct ftmac100_txdes txdes[TX_QUEUE_ENTRIES];
};
struct ftmac100 {
struct resource *res;
void __iomem *base;
int irq;
struct ftmac100_descs *descs;
dma_addr_t descs_dma_addr;
unsigned int rx_pointer;
unsigned int tx_clean_pointer;
unsigned int tx_pointer;
unsigned int tx_pending;
spinlock_t tx_lock;
struct net_device *netdev;
struct device *dev;
struct napi_struct napi;
struct mii_if_info mii;
};
static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
struct ftmac100_rxdes *rxdes, gfp_t gfp);
/******************************************************************************
* internal functions (hardware register access)
*****************************************************************************/
#define INT_MASK_ALL_ENABLED (FTMAC100_INT_RPKT_FINISH | \
FTMAC100_INT_NORXBUF | \
FTMAC100_INT_XPKT_OK | \
FTMAC100_INT_XPKT_LOST | \
FTMAC100_INT_RPKT_LOST | \
FTMAC100_INT_AHB_ERR | \
FTMAC100_INT_PHYSTS_CHG)
#define INT_MASK_ALL_DISABLED 0
static void ftmac100_enable_all_int(struct ftmac100 *priv)
{
iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTMAC100_OFFSET_IMR);
}
static void ftmac100_disable_all_int(struct ftmac100 *priv)
{
iowrite32(INT_MASK_ALL_DISABLED, priv->base + FTMAC100_OFFSET_IMR);
}
static void ftmac100_set_rx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
{
iowrite32(addr, priv->base + FTMAC100_OFFSET_RXR_BADR);
}
static void ftmac100_set_tx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
{
iowrite32(addr, priv->base + FTMAC100_OFFSET_TXR_BADR);
}
static void ftmac100_txdma_start_polling(struct ftmac100 *priv)
{
iowrite32(1, priv->base + FTMAC100_OFFSET_TXPD);
}
static int ftmac100_reset(struct ftmac100 *priv)
{
struct net_device *netdev = priv->netdev;
int i;
/* NOTE: reset clears all registers */
iowrite32(FTMAC100_MACCR_SW_RST, priv->base + FTMAC100_OFFSET_MACCR);
for (i = 0; i < 5; i++) {
unsigned int maccr;
maccr = ioread32(priv->base + FTMAC100_OFFSET_MACCR);
if (!(maccr & FTMAC100_MACCR_SW_RST)) {
/*
* FTMAC100_MACCR_SW_RST cleared does not indicate
* that hardware reset completed (what the f*ck).
* We still need to wait for a while.
*/
udelay(