2
* Faraday FTMAC100 Ethernet
4
* (C) Copyright 2009 Faraday Technology
5
* Po-Yu Chuang <ratbert@faraday-tech.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32
struct ftmac100_data {
33
volatile struct ftmac100_txdes txdes[1];
34
volatile struct ftmac100_rxdes rxdes[PKTBUFSRX];
41
static void ftmac100_reset (struct eth_device *dev)
43
struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
45
debug ("%s()\n", __func__);
47
writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
49
while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
56
static void ftmac100_set_mac (struct eth_device *dev, const unsigned char *mac)
58
struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
59
unsigned int maddr = mac[0] << 8 | mac[1];
60
unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
62
debug ("%s(%x %x)\n", __func__, maddr, laddr);
64
writel (maddr, &ftmac100->mac_madr);
65
writel (laddr, &ftmac100->mac_ladr);
68
static void ftmac100_set_mac_from_env (struct eth_device *dev)
70
eth_getenv_enetaddr ("ethaddr", dev->enetaddr);
72
ftmac100_set_mac (dev, dev->enetaddr);
76
* disable transmitter, receiver
78
static void ftmac100_halt (struct eth_device *dev)
80
struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
82
debug ("%s()\n", __func__);
84
writel (0, &ftmac100->maccr);
87
static int ftmac100_init (struct eth_device *dev, bd_t *bd)
89
struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
90
struct ftmac100_data *priv = dev->priv;
91
volatile struct ftmac100_txdes *txdes = priv->txdes;
92
volatile struct ftmac100_rxdes *rxdes = priv->rxdes;
96
debug ("%s()\n", __func__);
100
/* set the ethernet address */
102
ftmac100_set_mac_from_env (dev);
104
/* disable all interrupts */
106
writel (0, &ftmac100->imr);
108
/* initialize descriptors */
112
txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
113
rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
115
for (i = 0; i < PKTBUFSRX; i++) {
117
rxdes[i].rxdes2 = (unsigned int)NetRxPackets[i];
118
rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
119
rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
124
writel ((unsigned int)txdes, &ftmac100->txr_badr);
128
writel ((unsigned int)rxdes, &ftmac100->rxr_badr);
130
/* poll receive descriptor automatically */
132
writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
134
/* enable transmitter, receiver */
136
maccr = FTMAC100_MACCR_XMT_EN |
137
FTMAC100_MACCR_RCV_EN |
138
FTMAC100_MACCR_XDMA_EN |
139
FTMAC100_MACCR_RDMA_EN |
140
FTMAC100_MACCR_CRC_APD |
141
FTMAC100_MACCR_ENRX_IN_HALFTX |
142
FTMAC100_MACCR_RX_RUNT |
143
FTMAC100_MACCR_RX_BROADPKT;
145
writel (maccr, &ftmac100->maccr);
151
* Get a data block via Ethernet
153
static int ftmac100_recv (struct eth_device *dev)
155
struct ftmac100_data *priv = dev->priv;
156
volatile struct ftmac100_rxdes *curr_des;
157
unsigned short rxlen;
159
curr_des = &priv->rxdes[priv->rx_index];
161
if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
164
if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
165
FTMAC100_RXDES0_CRC_ERR |
166
FTMAC100_RXDES0_FTL |
167
FTMAC100_RXDES0_RUNT |
168
FTMAC100_RXDES0_RX_ODD_NB)) {
172
rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
174
debug ("%s(): RX buffer %d, %x received\n",
175
__func__, priv->rx_index, rxlen);
177
/* pass the packet up to the protocol layers. */
179
NetReceive ((void *)curr_des->rxdes2, rxlen);
181
/* release buffer to DMA */
183
curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
185
priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
191
* Send a data block via Ethernet
194
ftmac100_send (struct eth_device *dev, volatile void *packet, int length)
196
struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
197
struct ftmac100_data *priv = dev->priv;
198
volatile struct ftmac100_txdes *curr_des = priv->txdes;
201
if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
202
debug ("%s(): no TX descriptor available\n", __func__);
206
debug ("%s(%x, %x)\n", __func__, (int)packet, length);
208
length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
210
/* initiate a transmit sequence */
212
curr_des->txdes2 = (unsigned int)packet; /* TXBUF_BADR */
214
curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
215
curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
216
FTMAC100_TXDES1_LTS |
217
FTMAC100_TXDES1_TXBUF_SIZE (length);
219
curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
223
writel (1, &ftmac100->txpd);
225
/* wait for transfer to succeed */
227
tmo = get_timer (0) + 5 * CONFIG_SYS_HZ;
228
while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
229
if (get_timer (0) >= tmo) {
230
debug ("%s(): timed out\n", __func__);
235
debug ("%s(): packet sent\n", __func__);
240
int ftmac100_initialize (bd_t *bd)
242
struct eth_device *dev;
243
struct ftmac100_data *priv;
245
dev = malloc (sizeof *dev);
247
printf ("%s(): failed to allocate dev\n", __func__);
251
/* Transmit and receive descriptors should align to 16 bytes */
253
priv = memalign (16, sizeof (struct ftmac100_data));
255
printf ("%s(): failed to allocate priv\n", __func__);
259
memset (dev, 0, sizeof (*dev));
260
memset (priv, 0, sizeof (*priv));
262
sprintf (dev->name, "FTMAC100");
263
dev->iobase = CONFIG_FTMAC100_BASE;
264
dev->init = ftmac100_init;
265
dev->halt = ftmac100_halt;
266
dev->send = ftmac100_send;
267
dev->recv = ftmac100_recv;