blob: 40e6b3ba3950e94833b1940b955e2a6b2ed041e4 [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 Goater6afa3f12018-10-29 07:06:36 +010014#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070015#include <cpu_func.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010016#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010018#include <miiphy.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080019#include <net.h>
Cédric Le Goater9bcb6652018-10-29 07:06:35 +010020#include <wait_bit.h>
Simon Glass9bc15642020-02-03 07:36:16 -070021#include <dm/device_compat.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010022#include <linux/io.h>
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010023#include <linux/iopoll.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080024
25#include "ftgmac100.h"
26
Cédric Le Goater3174dfb2018-10-29 07:06:34 +010027/* Min frame ethernet frame size without FCS */
28#define ETH_ZLEN 60
Macpaul Lin199c6252010-12-21 16:59:46 +080029
Cédric Le Goater3174dfb2018-10-29 07:06:34 +010030/* Receive Buffer Size Register - HW default is 0x640 */
31#define FTGMAC100_RBSR_DEFAULT 0x640
Macpaul Lin199c6252010-12-21 16:59:46 +080032
33/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
34#define PKTBUFSTX 4 /* must be power of 2 */
35
Cédric Le Goater9bcb6652018-10-29 07:06:35 +010036/* Timeout for transmit */
37#define FTGMAC100_TX_TIMEOUT_MS 1000
38
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010039/* Timeout for a mdio read/write operation */
40#define FTGMAC100_MDIO_TIMEOUT_USEC 10000
41
42/*
43 * MDC clock cycle threshold
44 *
45 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
46 */
47#define MDC_CYCTHR 0x34
48
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010049/*
50 * ftgmac100 model variants
51 */
52enum ftgmac100_model {
53 FTGMAC100_MODEL_FARADAY,
54 FTGMAC100_MODEL_ASPEED,
55};
56
Cédric Le Goater38b33e92018-10-29 07:06:31 +010057/**
58 * struct ftgmac100_data - private data for the FTGMAC100 driver
59 *
60 * @iobase: The base address of the hardware registers
61 * @txdes: The array of transmit descriptors
62 * @rxdes: The array of receive descriptors
63 * @tx_index: Transmit descriptor index in @txdes
64 * @rx_index: Receive descriptor index in @rxdes
65 * @phy_addr: The PHY interface address to use
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010066 * @phydev: The PHY device backing the MAC
67 * @bus: The mdio bus
68 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
69 * @max_speed: Maximum speed of Ethernet connection supported by MAC
Cédric Le Goater6afa3f12018-10-29 07:06:36 +010070 * @clks: The bulk of clocks assigned to the device in the DT
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010071 * @rxdes0_edorr_mask: The bit number identifying the end of the RX ring buffer
72 * @txdes0_edotr_mask: The bit number identifying the end of the TX ring buffer
Cédric Le Goater38b33e92018-10-29 07:06:31 +010073 */
Macpaul Lin199c6252010-12-21 16:59:46 +080074struct ftgmac100_data {
Cédric Le Goater38b33e92018-10-29 07:06:31 +010075 struct ftgmac100 *iobase;
76
Cédric Le Goater0404e9f2019-11-28 13:37:04 +010077 struct ftgmac100_txdes txdes[PKTBUFSTX] __aligned(ARCH_DMA_MINALIGN);
78 struct ftgmac100_rxdes rxdes[PKTBUFSRX] __aligned(ARCH_DMA_MINALIGN);
Macpaul Lin199c6252010-12-21 16:59:46 +080079 int tx_index;
80 int rx_index;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010081
82 u32 phy_addr;
83 struct phy_device *phydev;
84 struct mii_dev *bus;
85 u32 phy_mode;
86 u32 max_speed;
Cédric Le Goater6afa3f12018-10-29 07:06:36 +010087
88 struct clk_bulk clks;
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010089
90 /* End of RX/TX ring buffer bits. Depend on model */
91 u32 rxdes0_edorr_mask;
92 u32 txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +080093};
94
95/*
96 * struct mii_bus functions
97 */
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010098static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
99 int reg_addr)
Macpaul Lin199c6252010-12-21 16:59:46 +0800100{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100101 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100102 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800103 int phycr;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100104 int data;
105 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800106
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100107 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
108 FTGMAC100_PHYCR_PHYAD(phy_addr) |
109 FTGMAC100_PHYCR_REGAD(reg_addr) |
110 FTGMAC100_PHYCR_MIIRD;
Macpaul Lin199c6252010-12-21 16:59:46 +0800111 writel(phycr, &ftgmac100->phycr);
112
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100113 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
114 !(phycr & FTGMAC100_PHYCR_MIIRD),
115 FTGMAC100_MDIO_TIMEOUT_USEC);
116 if (ret) {
117 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
118 priv->phydev->dev->name, phy_addr, reg_addr);
119 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800120 }
121
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100122 data = readl(&ftgmac100->phydata);
123
124 return FTGMAC100_PHYDATA_MIIRDATA(data);
Macpaul Lin199c6252010-12-21 16:59:46 +0800125}
126
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100127static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
128 int reg_addr, u16 value)
Macpaul Lin199c6252010-12-21 16:59:46 +0800129{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100130 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100131 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800132 int phycr;
133 int data;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100134 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800135
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100136 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
137 FTGMAC100_PHYCR_PHYAD(phy_addr) |
138 FTGMAC100_PHYCR_REGAD(reg_addr) |
139 FTGMAC100_PHYCR_MIIWR;
Macpaul Lin199c6252010-12-21 16:59:46 +0800140 data = FTGMAC100_PHYDATA_MIIWDATA(value);
141
142 writel(data, &ftgmac100->phydata);
143 writel(phycr, &ftgmac100->phycr);
144
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100145 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
146 !(phycr & FTGMAC100_PHYCR_MIIWR),
147 FTGMAC100_MDIO_TIMEOUT_USEC);
148 if (ret) {
149 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
150 priv->phydev->dev->name, phy_addr, reg_addr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800151 }
152
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100153 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800154}
155
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100156static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800157{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100158 struct ftgmac100_data *priv = dev_get_priv(dev);
159 struct mii_dev *bus;
160 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800161
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100162 bus = mdio_alloc();
163 if (!bus)
164 return -ENOMEM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800165
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100166 bus->read = ftgmac100_mdio_read;
167 bus->write = ftgmac100_mdio_write;
168 bus->priv = priv;
Macpaul Lin199c6252010-12-21 16:59:46 +0800169
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100170 ret = mdio_register_seq(bus, dev->seq);
171 if (ret) {
172 free(bus);
173 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800174 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800175
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100176 priv->bus = bus;
Macpaul Lin199c6252010-12-21 16:59:46 +0800177
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100178 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800179}
180
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100181static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800182{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100183 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100184 struct phy_device *phydev = priv->phydev;
185 u32 maccr;
Macpaul Lin199c6252010-12-21 16:59:46 +0800186
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100187 if (!phydev->link) {
188 dev_err(phydev->dev, "No link\n");
189 return -EREMOTEIO;
190 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800191
192 /* read MAC control register and clear related bits */
193 maccr = readl(&ftgmac100->maccr) &
194 ~(FTGMAC100_MACCR_GIGA_MODE |
195 FTGMAC100_MACCR_FAST_MODE |
196 FTGMAC100_MACCR_FULLDUP);
197
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100198 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Lin199c6252010-12-21 16:59:46 +0800199 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800200
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100201 if (phydev->speed == 100)
Macpaul Lin199c6252010-12-21 16:59:46 +0800202 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800203
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100204 if (phydev->duplex)
205 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Lin199c6252010-12-21 16:59:46 +0800206
207 /* update MII config into maccr */
208 writel(maccr, &ftgmac100->maccr);
209
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100210 return 0;
211}
212
213static int ftgmac100_phy_init(struct udevice *dev)
214{
215 struct ftgmac100_data *priv = dev_get_priv(dev);
216 struct phy_device *phydev;
217 int ret;
218
219 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
220 if (!phydev)
221 return -ENODEV;
222
223 phydev->supported &= PHY_GBIT_FEATURES;
224 if (priv->max_speed) {
225 ret = phy_set_supported(phydev, priv->max_speed);
226 if (ret)
227 return ret;
228 }
229 phydev->advertising = phydev->supported;
230 priv->phydev = phydev;
231 phy_config(phydev);
232
233 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800234}
235
236/*
237 * Reset MAC
238 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100239static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800240{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100241 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800242
243 debug("%s()\n", __func__);
244
Cédric Le Goatercef951c2018-10-29 07:06:32 +0100245 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Lin199c6252010-12-21 16:59:46 +0800246
247 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
248 ;
249}
250
251/*
252 * Set MAC address
253 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100254static int ftgmac100_set_mac(struct ftgmac100_data *priv,
255 const unsigned char *mac)
Macpaul Lin199c6252010-12-21 16:59:46 +0800256{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100257 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800258 unsigned int maddr = mac[0] << 8 | mac[1];
259 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
260
261 debug("%s(%x %x)\n", __func__, maddr, laddr);
262
263 writel(maddr, &ftgmac100->mac_madr);
264 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800265
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100266 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800267}
268
269/*
270 * disable transmitter, receiver
271 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100272static void ftgmac100_stop(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800273{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100274 struct ftgmac100_data *priv = dev_get_priv(dev);
275 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800276
277 debug("%s()\n", __func__);
278
279 writel(0, &ftgmac100->maccr);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100280
281 phy_shutdown(priv->phydev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800282}
283
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100284static int ftgmac100_start(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800285{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100286 struct eth_pdata *plat = dev_get_platdata(dev);
287 struct ftgmac100_data *priv = dev_get_priv(dev);
288 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100289 struct phy_device *phydev = priv->phydev;
Macpaul Lin199c6252010-12-21 16:59:46 +0800290 unsigned int maccr;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100291 ulong start, end;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100292 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800293 int i;
294
295 debug("%s()\n", __func__);
296
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100297 ftgmac100_reset(priv);
298
Macpaul Lin199c6252010-12-21 16:59:46 +0800299 /* set the ethernet address */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100300 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800301
302 /* disable all interrupts */
303 writel(0, &ftgmac100->ier);
304
305 /* initialize descriptors */
306 priv->tx_index = 0;
307 priv->rx_index = 0;
308
Macpaul Lin199c6252010-12-21 16:59:46 +0800309 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100310 priv->txdes[i].txdes3 = 0;
311 priv->txdes[i].txdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800312 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100313 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100314
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100315 start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100316 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
317 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800318
319 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100320 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
321 priv->rxdes[i].rxdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800322 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100323 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100324
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100325 start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100326 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
327 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800328
329 /* transmit ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100330 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800331
332 /* receive ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100333 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800334
335 /* poll receive descriptor automatically */
336 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
337
338 /* config receive buffer size register */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100339 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800340
341 /* enable transmitter, receiver */
342 maccr = FTGMAC100_MACCR_TXMAC_EN |
343 FTGMAC100_MACCR_RXMAC_EN |
344 FTGMAC100_MACCR_TXDMA_EN |
345 FTGMAC100_MACCR_RXDMA_EN |
346 FTGMAC100_MACCR_CRC_APD |
347 FTGMAC100_MACCR_FULLDUP |
348 FTGMAC100_MACCR_RX_RUNT |
349 FTGMAC100_MACCR_RX_BROADPKT;
350
351 writel(maccr, &ftgmac100->maccr);
352
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100353 ret = phy_startup(phydev);
354 if (ret) {
355 dev_err(phydev->dev, "Could not start PHY\n");
356 return ret;
357 }
358
359 ret = ftgmac100_phy_adjust_link(priv);
360 if (ret) {
361 dev_err(phydev->dev, "Could not adjust link\n");
362 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800363 }
364
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100365 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
366 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
367
Macpaul Lin199c6252010-12-21 16:59:46 +0800368 return 0;
369}
370
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100371static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
372{
373 struct ftgmac100_data *priv = dev_get_priv(dev);
374 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100375 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100376 ulong des_end = des_start +
377 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100378
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100379 /* Release buffer to DMA and flush descriptor */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100380 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100381 flush_dcache_range(des_start, des_end);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100382
383 /* Move to next descriptor */
384 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
385
386 return 0;
387}
388
Macpaul Lin199c6252010-12-21 16:59:46 +0800389/*
390 * Get a data block via Ethernet
391 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100392static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Lin199c6252010-12-21 16:59:46 +0800393{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100394 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100395 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Lin199c6252010-12-21 16:59:46 +0800396 unsigned short rxlen;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100397 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100398 ulong des_end = des_start +
399 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
400 ulong data_start = curr_des->rxdes3;
401 ulong data_end;
Macpaul Lin199c6252010-12-21 16:59:46 +0800402
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100403 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800404
405 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100406 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800407
408 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
409 FTGMAC100_RXDES0_CRC_ERR |
410 FTGMAC100_RXDES0_FTL |
411 FTGMAC100_RXDES0_RUNT |
412 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100413 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800414 }
415
416 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
417
418 debug("%s(): RX buffer %d, %x received\n",
419 __func__, priv->rx_index, rxlen);
420
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100421 /* Invalidate received data */
422 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
423 invalidate_dcache_range(data_start, data_end);
424 *packetp = (uchar *)data_start;
Macpaul Lin199c6252010-12-21 16:59:46 +0800425
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100426 return rxlen;
Macpaul Lin199c6252010-12-21 16:59:46 +0800427}
428
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100429static u32 ftgmac100_read_txdesc(const void *desc)
430{
431 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100432 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100433 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
434
435 invalidate_dcache_range(des_start, des_end);
436
437 return txdes->txdes0;
438}
439
440BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
441
Macpaul Lin199c6252010-12-21 16:59:46 +0800442/*
443 * Send a data block via Ethernet
444 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100445static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Lin199c6252010-12-21 16:59:46 +0800446{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100447 struct ftgmac100_data *priv = dev_get_priv(dev);
448 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800449 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100450 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100451 ulong des_end = des_start +
452 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
453 ulong data_start;
454 ulong data_end;
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100455 int rc;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100456
457 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800458
459 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100460 dev_err(dev, "no TX descriptor available\n");
461 return -EPERM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800462 }
463
464 debug("%s(%x, %x)\n", __func__, (int)packet, length);
465
466 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
467
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100468 curr_des->txdes3 = (unsigned int)packet;
469
470 /* Flush data to be sent */
471 data_start = curr_des->txdes3;
472 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
473 flush_dcache_range(data_start, data_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800474
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100475 /* Only one segment on TXBUF */
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100476 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +0800477 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
478 FTGMAC100_TXDES0_LTS |
479 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
480 FTGMAC100_TXDES0_TXDMA_OWN ;
481
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100482 /* Flush modified buffer descriptor */
483 flush_dcache_range(des_start, des_end);
484
485 /* Start transmit */
Macpaul Lin199c6252010-12-21 16:59:46 +0800486 writel(1, &ftgmac100->txpd);
487
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100488 rc = wait_for_bit_ftgmac100_txdone(curr_des,
489 FTGMAC100_TXDES0_TXDMA_OWN, false,
490 FTGMAC100_TX_TIMEOUT_MS, true);
491 if (rc)
492 return rc;
493
Macpaul Lin199c6252010-12-21 16:59:46 +0800494 debug("%s(): packet sent\n", __func__);
495
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100496 /* Move to next descriptor */
Macpaul Lin199c6252010-12-21 16:59:46 +0800497 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
498
499 return 0;
500}
501
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100502static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800503{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100504 struct eth_pdata *pdata = dev_get_platdata(dev);
505 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800506
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100507 return ftgmac100_set_mac(priv, pdata->enetaddr);
508}
Macpaul Lin199c6252010-12-21 16:59:46 +0800509
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100510static int ftgmac100_ofdata_to_platdata(struct udevice *dev)
511{
512 struct eth_pdata *pdata = dev_get_platdata(dev);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100513 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100514 const char *phy_mode;
Macpaul Lin199c6252010-12-21 16:59:46 +0800515
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100516 pdata->iobase = devfdt_get_addr(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100517 pdata->phy_interface = -1;
518 phy_mode = dev_read_string(dev, "phy-mode");
519 if (phy_mode)
520 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
521 if (pdata->phy_interface == -1) {
522 dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
523 return -EINVAL;
524 }
525
526 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
527
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100528 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
529 priv->rxdes0_edorr_mask = BIT(30);
530 priv->txdes0_edotr_mask = BIT(30);
531 } else {
532 priv->rxdes0_edorr_mask = BIT(15);
533 priv->txdes0_edotr_mask = BIT(15);
534 }
535
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100536 return clk_get_bulk(dev, &priv->clks);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100537}
Macpaul Lin199c6252010-12-21 16:59:46 +0800538
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100539static int ftgmac100_probe(struct udevice *dev)
540{
541 struct eth_pdata *pdata = dev_get_platdata(dev);
542 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100543 int ret;
Macpaul Linc56c5a32011-09-20 19:54:32 +0000544
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100545 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100546 priv->phy_mode = pdata->phy_interface;
547 priv->max_speed = pdata->max_speed;
548 priv->phy_addr = 0;
549
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100550 ret = clk_enable_bulk(&priv->clks);
551 if (ret)
552 goto out;
553
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100554 ret = ftgmac100_mdio_init(dev);
555 if (ret) {
556 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
557 goto out;
558 }
559
560 ret = ftgmac100_phy_init(dev);
561 if (ret) {
562 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
563 goto out;
564 }
565
566out:
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100567 if (ret)
568 clk_release_bulk(&priv->clks);
569
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100570 return ret;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100571}
Macpaul Lin199c6252010-12-21 16:59:46 +0800572
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100573static int ftgmac100_remove(struct udevice *dev)
574{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100575 struct ftgmac100_data *priv = dev_get_priv(dev);
576
577 free(priv->phydev);
578 mdio_unregister(priv->bus);
579 mdio_free(priv->bus);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100580 clk_release_bulk(&priv->clks);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100581
Macpaul Lin199c6252010-12-21 16:59:46 +0800582 return 0;
583}
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100584
585static const struct eth_ops ftgmac100_ops = {
586 .start = ftgmac100_start,
587 .send = ftgmac100_send,
588 .recv = ftgmac100_recv,
589 .stop = ftgmac100_stop,
590 .free_pkt = ftgmac100_free_pkt,
591 .write_hwaddr = ftgmac100_write_hwaddr,
592};
593
594static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100595 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
596 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100597 { }
598};
599
600U_BOOT_DRIVER(ftgmac100) = {
601 .name = "ftgmac100",
602 .id = UCLASS_ETH,
603 .of_match = ftgmac100_ids,
604 .ofdata_to_platdata = ftgmac100_ofdata_to_platdata,
605 .probe = ftgmac100_probe,
606 .remove = ftgmac100_remove,
607 .ops = &ftgmac100_ops,
608 .priv_auto_alloc_size = sizeof(struct ftgmac100_data),
609 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
610 .flags = DM_FLAG_ALLOC_PRIV_DMA,
611};