blob: 78cd9df6298677f8d0cf0ae0007f283951b7fe4a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Lin199c6252010-12-21 16:59:46 +08002/*
3 * Faraday FTGMAC100 Ethernet
4 *
5 * (C) Copyright 2009 Faraday Technology
6 * Po-Yu Chuang <ratbert@faraday-tech.com>
7 *
8 * (C) Copyright 2010 Andes Technology
9 * Macpaul Lin <macpaul@andestech.com>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010010 *
11 * Copyright (C) 2018, IBM Corporation.
Macpaul Lin199c6252010-12-21 16:59:46 +080012 */
13
Cédric Le Goater38b33e92018-10-29 07:06:31 +010014#include <dm.h>
15#include <miiphy.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080016#include <malloc.h>
17#include <net.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010018#include <linux/io.h>
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +080019#include <asm/dma-mapping.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080020
21#include "ftgmac100.h"
22
23#define ETH_ZLEN 60
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +080024#define CFG_XBUF_SIZE 1536
Macpaul Lin199c6252010-12-21 16:59:46 +080025
Macpaul Lin199c6252010-12-21 16:59:46 +080026/* RBSR - hw default init value is also 0x640 */
27#define RBSR_DEFAULT_VALUE 0x640
28
29/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
30#define PKTBUFSTX 4 /* must be power of 2 */
31
Cédric Le Goater38b33e92018-10-29 07:06:31 +010032/**
33 * struct ftgmac100_data - private data for the FTGMAC100 driver
34 *
35 * @iobase: The base address of the hardware registers
36 * @txdes: The array of transmit descriptors
37 * @rxdes: The array of receive descriptors
38 * @tx_index: Transmit descriptor index in @txdes
39 * @rx_index: Receive descriptor index in @rxdes
40 * @phy_addr: The PHY interface address to use
41 */
Macpaul Lin199c6252010-12-21 16:59:46 +080042struct ftgmac100_data {
Cédric Le Goater38b33e92018-10-29 07:06:31 +010043 struct ftgmac100 *iobase;
44
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +080045 ulong txdes_dma;
46 struct ftgmac100_txdes *txdes;
47 ulong rxdes_dma;
48 struct ftgmac100_rxdes *rxdes;
Macpaul Lin199c6252010-12-21 16:59:46 +080049 int tx_index;
50 int rx_index;
51 int phy_addr;
52};
53
54/*
55 * struct mii_bus functions
56 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +010057static int ftgmac100_mdiobus_read(struct ftgmac100_data *priv, int phy_addr,
58 int regnum)
Macpaul Lin199c6252010-12-21 16:59:46 +080059{
Cédric Le Goater38b33e92018-10-29 07:06:31 +010060 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +080061 int phycr;
62 int i;
63
64 phycr = readl(&ftgmac100->phycr);
65
66 /* preserve MDC cycle threshold */
67 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
68
69 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr)
70 | FTGMAC100_PHYCR_REGAD(regnum)
71 | FTGMAC100_PHYCR_MIIRD;
72
73 writel(phycr, &ftgmac100->phycr);
74
75 for (i = 0; i < 10; i++) {
76 phycr = readl(&ftgmac100->phycr);
77
78 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
79 int data;
80
81 data = readl(&ftgmac100->phydata);
82 return FTGMAC100_PHYDATA_MIIRDATA(data);
83 }
84
85 mdelay(10);
86 }
87
88 debug("mdio read timed out\n");
89 return -1;
90}
91
Cédric Le Goater38b33e92018-10-29 07:06:31 +010092static int ftgmac100_mdiobus_write(struct ftgmac100_data *priv, int phy_addr,
93 int regnum, u16 value)
Macpaul Lin199c6252010-12-21 16:59:46 +080094{
Cédric Le Goater38b33e92018-10-29 07:06:31 +010095 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +080096 int phycr;
97 int data;
98 int i;
99
100 phycr = readl(&ftgmac100->phycr);
101
102 /* preserve MDC cycle threshold */
103 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
104
105 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr)
106 | FTGMAC100_PHYCR_REGAD(regnum)
107 | FTGMAC100_PHYCR_MIIWR;
108
109 data = FTGMAC100_PHYDATA_MIIWDATA(value);
110
111 writel(data, &ftgmac100->phydata);
112 writel(phycr, &ftgmac100->phycr);
113
114 for (i = 0; i < 10; i++) {
115 phycr = readl(&ftgmac100->phycr);
116
117 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0) {
118 debug("(phycr & FTGMAC100_PHYCR_MIIWR) == 0: " \
119 "phy_addr: %x\n", phy_addr);
120 return 0;
121 }
122
123 mdelay(1);
124 }
125
126 debug("mdio write timed out\n");
127 return -1;
128}
129
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100130int ftgmac100_phy_read(struct ftgmac100_data *priv, int addr, int reg,
131 u16 *value)
Macpaul Lin199c6252010-12-21 16:59:46 +0800132{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100133 *value = ftgmac100_mdiobus_read(priv, addr, reg);
Macpaul Lin199c6252010-12-21 16:59:46 +0800134
135 if (*value == -1)
136 return -1;
137
138 return 0;
139}
140
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100141int ftgmac100_phy_write(struct ftgmac100_data *priv, int addr, int reg,
142 u16 value)
Macpaul Lin199c6252010-12-21 16:59:46 +0800143{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100144 if (ftgmac100_mdiobus_write(priv, addr, reg, value) == -1)
Macpaul Lin199c6252010-12-21 16:59:46 +0800145 return -1;
146
147 return 0;
148}
149
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100150static int ftgmac100_phy_reset(struct ftgmac100_data *priv, struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800151{
Macpaul Lin199c6252010-12-21 16:59:46 +0800152 int i;
153 u16 status, adv;
154
155 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
156
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100157 ftgmac100_phy_write(priv, priv->phy_addr, MII_ADVERTISE, adv);
Macpaul Lin199c6252010-12-21 16:59:46 +0800158
159 printf("%s: Starting autonegotiation...\n", dev->name);
160
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100161 ftgmac100_phy_write(priv, priv->phy_addr,
162 MII_BMCR, (BMCR_ANENABLE | BMCR_ANRESTART));
Macpaul Lin199c6252010-12-21 16:59:46 +0800163
164 for (i = 0; i < 100000 / 100; i++) {
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100165 ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &status);
Macpaul Lin199c6252010-12-21 16:59:46 +0800166
167 if (status & BMSR_ANEGCOMPLETE)
168 break;
169 mdelay(1);
170 }
171
172 if (status & BMSR_ANEGCOMPLETE) {
173 printf("%s: Autonegotiation complete\n", dev->name);
174 } else {
175 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
176 dev->name, status);
177 return 0;
178 }
179
180 return 1;
181}
182
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100183static int ftgmac100_phy_init(struct ftgmac100_data *priv, struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800184{
Macpaul Lin199c6252010-12-21 16:59:46 +0800185 int phy_addr;
186 u16 phy_id, status, adv, lpa, stat_ge;
187 int media, speed, duplex;
188 int i;
189
190 /* Check if the PHY is up to snuff... */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100191 for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
Macpaul Lin199c6252010-12-21 16:59:46 +0800192
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100193 ftgmac100_phy_read(priv, phy_addr, MII_PHYSID1, &phy_id);
Macpaul Lin199c6252010-12-21 16:59:46 +0800194
195 /*
196 * When it is unable to found PHY,
197 * the interface usually return 0xffff or 0x0000
198 */
199 if (phy_id != 0xffff && phy_id != 0x0) {
200 printf("%s: found PHY at 0x%02x\n",
201 dev->name, phy_addr);
202 priv->phy_addr = phy_addr;
203 break;
204 }
205 }
206
207 if (phy_id == 0xffff || phy_id == 0x0) {
208 printf("%s: no PHY present\n", dev->name);
209 return 0;
210 }
211
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100212 ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &status);
Macpaul Lin199c6252010-12-21 16:59:46 +0800213
214 if (!(status & BMSR_LSTATUS)) {
215 /* Try to re-negotiate if we don't have link already. */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100216 ftgmac100_phy_reset(priv, dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800217
218 for (i = 0; i < 100000 / 100; i++) {
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100219 ftgmac100_phy_read(priv, priv->phy_addr,
220 MII_BMSR, &status);
Macpaul Lin199c6252010-12-21 16:59:46 +0800221 if (status & BMSR_LSTATUS)
222 break;
223 udelay(100);
224 }
225 }
226
227 if (!(status & BMSR_LSTATUS)) {
228 printf("%s: link down\n", dev->name);
229 return 0;
230 }
231
232#ifdef CONFIG_FTGMAC100_EGIGA
233 /* 1000 Base-T Status Register */
234 ftgmac100_phy_read(dev, priv->phy_addr,
235 MII_STAT1000, &stat_ge);
236
237 speed = (stat_ge & (LPA_1000FULL | LPA_1000HALF)
238 ? 1 : 0);
239
240 duplex = ((stat_ge & LPA_1000FULL)
241 ? 1 : 0);
242
243 if (speed) { /* Speed is 1000 */
244 printf("%s: link up, 1000bps %s-duplex\n",
245 dev->name, duplex ? "full" : "half");
246 return 0;
247 }
248#endif
249
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100250 ftgmac100_phy_read(priv, priv->phy_addr, MII_ADVERTISE, &adv);
251 ftgmac100_phy_read(priv, priv->phy_addr, MII_LPA, &lpa);
Macpaul Lin199c6252010-12-21 16:59:46 +0800252
253 media = mii_nway_result(lpa & adv);
254 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 1 : 0);
255 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
256
257 printf("%s: link up, %sMbps %s-duplex\n",
258 dev->name, speed ? "100" : "10", duplex ? "full" : "half");
259
260 return 1;
261}
262
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100263static int ftgmac100_update_link_speed(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800264{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100265 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800266 unsigned short stat_fe;
267 unsigned short stat_ge;
268 unsigned int maccr;
269
270#ifdef CONFIG_FTGMAC100_EGIGA
271 /* 1000 Base-T Status Register */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100272 ftgmac100_phy_read(priv, priv->phy_addr, MII_STAT1000, &stat_ge);
Macpaul Lin199c6252010-12-21 16:59:46 +0800273#endif
274
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100275 ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &stat_fe);
Macpaul Lin199c6252010-12-21 16:59:46 +0800276
277 if (!(stat_fe & BMSR_LSTATUS)) /* link status up? */
278 return 0;
279
280 /* read MAC control register and clear related bits */
281 maccr = readl(&ftgmac100->maccr) &
282 ~(FTGMAC100_MACCR_GIGA_MODE |
283 FTGMAC100_MACCR_FAST_MODE |
284 FTGMAC100_MACCR_FULLDUP);
285
286#ifdef CONFIG_FTGMAC100_EGIGA
287 if (stat_ge & LPA_1000FULL) {
288 /* set gmac for 1000BaseTX and Full Duplex */
289 maccr |= FTGMAC100_MACCR_GIGA_MODE | FTGMAC100_MACCR_FULLDUP;
290 }
291
292 if (stat_ge & LPA_1000HALF) {
293 /* set gmac for 1000BaseTX and Half Duplex */
294 maccr |= FTGMAC100_MACCR_GIGA_MODE;
295 }
296#endif
297
298 if (stat_fe & BMSR_100FULL) {
299 /* set MII for 100BaseTX and Full Duplex */
300 maccr |= FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_FULLDUP;
301 }
302
303 if (stat_fe & BMSR_10FULL) {
304 /* set MII for 10BaseT and Full Duplex */
305 maccr |= FTGMAC100_MACCR_FULLDUP;
306 }
307
308 if (stat_fe & BMSR_100HALF) {
309 /* set MII for 100BaseTX and Half Duplex */
310 maccr |= FTGMAC100_MACCR_FAST_MODE;
311 }
312
313 if (stat_fe & BMSR_10HALF) {
314 /* set MII for 10BaseT and Half Duplex */
315 /* we have already clear these bits, do nothing */
316 ;
317 }
318
319 /* update MII config into maccr */
320 writel(maccr, &ftgmac100->maccr);
321
322 return 1;
323}
324
325/*
326 * Reset MAC
327 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100328static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800329{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100330 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800331
332 debug("%s()\n", __func__);
333
Cédric Le Goatercef951c2018-10-29 07:06:32 +0100334 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Lin199c6252010-12-21 16:59:46 +0800335
336 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
337 ;
338}
339
340/*
341 * Set MAC address
342 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100343static int ftgmac100_set_mac(struct ftgmac100_data *priv,
344 const unsigned char *mac)
Macpaul Lin199c6252010-12-21 16:59:46 +0800345{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100346 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800347 unsigned int maddr = mac[0] << 8 | mac[1];
348 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
349
350 debug("%s(%x %x)\n", __func__, maddr, laddr);
351
352 writel(maddr, &ftgmac100->mac_madr);
353 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800354
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100355 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800356}
357
358/*
359 * disable transmitter, receiver
360 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100361static void ftgmac100_stop(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800362{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100363 struct ftgmac100_data *priv = dev_get_priv(dev);
364 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800365
366 debug("%s()\n", __func__);
367
368 writel(0, &ftgmac100->maccr);
369}
370
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100371static int ftgmac100_start(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800372{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100373 struct eth_pdata *plat = dev_get_platdata(dev);
374 struct ftgmac100_data *priv = dev_get_priv(dev);
375 struct ftgmac100 *ftgmac100 = priv->iobase;
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800376 struct ftgmac100_txdes *txdes;
377 struct ftgmac100_rxdes *rxdes;
Macpaul Lin199c6252010-12-21 16:59:46 +0800378 unsigned int maccr;
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800379 void *buf;
Macpaul Lin199c6252010-12-21 16:59:46 +0800380 int i;
381
382 debug("%s()\n", __func__);
383
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100384 ftgmac100_reset(priv);
385
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800386 if (!priv->txdes) {
387 txdes = dma_alloc_coherent(
388 sizeof(*txdes) * PKTBUFSTX, &priv->txdes_dma);
389 if (!txdes)
390 panic("ftgmac100: out of memory\n");
391 memset(txdes, 0, sizeof(*txdes) * PKTBUFSTX);
392 priv->txdes = txdes;
393 }
394 txdes = priv->txdes;
395
396 if (!priv->rxdes) {
397 rxdes = dma_alloc_coherent(
398 sizeof(*rxdes) * PKTBUFSRX, &priv->rxdes_dma);
399 if (!rxdes)
400 panic("ftgmac100: out of memory\n");
401 memset(rxdes, 0, sizeof(*rxdes) * PKTBUFSRX);
402 priv->rxdes = rxdes;
403 }
404 rxdes = priv->rxdes;
405
Macpaul Lin199c6252010-12-21 16:59:46 +0800406 /* set the ethernet address */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100407 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800408
409 /* disable all interrupts */
410 writel(0, &ftgmac100->ier);
411
412 /* initialize descriptors */
413 priv->tx_index = 0;
414 priv->rx_index = 0;
415
416 txdes[PKTBUFSTX - 1].txdes0 = FTGMAC100_TXDES0_EDOTR;
417 rxdes[PKTBUFSRX - 1].rxdes0 = FTGMAC100_RXDES0_EDORR;
418
419 for (i = 0; i < PKTBUFSTX; i++) {
420 /* TXBUF_BADR */
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800421 if (!txdes[i].txdes2) {
422 buf = memalign(ARCH_DMA_MINALIGN, CFG_XBUF_SIZE);
423 if (!buf)
424 panic("ftgmac100: out of memory\n");
425 txdes[i].txdes3 = virt_to_phys(buf);
426 txdes[i].txdes2 = (uint)buf;
427 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800428 txdes[i].txdes1 = 0;
429 }
430
431 for (i = 0; i < PKTBUFSRX; i++) {
432 /* RXBUF_BADR */
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800433 if (!rxdes[i].rxdes2) {
Joe Hershberger9f09a362015-04-08 01:41:06 -0500434 buf = net_rx_packets[i];
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800435 rxdes[i].rxdes3 = virt_to_phys(buf);
436 rxdes[i].rxdes2 = (uint)buf;
437 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800438 rxdes[i].rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
439 }
440
441 /* transmit ring */
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800442 writel(priv->txdes_dma, &ftgmac100->txr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800443
444 /* receive ring */
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800445 writel(priv->rxdes_dma, &ftgmac100->rxr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800446
447 /* poll receive descriptor automatically */
448 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
449
450 /* config receive buffer size register */
451 writel(FTGMAC100_RBSR_SIZE(RBSR_DEFAULT_VALUE), &ftgmac100->rbsr);
452
453 /* enable transmitter, receiver */
454 maccr = FTGMAC100_MACCR_TXMAC_EN |
455 FTGMAC100_MACCR_RXMAC_EN |
456 FTGMAC100_MACCR_TXDMA_EN |
457 FTGMAC100_MACCR_RXDMA_EN |
458 FTGMAC100_MACCR_CRC_APD |
459 FTGMAC100_MACCR_FULLDUP |
460 FTGMAC100_MACCR_RX_RUNT |
461 FTGMAC100_MACCR_RX_BROADPKT;
462
463 writel(maccr, &ftgmac100->maccr);
464
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100465 if (!ftgmac100_phy_init(priv, dev)) {
466 if (!ftgmac100_update_link_speed(priv))
Macpaul Lin199c6252010-12-21 16:59:46 +0800467 return -1;
468 }
469
470 return 0;
471}
472
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100473static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
474{
475 struct ftgmac100_data *priv = dev_get_priv(dev);
476 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
477
478 /* Release buffer to DMA */
479 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
480
481 /* Move to next descriptor */
482 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
483
484 return 0;
485}
486
Macpaul Lin199c6252010-12-21 16:59:46 +0800487/*
488 * Get a data block via Ethernet
489 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100490static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Lin199c6252010-12-21 16:59:46 +0800491{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100492 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800493 struct ftgmac100_rxdes *curr_des;
494 unsigned short rxlen;
495
496 curr_des = &priv->rxdes[priv->rx_index];
497
498 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
499 return -1;
500
501 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
502 FTGMAC100_RXDES0_CRC_ERR |
503 FTGMAC100_RXDES0_FTL |
504 FTGMAC100_RXDES0_RUNT |
505 FTGMAC100_RXDES0_RX_ODD_NB)) {
506 return -1;
507 }
508
509 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
510
511 debug("%s(): RX buffer %d, %x received\n",
512 __func__, priv->rx_index, rxlen);
513
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800514 /* invalidate d-cache */
515 dma_map_single((void *)curr_des->rxdes2, rxlen, DMA_FROM_DEVICE);
516
Macpaul Lin199c6252010-12-21 16:59:46 +0800517 /* pass the packet up to the protocol layers. */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500518 net_process_received_packet((void *)curr_des->rxdes2, rxlen);
Macpaul Lin199c6252010-12-21 16:59:46 +0800519
520 /* release buffer to DMA */
521 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
522
523 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
524
525 return 0;
526}
527
528/*
529 * Send a data block via Ethernet
530 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100531static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Lin199c6252010-12-21 16:59:46 +0800532{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100533 struct ftgmac100_data *priv = dev_get_priv(dev);
534 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800535 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Macpaul Lin199c6252010-12-21 16:59:46 +0800536
537 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
538 debug("%s(): no TX descriptor available\n", __func__);
539 return -1;
540 }
541
542 debug("%s(%x, %x)\n", __func__, (int)packet, length);
543
544 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
545
Kuo-Jung Sucd19bd32013-05-07 14:33:51 +0800546 memcpy((void *)curr_des->txdes2, (void *)packet, length);
547 dma_map_single((void *)curr_des->txdes2, length, DMA_TO_DEVICE);
Macpaul Lin199c6252010-12-21 16:59:46 +0800548
549 /* only one descriptor on TXBUF */
550 curr_des->txdes0 &= FTGMAC100_TXDES0_EDOTR;
551 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
552 FTGMAC100_TXDES0_LTS |
553 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
554 FTGMAC100_TXDES0_TXDMA_OWN ;
555
556 /* start transmit */
557 writel(1, &ftgmac100->txpd);
558
Macpaul Lin199c6252010-12-21 16:59:46 +0800559 debug("%s(): packet sent\n", __func__);
560
561 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
562
563 return 0;
564}
565
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100566static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800567{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100568 struct eth_pdata *pdata = dev_get_platdata(dev);
569 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800570
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100571 return ftgmac100_set_mac(priv, pdata->enetaddr);
572}
Macpaul Lin199c6252010-12-21 16:59:46 +0800573
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100574static int ftgmac100_ofdata_to_platdata(struct udevice *dev)
575{
576 struct eth_pdata *pdata = dev_get_platdata(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800577
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100578 pdata->iobase = devfdt_get_addr(dev);
579 return 0;
580}
Macpaul Lin199c6252010-12-21 16:59:46 +0800581
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100582static int ftgmac100_probe(struct udevice *dev)
583{
584 struct eth_pdata *pdata = dev_get_platdata(dev);
585 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Linc56c5a32011-09-20 19:54:32 +0000586
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100587 priv->iobase = (struct ftgmac100 *)pdata->iobase;
588 return 0;
589}
Macpaul Lin199c6252010-12-21 16:59:46 +0800590
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100591static int ftgmac100_remove(struct udevice *dev)
592{
Macpaul Lin199c6252010-12-21 16:59:46 +0800593 return 0;
594}
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100595
596static const struct eth_ops ftgmac100_ops = {
597 .start = ftgmac100_start,
598 .send = ftgmac100_send,
599 .recv = ftgmac100_recv,
600 .stop = ftgmac100_stop,
601 .free_pkt = ftgmac100_free_pkt,
602 .write_hwaddr = ftgmac100_write_hwaddr,
603};
604
605static const struct udevice_id ftgmac100_ids[] = {
606 { .compatible = "faraday,ftgmac100" },
607 { }
608};
609
610U_BOOT_DRIVER(ftgmac100) = {
611 .name = "ftgmac100",
612 .id = UCLASS_ETH,
613 .of_match = ftgmac100_ids,
614 .ofdata_to_platdata = ftgmac100_ofdata_to_platdata,
615 .probe = ftgmac100_probe,
616 .remove = ftgmac100_remove,
617 .ops = &ftgmac100_ops,
618 .priv_auto_alloc_size = sizeof(struct ftgmac100_data),
619 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
620 .flags = DM_FLAG_ALLOC_PRIV_DMA,
621};