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 struct ftmac100_txdes txdes[1];
34 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 struct ftmac100_txdes *txdes = priv->txdes;
92 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 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
193 static int ftmac100_send(struct eth_device *dev, void *packet, int length)
195 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
196 struct ftmac100_data *priv = dev->priv;
197 struct ftmac100_txdes *curr_des = priv->txdes;
200 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
201 debug ("%s(): no TX descriptor available\n", __func__);
205 debug ("%s(%x, %x)\n", __func__, (int)packet, length);
207 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
209 /* initiate a transmit sequence */
211 curr_des->txdes2 = (unsigned int)packet; /* TXBUF_BADR */
213 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
214 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
215 FTMAC100_TXDES1_LTS |
216 FTMAC100_TXDES1_TXBUF_SIZE (length);
218 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
222 writel (1, &ftmac100->txpd);
224 /* wait for transfer to succeed */
226 start = get_timer(0);
227 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
228 if (get_timer(start) >= 5) {
229 debug ("%s(): timed out\n", __func__);
234 debug ("%s(): packet sent\n", __func__);
239 int ftmac100_initialize (bd_t *bd)
241 struct eth_device *dev;
242 struct ftmac100_data *priv;
244 dev = malloc (sizeof *dev);
246 printf ("%s(): failed to allocate dev\n", __func__);
250 /* Transmit and receive descriptors should align to 16 bytes */
252 priv = memalign (16, sizeof (struct ftmac100_data));
254 printf ("%s(): failed to allocate priv\n", __func__);
258 memset (dev, 0, sizeof (*dev));
259 memset (priv, 0, sizeof (*priv));
261 sprintf (dev->name, "FTMAC100");
262 dev->iobase = CONFIG_FTMAC100_BASE;
263 dev->init = ftmac100_init;
264 dev->halt = ftmac100_halt;
265 dev->send = ftmac100_send;
266 dev->recv = ftmac100_recv;