2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2009-08-10 03:00:00 +00:00
|
|
|
/*
|
|
|
|
* Faraday FTMAC100 Ethernet
|
|
|
|
*
|
|
|
|
* (C) Copyright 2009 Faraday Technology
|
|
|
|
* Po-Yu Chuang <ratbert@faraday-tech.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <common.h>
|
2019-11-14 19:57:39 +00:00
|
|
|
#include <cpu_func.h>
|
2019-08-01 15:46:52 +00:00
|
|
|
#include <env.h>
|
2009-08-10 03:00:00 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <net.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2017-05-23 05:48:27 +00:00
|
|
|
#include <linux/io.h>
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
#include "ftmac100.h"
|
2017-05-23 05:48:27 +00:00
|
|
|
#include <dm.h>
|
2022-11-19 23:45:40 +00:00
|
|
|
|
2017-05-23 05:48:27 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
2022-11-19 23:45:40 +00:00
|
|
|
|
2009-08-10 03:00:00 +00:00
|
|
|
#define ETH_ZLEN 60
|
|
|
|
|
|
|
|
struct ftmac100_data {
|
2011-01-21 07:50:37 +00:00
|
|
|
struct ftmac100_txdes txdes[1];
|
|
|
|
struct ftmac100_rxdes rxdes[PKTBUFSRX];
|
2009-08-10 03:00:00 +00:00
|
|
|
int rx_index;
|
2017-05-23 05:48:27 +00:00
|
|
|
const char *name;
|
|
|
|
phys_addr_t iobase;
|
2009-08-10 03:00:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset MAC
|
|
|
|
*/
|
2017-05-23 05:48:27 +00:00
|
|
|
static void ftmac100_reset(struct ftmac100_data *priv)
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2021-01-31 12:36:02 +00:00
|
|
|
struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
debug ("%s()\n", __func__);
|
|
|
|
|
|
|
|
writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
|
|
|
|
|
|
|
|
while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
|
2017-08-29 02:15:05 +00:00
|
|
|
mdelay(1);
|
|
|
|
/*
|
|
|
|
* When soft reset complete, write mac address immediately maybe fail somehow
|
|
|
|
* Wait for a while can avoid this problem
|
|
|
|
*/
|
|
|
|
mdelay(1);
|
2009-08-10 03:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set MAC address
|
|
|
|
*/
|
2017-05-23 05:48:27 +00:00
|
|
|
static void ftmac100_set_mac(struct ftmac100_data *priv ,
|
|
|
|
const unsigned char *mac)
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2021-01-31 12:36:02 +00:00
|
|
|
struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
|
2009-08-10 03:00:00 +00:00
|
|
|
unsigned int maddr = mac[0] << 8 | mac[1];
|
|
|
|
unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
|
|
|
|
|
|
|
|
debug ("%s(%x %x)\n", __func__, maddr, laddr);
|
|
|
|
|
|
|
|
writel (maddr, &ftmac100->mac_madr);
|
|
|
|
writel (laddr, &ftmac100->mac_ladr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-05-23 05:48:27 +00:00
|
|
|
* Disable MAC
|
2009-08-10 03:00:00 +00:00
|
|
|
*/
|
2017-05-23 05:48:27 +00:00
|
|
|
static void _ftmac100_halt(struct ftmac100_data *priv)
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2021-01-31 12:36:02 +00:00
|
|
|
struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
|
2009-08-10 03:00:00 +00:00
|
|
|
debug ("%s()\n", __func__);
|
|
|
|
writel (0, &ftmac100->maccr);
|
|
|
|
}
|
|
|
|
|
2017-05-23 05:48:27 +00:00
|
|
|
/*
|
|
|
|
* Initialize MAC
|
|
|
|
*/
|
|
|
|
static int _ftmac100_init(struct ftmac100_data *priv, unsigned char enetaddr[6])
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2021-01-31 12:36:02 +00:00
|
|
|
struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
|
2011-01-21 07:50:37 +00:00
|
|
|
struct ftmac100_txdes *txdes = priv->txdes;
|
|
|
|
struct ftmac100_rxdes *rxdes = priv->rxdes;
|
2009-08-10 03:00:00 +00:00
|
|
|
unsigned int maccr;
|
|
|
|
int i;
|
|
|
|
debug ("%s()\n", __func__);
|
|
|
|
|
2017-05-23 05:48:27 +00:00
|
|
|
ftmac100_reset(priv);
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
/* set the ethernet address */
|
2017-05-23 05:48:27 +00:00
|
|
|
ftmac100_set_mac(priv, enetaddr);
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* disable all interrupts */
|
|
|
|
|
|
|
|
writel (0, &ftmac100->imr);
|
|
|
|
|
|
|
|
/* initialize descriptors */
|
|
|
|
|
|
|
|
priv->rx_index = 0;
|
|
|
|
|
|
|
|
txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
|
|
|
|
rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
|
|
|
|
|
|
|
|
for (i = 0; i < PKTBUFSRX; i++) {
|
|
|
|
/* RXBUF_BADR */
|
2018-05-29 02:42:19 +00:00
|
|
|
rxdes[i].rxdes2 = (unsigned int)(unsigned long)net_rx_packets[i];
|
2009-08-10 03:00:00 +00:00
|
|
|
rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
|
|
|
|
rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* transmit ring */
|
|
|
|
|
2018-05-29 02:42:19 +00:00
|
|
|
writel ((unsigned long)txdes, &ftmac100->txr_badr);
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
/* receive ring */
|
|
|
|
|
2018-05-29 02:42:19 +00:00
|
|
|
writel ((unsigned long)rxdes, &ftmac100->rxr_badr);
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
/* poll receive descriptor automatically */
|
|
|
|
|
|
|
|
writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
|
|
|
|
|
|
|
|
/* enable transmitter, receiver */
|
|
|
|
|
|
|
|
maccr = FTMAC100_MACCR_XMT_EN |
|
|
|
|
FTMAC100_MACCR_RCV_EN |
|
|
|
|
FTMAC100_MACCR_XDMA_EN |
|
|
|
|
FTMAC100_MACCR_RDMA_EN |
|
|
|
|
FTMAC100_MACCR_CRC_APD |
|
|
|
|
FTMAC100_MACCR_ENRX_IN_HALFTX |
|
|
|
|
FTMAC100_MACCR_RX_RUNT |
|
|
|
|
FTMAC100_MACCR_RX_BROADPKT;
|
|
|
|
|
|
|
|
writel (maccr, &ftmac100->maccr);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-05-23 05:48:27 +00:00
|
|
|
* Free receiving buffer
|
2009-08-10 03:00:00 +00:00
|
|
|
*/
|
2017-05-23 05:48:27 +00:00
|
|
|
static int _ftmac100_free_pkt(struct ftmac100_data *priv)
|
|
|
|
{
|
|
|
|
struct ftmac100_rxdes *curr_des;
|
|
|
|
curr_des = &priv->rxdes[priv->rx_index];
|
|
|
|
/* release buffer to DMA */
|
|
|
|
curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
|
|
|
|
priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a data block via Ethernet
|
|
|
|
*/
|
|
|
|
static int __ftmac100_recv(struct ftmac100_data *priv)
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2011-01-21 07:50:37 +00:00
|
|
|
struct ftmac100_rxdes *curr_des;
|
2009-08-10 03:00:00 +00:00
|
|
|
unsigned short rxlen;
|
|
|
|
|
|
|
|
curr_des = &priv->rxdes[priv->rx_index];
|
|
|
|
if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
|
2017-05-23 05:48:27 +00:00
|
|
|
return 0;
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
|
|
|
|
FTMAC100_RXDES0_CRC_ERR |
|
|
|
|
FTMAC100_RXDES0_FTL |
|
|
|
|
FTMAC100_RXDES0_RUNT |
|
|
|
|
FTMAC100_RXDES0_RX_ODD_NB)) {
|
2017-05-23 05:48:27 +00:00
|
|
|
return 0;
|
2009-08-10 03:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
|
2017-08-29 02:09:00 +00:00
|
|
|
invalidate_dcache_range(curr_des->rxdes2,curr_des->rxdes2+rxlen);
|
2009-08-10 03:00:00 +00:00
|
|
|
debug ("%s(): RX buffer %d, %x received\n",
|
|
|
|
__func__, priv->rx_index, rxlen);
|
|
|
|
|
2017-05-23 05:48:27 +00:00
|
|
|
return rxlen;
|
2009-08-10 03:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a data block via Ethernet
|
|
|
|
*/
|
2017-05-23 05:48:27 +00:00
|
|
|
static int _ftmac100_send(struct ftmac100_data *priv, void *packet, int length)
|
2009-08-10 03:00:00 +00:00
|
|
|
{
|
2021-01-31 12:36:02 +00:00
|
|
|
struct ftmac100 *ftmac100 = (struct ftmac100 *)(uintptr_t)priv->iobase;
|
2011-01-21 07:50:37 +00:00
|
|
|
struct ftmac100_txdes *curr_des = priv->txdes;
|
2011-01-21 07:51:25 +00:00
|
|
|
ulong start;
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
|
|
|
|
debug ("%s(): no TX descriptor available\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-05-29 02:42:19 +00:00
|
|
|
debug ("%s(%lx, %x)\n", __func__, (unsigned long)packet, length);
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
|
|
|
|
|
|
|
|
/* initiate a transmit sequence */
|
|
|
|
|
2018-05-29 02:42:19 +00:00
|
|
|
flush_dcache_range((unsigned long)packet,(unsigned long)packet+length);
|
|
|
|
curr_des->txdes2 = (unsigned int)(unsigned long)packet; /* TXBUF_BADR */
|
2009-08-10 03:00:00 +00:00
|
|
|
|
|
|
|
curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
|
|
|
|
curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
|
|
|
|
FTMAC100_TXDES1_LTS |
|
|
|
|
FTMAC100_TXDES1_TXBUF_SIZE (length);
|
|
|
|
|
|
|
|
curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
|
|
|
|
|
|
|
|
/* start transmit */
|
|
|
|
|
|
|
|
writel (1, &ftmac100->txpd);
|
|
|
|
|
|
|
|
/* wait for transfer to succeed */
|
|
|
|
|
2011-01-21 07:51:25 +00:00
|
|
|
start = get_timer(0);
|
2009-08-10 03:00:00 +00:00
|
|
|
while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
|
2011-01-21 07:51:25 +00:00
|
|
|
if (get_timer(start) >= 5) {
|
2009-08-10 03:00:00 +00:00
|
|
|
debug ("%s(): timed out\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug ("%s(): packet sent\n", __func__);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-23 05:48:27 +00:00
|
|
|
static int ftmac100_start(struct udevice *dev)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct eth_pdata *plat = dev_get_plat(dev);
|
2017-05-23 05:48:27 +00:00
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return _ftmac100_init(priv, plat->enetaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ftmac100_stop(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
_ftmac100_halt(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ftmac100_send(struct udevice *dev, void *packet, int length)
|
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
ret = _ftmac100_send(priv , packet , length);
|
|
|
|
return ret ? 0 : -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ftmac100_recv(struct udevice *dev, int flags, uchar **packetp)
|
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
struct ftmac100_rxdes *curr_des;
|
|
|
|
curr_des = &priv->rxdes[priv->rx_index];
|
|
|
|
int len;
|
|
|
|
len = __ftmac100_recv(priv);
|
|
|
|
if (len)
|
2018-05-29 02:42:19 +00:00
|
|
|
*packetp = (uchar *)(unsigned long)curr_des->rxdes2;
|
2017-05-23 05:48:27 +00:00
|
|
|
|
|
|
|
return len ? len : -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ftmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
|
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
_ftmac100_free_pkt(priv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ftmac100_read_rom_hwaddr(struct udevice *dev)
|
|
|
|
{
|
2020-12-03 23:55:20 +00:00
|
|
|
struct eth_pdata *pdata = dev_get_plat(dev);
|
2017-08-03 18:22:14 +00:00
|
|
|
eth_env_get_enetaddr("ethaddr", pdata->enetaddr);
|
2017-05-23 05:48:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *dtbmacaddr(u32 ifno)
|
|
|
|
{
|
|
|
|
int node, len;
|
|
|
|
char enet[16];
|
|
|
|
const char *mac;
|
|
|
|
const char *path;
|
|
|
|
if (gd->fdt_blob == NULL) {
|
|
|
|
printf("%s: don't have a valid gd->fdt_blob!\n", __func__);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
node = fdt_path_offset(gd->fdt_blob, "/aliases");
|
|
|
|
if (node < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sprintf(enet, "ethernet%d", ifno);
|
|
|
|
path = fdt_getprop(gd->fdt_blob, node, enet, NULL);
|
|
|
|
if (!path) {
|
|
|
|
printf("no alias for %s\n", enet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
node = fdt_path_offset(gd->fdt_blob, path);
|
|
|
|
mac = fdt_getprop(gd->fdt_blob, node, "mac-address", &len);
|
|
|
|
if (mac && is_valid_ethaddr((u8 *)mac))
|
|
|
|
return mac;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int ftmac100_of_to_plat(struct udevice *dev)
|
2017-05-23 05:48:27 +00:00
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
2020-12-03 23:55:20 +00:00
|
|
|
struct eth_pdata *pdata = dev_get_plat(dev);
|
2017-05-23 05:48:27 +00:00
|
|
|
const char *mac;
|
2020-07-17 05:36:48 +00:00
|
|
|
pdata->iobase = dev_read_addr(dev);
|
2017-05-23 05:48:27 +00:00
|
|
|
priv->iobase = pdata->iobase;
|
|
|
|
mac = dtbmacaddr(0);
|
|
|
|
if (mac)
|
|
|
|
memcpy(pdata->enetaddr , mac , 6);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ftmac100_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct ftmac100_data *priv = dev_get_priv(dev);
|
|
|
|
priv->name = dev->name;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ftmac100_bind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
return device_set_name(dev, dev->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct eth_ops ftmac100_ops = {
|
|
|
|
.start = ftmac100_start,
|
|
|
|
.send = ftmac100_send,
|
|
|
|
.recv = ftmac100_recv,
|
|
|
|
.stop = ftmac100_stop,
|
|
|
|
.free_pkt = ftmac100_free_pkt,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id ftmac100_ids[] = {
|
|
|
|
{ .compatible = "andestech,atmac100" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(ftmac100) = {
|
|
|
|
.name = "nds32_mac",
|
|
|
|
.id = UCLASS_ETH,
|
|
|
|
.of_match = ftmac100_ids,
|
|
|
|
.bind = ftmac100_bind,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = ftmac100_of_to_plat,
|
2017-05-23 05:48:27 +00:00
|
|
|
.probe = ftmac100_probe,
|
|
|
|
.ops = &ftmac100_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct ftmac100_data),
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat_auto = sizeof(struct eth_pdata),
|
2017-05-23 05:48:27 +00:00
|
|
|
.flags = DM_FLAG_ALLOC_PRIV_DMA,
|
|
|
|
};
|