2 * Faraday FTMAC100 Ethernet
4 * (C) Copyright 2009 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
7 * SPDX-License-Identifier: GPL-2.0+
20 struct ftmac100_data {
21 struct ftmac100_txdes txdes[1];
22 struct ftmac100_rxdes rxdes[PKTBUFSRX];
29 static void ftmac100_reset (struct eth_device *dev)
31 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
33 debug ("%s()\n", __func__);
35 writel (FTMAC100_MACCR_SW_RST, &ftmac100->maccr);
37 while (readl (&ftmac100->maccr) & FTMAC100_MACCR_SW_RST)
44 static void ftmac100_set_mac (struct eth_device *dev, const unsigned char *mac)
46 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
47 unsigned int maddr = mac[0] << 8 | mac[1];
48 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
50 debug ("%s(%x %x)\n", __func__, maddr, laddr);
52 writel (maddr, &ftmac100->mac_madr);
53 writel (laddr, &ftmac100->mac_ladr);
56 static void ftmac100_set_mac_from_env (struct eth_device *dev)
58 eth_getenv_enetaddr ("ethaddr", dev->enetaddr);
60 ftmac100_set_mac (dev, dev->enetaddr);
64 * disable transmitter, receiver
66 static void ftmac100_halt (struct eth_device *dev)
68 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
70 debug ("%s()\n", __func__);
72 writel (0, &ftmac100->maccr);
75 static int ftmac100_init (struct eth_device *dev, bd_t *bd)
77 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
78 struct ftmac100_data *priv = dev->priv;
79 struct ftmac100_txdes *txdes = priv->txdes;
80 struct ftmac100_rxdes *rxdes = priv->rxdes;
84 debug ("%s()\n", __func__);
88 /* set the ethernet address */
90 ftmac100_set_mac_from_env (dev);
92 /* disable all interrupts */
94 writel (0, &ftmac100->imr);
96 /* initialize descriptors */
100 txdes[0].txdes1 = FTMAC100_TXDES1_EDOTR;
101 rxdes[PKTBUFSRX - 1].rxdes1 = FTMAC100_RXDES1_EDORR;
103 for (i = 0; i < PKTBUFSRX; i++) {
105 rxdes[i].rxdes2 = (unsigned int)net_rx_packets[i];
106 rxdes[i].rxdes1 |= FTMAC100_RXDES1_RXBUF_SIZE (PKTSIZE_ALIGN);
107 rxdes[i].rxdes0 = FTMAC100_RXDES0_RXDMA_OWN;
112 writel ((unsigned int)txdes, &ftmac100->txr_badr);
116 writel ((unsigned int)rxdes, &ftmac100->rxr_badr);
118 /* poll receive descriptor automatically */
120 writel (FTMAC100_APTC_RXPOLL_CNT (1), &ftmac100->aptc);
122 /* enable transmitter, receiver */
124 maccr = FTMAC100_MACCR_XMT_EN |
125 FTMAC100_MACCR_RCV_EN |
126 FTMAC100_MACCR_XDMA_EN |
127 FTMAC100_MACCR_RDMA_EN |
128 FTMAC100_MACCR_CRC_APD |
129 FTMAC100_MACCR_ENRX_IN_HALFTX |
130 FTMAC100_MACCR_RX_RUNT |
131 FTMAC100_MACCR_RX_BROADPKT;
133 writel (maccr, &ftmac100->maccr);
139 * Get a data block via Ethernet
141 static int ftmac100_recv (struct eth_device *dev)
143 struct ftmac100_data *priv = dev->priv;
144 struct ftmac100_rxdes *curr_des;
145 unsigned short rxlen;
147 curr_des = &priv->rxdes[priv->rx_index];
149 if (curr_des->rxdes0 & FTMAC100_RXDES0_RXDMA_OWN)
152 if (curr_des->rxdes0 & (FTMAC100_RXDES0_RX_ERR |
153 FTMAC100_RXDES0_CRC_ERR |
154 FTMAC100_RXDES0_FTL |
155 FTMAC100_RXDES0_RUNT |
156 FTMAC100_RXDES0_RX_ODD_NB)) {
160 rxlen = FTMAC100_RXDES0_RFL (curr_des->rxdes0);
162 debug ("%s(): RX buffer %d, %x received\n",
163 __func__, priv->rx_index, rxlen);
165 /* pass the packet up to the protocol layers. */
167 net_process_received_packet((void *)curr_des->rxdes2, rxlen);
169 /* release buffer to DMA */
171 curr_des->rxdes0 |= FTMAC100_RXDES0_RXDMA_OWN;
173 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
179 * Send a data block via Ethernet
181 static int ftmac100_send(struct eth_device *dev, void *packet, int length)
183 struct ftmac100 *ftmac100 = (struct ftmac100 *)dev->iobase;
184 struct ftmac100_data *priv = dev->priv;
185 struct ftmac100_txdes *curr_des = priv->txdes;
188 if (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
189 debug ("%s(): no TX descriptor available\n", __func__);
193 debug ("%s(%x, %x)\n", __func__, (int)packet, length);
195 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
197 /* initiate a transmit sequence */
199 curr_des->txdes2 = (unsigned int)packet; /* TXBUF_BADR */
201 curr_des->txdes1 &= FTMAC100_TXDES1_EDOTR;
202 curr_des->txdes1 |= FTMAC100_TXDES1_FTS |
203 FTMAC100_TXDES1_LTS |
204 FTMAC100_TXDES1_TXBUF_SIZE (length);
206 curr_des->txdes0 = FTMAC100_TXDES0_TXDMA_OWN;
210 writel (1, &ftmac100->txpd);
212 /* wait for transfer to succeed */
214 start = get_timer(0);
215 while (curr_des->txdes0 & FTMAC100_TXDES0_TXDMA_OWN) {
216 if (get_timer(start) >= 5) {
217 debug ("%s(): timed out\n", __func__);
222 debug ("%s(): packet sent\n", __func__);
227 int ftmac100_initialize (bd_t *bd)
229 struct eth_device *dev;
230 struct ftmac100_data *priv;
232 dev = malloc (sizeof *dev);
234 printf ("%s(): failed to allocate dev\n", __func__);
238 /* Transmit and receive descriptors should align to 16 bytes */
240 priv = memalign (16, sizeof (struct ftmac100_data));
242 printf ("%s(): failed to allocate priv\n", __func__);
246 memset (dev, 0, sizeof (*dev));
247 memset (priv, 0, sizeof (*priv));
249 strcpy(dev->name, "FTMAC100");
250 dev->iobase = CONFIG_FTMAC100_BASE;
251 dev->init = ftmac100_init;
252 dev->halt = ftmac100_halt;
253 dev->send = ftmac100_send;
254 dev->recv = ftmac100_recv;