blob: 9d64125bfcb8ac8f060c9e0c107c705e2f723eb5 [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>
Dylan Hung1a2ef082023-07-27 09:58:14 +080015#include <reset.h>
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010017#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <malloc.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010020#include <miiphy.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080021#include <net.h>
Cédric Le Goater9bcb6652018-10-29 07:06:35 +010022#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Cédric Le Goater38b33e92018-10-29 07:06:31 +010026#include <linux/io.h>
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010027#include <linux/iopoll.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060028#include <linux/printk.h>
Macpaul Lin199c6252010-12-21 16:59:46 +080029
30#include "ftgmac100.h"
31
Cédric Le Goater3174dfb2018-10-29 07:06:34 +010032/* Min frame ethernet frame size without FCS */
33#define ETH_ZLEN 60
Macpaul Lin199c6252010-12-21 16:59:46 +080034
Cédric Le Goater3174dfb2018-10-29 07:06:34 +010035/* Receive Buffer Size Register - HW default is 0x640 */
36#define FTGMAC100_RBSR_DEFAULT 0x640
Macpaul Lin199c6252010-12-21 16:59:46 +080037
38/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
39#define PKTBUFSTX 4 /* must be power of 2 */
40
Cédric Le Goater9bcb6652018-10-29 07:06:35 +010041/* Timeout for transmit */
42#define FTGMAC100_TX_TIMEOUT_MS 1000
43
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010044/* Timeout for a mdio read/write operation */
45#define FTGMAC100_MDIO_TIMEOUT_USEC 10000
46
47/*
48 * MDC clock cycle threshold
49 *
50 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
51 */
52#define MDC_CYCTHR 0x34
53
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010054/*
55 * ftgmac100 model variants
56 */
57enum ftgmac100_model {
58 FTGMAC100_MODEL_FARADAY,
59 FTGMAC100_MODEL_ASPEED,
60};
61
Cédric Le Goater38b33e92018-10-29 07:06:31 +010062/**
63 * struct ftgmac100_data - private data for the FTGMAC100 driver
64 *
65 * @iobase: The base address of the hardware registers
66 * @txdes: The array of transmit descriptors
67 * @rxdes: The array of receive descriptors
68 * @tx_index: Transmit descriptor index in @txdes
69 * @rx_index: Receive descriptor index in @rxdes
70 * @phy_addr: The PHY interface address to use
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010071 * @phydev: The PHY device backing the MAC
72 * @bus: The mdio bus
73 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
74 * @max_speed: Maximum speed of Ethernet connection supported by MAC
Cédric Le Goater6afa3f12018-10-29 07:06:36 +010075 * @clks: The bulk of clocks assigned to the device in the DT
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010076 * @rxdes0_edorr_mask: The bit number identifying the end of the RX ring buffer
77 * @txdes0_edotr_mask: The bit number identifying the end of the TX ring buffer
Cédric Le Goater38b33e92018-10-29 07:06:31 +010078 */
Macpaul Lin199c6252010-12-21 16:59:46 +080079struct ftgmac100_data {
Cédric Le Goater38b33e92018-10-29 07:06:31 +010080 struct ftgmac100 *iobase;
81
Cédric Le Goater0404e9f2019-11-28 13:37:04 +010082 struct ftgmac100_txdes txdes[PKTBUFSTX] __aligned(ARCH_DMA_MINALIGN);
83 struct ftgmac100_rxdes rxdes[PKTBUFSRX] __aligned(ARCH_DMA_MINALIGN);
Macpaul Lin199c6252010-12-21 16:59:46 +080084 int tx_index;
85 int rx_index;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +010086
87 u32 phy_addr;
88 struct phy_device *phydev;
89 struct mii_dev *bus;
90 u32 phy_mode;
91 u32 max_speed;
Cédric Le Goater6afa3f12018-10-29 07:06:36 +010092
93 struct clk_bulk clks;
Dylan Hung1a2ef082023-07-27 09:58:14 +080094 struct reset_ctl *reset_ctl;
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010095
96 /* End of RX/TX ring buffer bits. Depend on model */
97 u32 rxdes0_edorr_mask;
98 u32 txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +080099};
100
101/*
102 * struct mii_bus functions
103 */
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100104static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
105 int reg_addr)
Macpaul Lin199c6252010-12-21 16:59:46 +0800106{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100107 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100108 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800109 int phycr;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100110 int data;
111 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800112
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100113 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
114 FTGMAC100_PHYCR_PHYAD(phy_addr) |
115 FTGMAC100_PHYCR_REGAD(reg_addr) |
116 FTGMAC100_PHYCR_MIIRD;
Macpaul Lin199c6252010-12-21 16:59:46 +0800117 writel(phycr, &ftgmac100->phycr);
118
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100119 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
120 !(phycr & FTGMAC100_PHYCR_MIIRD),
121 FTGMAC100_MDIO_TIMEOUT_USEC);
122 if (ret) {
123 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
Zev Weiss4e5ca0f2022-05-17 15:16:39 -0700124 bus->name, phy_addr, reg_addr);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100125 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800126 }
127
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100128 data = readl(&ftgmac100->phydata);
129
130 return FTGMAC100_PHYDATA_MIIRDATA(data);
Macpaul Lin199c6252010-12-21 16:59:46 +0800131}
132
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100133static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
134 int reg_addr, u16 value)
Macpaul Lin199c6252010-12-21 16:59:46 +0800135{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100136 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100137 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800138 int phycr;
139 int data;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100140 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800141
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100142 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
143 FTGMAC100_PHYCR_PHYAD(phy_addr) |
144 FTGMAC100_PHYCR_REGAD(reg_addr) |
145 FTGMAC100_PHYCR_MIIWR;
Macpaul Lin199c6252010-12-21 16:59:46 +0800146 data = FTGMAC100_PHYDATA_MIIWDATA(value);
147
148 writel(data, &ftgmac100->phydata);
149 writel(phycr, &ftgmac100->phycr);
150
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100151 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
152 !(phycr & FTGMAC100_PHYCR_MIIWR),
153 FTGMAC100_MDIO_TIMEOUT_USEC);
154 if (ret) {
155 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
Zev Weiss4e5ca0f2022-05-17 15:16:39 -0700156 bus->name, phy_addr, reg_addr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800157 }
158
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100159 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800160}
161
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100162static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800163{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100164 struct ftgmac100_data *priv = dev_get_priv(dev);
165 struct mii_dev *bus;
166 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800167
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100168 bus = mdio_alloc();
169 if (!bus)
170 return -ENOMEM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800171
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100172 bus->read = ftgmac100_mdio_read;
173 bus->write = ftgmac100_mdio_write;
174 bus->priv = priv;
Macpaul Lin199c6252010-12-21 16:59:46 +0800175
Simon Glass75e534b2020-12-16 21:20:07 -0700176 ret = mdio_register_seq(bus, dev_seq(dev));
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100177 if (ret) {
178 free(bus);
179 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800180 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800181
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100182 priv->bus = bus;
Macpaul Lin199c6252010-12-21 16:59:46 +0800183
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100184 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800185}
186
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100187static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800188{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100189 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100190 struct phy_device *phydev = priv->phydev;
191 u32 maccr;
Macpaul Lin199c6252010-12-21 16:59:46 +0800192
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930193 if (!phydev->link && priv->phy_mode != PHY_INTERFACE_MODE_NCSI) {
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100194 dev_err(phydev->dev, "No link\n");
195 return -EREMOTEIO;
196 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800197
198 /* read MAC control register and clear related bits */
199 maccr = readl(&ftgmac100->maccr) &
200 ~(FTGMAC100_MACCR_GIGA_MODE |
201 FTGMAC100_MACCR_FAST_MODE |
202 FTGMAC100_MACCR_FULLDUP);
203
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100204 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Lin199c6252010-12-21 16:59:46 +0800205 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800206
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100207 if (phydev->speed == 100)
Macpaul Lin199c6252010-12-21 16:59:46 +0800208 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800209
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100210 if (phydev->duplex)
211 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Lin199c6252010-12-21 16:59:46 +0800212
213 /* update MII config into maccr */
214 writel(maccr, &ftgmac100->maccr);
215
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100216 return 0;
217}
218
219static int ftgmac100_phy_init(struct udevice *dev)
220{
221 struct ftgmac100_data *priv = dev_get_priv(dev);
222 struct phy_device *phydev;
223 int ret;
224
Jacky Chou7c011532024-06-28 15:14:45 +0800225 if (IS_ENABLED(CONFIG_DM_MDIO) && priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
Dylan Hung2bec3722021-12-09 10:12:24 +0800226 phydev = dm_eth_phy_connect(dev);
227 else
228 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
229
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100230 if (!phydev)
231 return -ENODEV;
232
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930233 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
234 phydev->supported &= PHY_GBIT_FEATURES;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100235 if (priv->max_speed) {
236 ret = phy_set_supported(phydev, priv->max_speed);
237 if (ret)
238 return ret;
239 }
240 phydev->advertising = phydev->supported;
241 priv->phydev = phydev;
242 phy_config(phydev);
243
244 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800245}
246
247/*
248 * Reset MAC
249 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100250static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800251{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100252 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800253
254 debug("%s()\n", __func__);
255
Cédric Le Goatercef951c2018-10-29 07:06:32 +0100256 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Lin199c6252010-12-21 16:59:46 +0800257
258 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
259 ;
260}
261
262/*
263 * Set MAC address
264 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100265static int ftgmac100_set_mac(struct ftgmac100_data *priv,
266 const unsigned char *mac)
Macpaul Lin199c6252010-12-21 16:59:46 +0800267{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100268 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800269 unsigned int maddr = mac[0] << 8 | mac[1];
270 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
271
272 debug("%s(%x %x)\n", __func__, maddr, laddr);
273
274 writel(maddr, &ftgmac100->mac_madr);
275 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800276
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100277 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800278}
279
280/*
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500281 * Get MAC address
282 */
283static int ftgmac100_get_mac(struct ftgmac100_data *priv,
284 unsigned char *mac)
285{
286 struct ftgmac100 *ftgmac100 = priv->iobase;
287 unsigned int maddr = readl(&ftgmac100->mac_madr);
288 unsigned int laddr = readl(&ftgmac100->mac_ladr);
289
290 debug("%s(%x %x)\n", __func__, maddr, laddr);
291
292 mac[0] = (maddr >> 8) & 0xff;
293 mac[1] = maddr & 0xff;
294 mac[2] = (laddr >> 24) & 0xff;
295 mac[3] = (laddr >> 16) & 0xff;
296 mac[4] = (laddr >> 8) & 0xff;
297 mac[5] = laddr & 0xff;
298
299 return 0;
300}
301
302/*
Macpaul Lin199c6252010-12-21 16:59:46 +0800303 * disable transmitter, receiver
304 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100305static void ftgmac100_stop(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800306{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100307 struct ftgmac100_data *priv = dev_get_priv(dev);
308 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800309
310 debug("%s()\n", __func__);
311
312 writel(0, &ftgmac100->maccr);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100313
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930314 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
315 phy_shutdown(priv->phydev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800316}
317
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100318static int ftgmac100_start(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800319{
Simon Glassfa20e932020-12-03 16:55:20 -0700320 struct eth_pdata *plat = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100321 struct ftgmac100_data *priv = dev_get_priv(dev);
322 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100323 struct phy_device *phydev = priv->phydev;
Jacky Choua3e259b2024-06-28 17:38:50 +0800324 unsigned int maccr, dblac, desc_size;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100325 ulong start, end;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100326 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800327 int i;
328
329 debug("%s()\n", __func__);
330
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100331 ftgmac100_reset(priv);
332
Macpaul Lin199c6252010-12-21 16:59:46 +0800333 /* set the ethernet address */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100334 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800335
336 /* disable all interrupts */
337 writel(0, &ftgmac100->ier);
338
339 /* initialize descriptors */
340 priv->tx_index = 0;
341 priv->rx_index = 0;
342
Macpaul Lin199c6252010-12-21 16:59:46 +0800343 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100344 priv->txdes[i].txdes3 = 0;
345 priv->txdes[i].txdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800346 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100347 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100348
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100349 start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100350 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
351 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800352
353 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100354 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
355 priv->rxdes[i].rxdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800356 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100357 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100358
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100359 start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100360 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
361 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800362
363 /* transmit ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100364 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800365
366 /* receive ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100367 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800368
Jacky Choua3e259b2024-06-28 17:38:50 +0800369 /* Configure TX/RX decsriptor size
370 * This size is calculated based on cache line.
371 */
372 desc_size = ARCH_DMA_MINALIGN / FTGMAC100_DESC_UNIT;
373 /* The descriptor size is at least 2 descriptor units. */
374 if (desc_size < 2)
375 desc_size = 2;
376 dblac = readl(&ftgmac100->dblac) & ~GENMASK(19, 12);
377 dblac |= FTGMAC100_DBLAC_RXDES_SIZE(desc_size) | FTGMAC100_DBLAC_TXDES_SIZE(desc_size);
378 writel(dblac, &ftgmac100->dblac);
379
Macpaul Lin199c6252010-12-21 16:59:46 +0800380 /* poll receive descriptor automatically */
381 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
382
383 /* config receive buffer size register */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100384 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800385
386 /* enable transmitter, receiver */
387 maccr = FTGMAC100_MACCR_TXMAC_EN |
388 FTGMAC100_MACCR_RXMAC_EN |
389 FTGMAC100_MACCR_TXDMA_EN |
390 FTGMAC100_MACCR_RXDMA_EN |
391 FTGMAC100_MACCR_CRC_APD |
392 FTGMAC100_MACCR_FULLDUP |
393 FTGMAC100_MACCR_RX_RUNT |
394 FTGMAC100_MACCR_RX_BROADPKT;
395
396 writel(maccr, &ftgmac100->maccr);
397
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100398 ret = phy_startup(phydev);
399 if (ret) {
400 dev_err(phydev->dev, "Could not start PHY\n");
401 return ret;
402 }
403
404 ret = ftgmac100_phy_adjust_link(priv);
405 if (ret) {
406 dev_err(phydev->dev, "Could not adjust link\n");
407 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800408 }
409
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100410 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
411 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
412
Macpaul Lin199c6252010-12-21 16:59:46 +0800413 return 0;
414}
415
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100416static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
417{
418 struct ftgmac100_data *priv = dev_get_priv(dev);
419 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100420 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100421 ulong des_end = des_start +
422 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100423
Jacky Chou69d57072024-06-27 14:26:00 +0800424 /*
425 * Make sure there are no stale data in write-back over this area, which
426 * might get written into the memory while the ftgmac100 also writes
427 * into the same memory area.
428 */
429 flush_dcache_range((ulong)net_rx_packets[priv->rx_index],
430 (ulong)net_rx_packets[priv->rx_index] + PKTSIZE_ALIGN);
431
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100432 /* Release buffer to DMA and flush descriptor */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100433 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100434 flush_dcache_range(des_start, des_end);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100435
436 /* Move to next descriptor */
437 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
438
439 return 0;
440}
441
Macpaul Lin199c6252010-12-21 16:59:46 +0800442/*
443 * Get a data block via Ethernet
444 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100445static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
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);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100448 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Lin199c6252010-12-21 16:59:46 +0800449 unsigned short rxlen;
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 = curr_des->rxdes3;
454 ulong data_end;
Macpaul Lin199c6252010-12-21 16:59:46 +0800455
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100456 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800457
458 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100459 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800460
461 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
462 FTGMAC100_RXDES0_CRC_ERR |
463 FTGMAC100_RXDES0_FTL |
464 FTGMAC100_RXDES0_RUNT |
465 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100466 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800467 }
468
469 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
470
471 debug("%s(): RX buffer %d, %x received\n",
472 __func__, priv->rx_index, rxlen);
473
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100474 /* Invalidate received data */
475 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
476 invalidate_dcache_range(data_start, data_end);
477 *packetp = (uchar *)data_start;
Macpaul Lin199c6252010-12-21 16:59:46 +0800478
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100479 return rxlen;
Macpaul Lin199c6252010-12-21 16:59:46 +0800480}
481
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100482static u32 ftgmac100_read_txdesc(const void *desc)
483{
484 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100485 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100486 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
487
488 invalidate_dcache_range(des_start, des_end);
489
490 return txdes->txdes0;
491}
492
493BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
494
Macpaul Lin199c6252010-12-21 16:59:46 +0800495/*
496 * Send a data block via Ethernet
497 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100498static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Lin199c6252010-12-21 16:59:46 +0800499{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100500 struct ftgmac100_data *priv = dev_get_priv(dev);
501 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800502 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100503 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100504 ulong des_end = des_start +
505 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
506 ulong data_start;
507 ulong data_end;
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100508 int rc;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100509
510 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800511
512 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100513 dev_err(dev, "no TX descriptor available\n");
514 return -EPERM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800515 }
516
517 debug("%s(%x, %x)\n", __func__, (int)packet, length);
518
519 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
520
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100521 curr_des->txdes3 = (unsigned int)packet;
522
523 /* Flush data to be sent */
524 data_start = curr_des->txdes3;
525 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
526 flush_dcache_range(data_start, data_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800527
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100528 /* Only one segment on TXBUF */
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100529 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +0800530 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
531 FTGMAC100_TXDES0_LTS |
532 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
533 FTGMAC100_TXDES0_TXDMA_OWN ;
534
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100535 /* Flush modified buffer descriptor */
536 flush_dcache_range(des_start, des_end);
537
538 /* Start transmit */
Macpaul Lin199c6252010-12-21 16:59:46 +0800539 writel(1, &ftgmac100->txpd);
540
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100541 rc = wait_for_bit_ftgmac100_txdone(curr_des,
542 FTGMAC100_TXDES0_TXDMA_OWN, false,
543 FTGMAC100_TX_TIMEOUT_MS, true);
544 if (rc)
545 return rc;
546
Macpaul Lin199c6252010-12-21 16:59:46 +0800547 debug("%s(): packet sent\n", __func__);
548
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100549 /* Move to next descriptor */
Macpaul Lin199c6252010-12-21 16:59:46 +0800550 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
551
552 return 0;
553}
554
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100555static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800556{
Simon Glassfa20e932020-12-03 16:55:20 -0700557 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100558 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800559
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100560 return ftgmac100_set_mac(priv, pdata->enetaddr);
561}
Macpaul Lin199c6252010-12-21 16:59:46 +0800562
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500563static int ftgmac_read_hwaddr(struct udevice *dev)
564{
565 struct eth_pdata *pdata = dev_get_plat(dev);
566 struct ftgmac100_data *priv = dev_get_priv(dev);
567
568 return ftgmac100_get_mac(priv, pdata->enetaddr);
569}
570
Simon Glassaad29ae2020-12-03 16:55:21 -0700571static int ftgmac100_of_to_plat(struct udevice *dev)
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100572{
Simon Glassfa20e932020-12-03 16:55:20 -0700573 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100574 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800575
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900576 pdata->iobase = dev_read_addr(dev);
Marek Behúnbc194772022-04-07 00:33:01 +0200577
578 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200579 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100580 return -EINVAL;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100581
582 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
583
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100584 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
585 priv->rxdes0_edorr_mask = BIT(30);
586 priv->txdes0_edotr_mask = BIT(30);
587 } else {
588 priv->rxdes0_edorr_mask = BIT(15);
589 priv->txdes0_edotr_mask = BIT(15);
590 }
591
Dylan Hung1a2ef082023-07-27 09:58:14 +0800592 priv->reset_ctl = devm_reset_control_get_optional(dev, NULL);
593
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100594 return clk_get_bulk(dev, &priv->clks);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100595}
Macpaul Lin199c6252010-12-21 16:59:46 +0800596
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100597static int ftgmac100_probe(struct udevice *dev)
598{
Simon Glassfa20e932020-12-03 16:55:20 -0700599 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100600 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100601 int ret;
Macpaul Linc56c5a32011-09-20 19:54:32 +0000602
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100603 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100604 priv->phy_mode = pdata->phy_interface;
605 priv->max_speed = pdata->max_speed;
606 priv->phy_addr = 0;
607
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930608 if (dev_read_bool(dev, "use-ncsi"))
609 priv->phy_mode = PHY_INTERFACE_MODE_NCSI;
610
Thirupathaiah Annapureddy22bb3772020-08-17 17:08:26 -0700611#ifdef CONFIG_PHY_ADDR
612 priv->phy_addr = CONFIG_PHY_ADDR;
613#endif
614
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100615 ret = clk_enable_bulk(&priv->clks);
616 if (ret)
617 goto out;
618
Dylan Hung1a2ef082023-07-27 09:58:14 +0800619 if (priv->reset_ctl) {
620 ret = reset_deassert(priv->reset_ctl);
621 if (ret)
622 goto out;
623 }
624
Dylan Hung2bec3722021-12-09 10:12:24 +0800625 /*
626 * If DM MDIO is enabled, the MDIO bus will be initialized later in
627 * dm_eth_phy_connect
628 */
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930629 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI &&
630 !IS_ENABLED(CONFIG_DM_MDIO)) {
Dylan Hung2bec3722021-12-09 10:12:24 +0800631 ret = ftgmac100_mdio_init(dev);
632 if (ret) {
633 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
634 goto out;
635 }
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100636 }
637
638 ret = ftgmac100_phy_init(dev);
639 if (ret) {
640 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
641 goto out;
642 }
643
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500644 ftgmac_read_hwaddr(dev);
645
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100646out:
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100647 if (ret)
648 clk_release_bulk(&priv->clks);
649
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100650 return ret;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100651}
Macpaul Lin199c6252010-12-21 16:59:46 +0800652
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100653static int ftgmac100_remove(struct udevice *dev)
654{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100655 struct ftgmac100_data *priv = dev_get_priv(dev);
656
657 free(priv->phydev);
658 mdio_unregister(priv->bus);
659 mdio_free(priv->bus);
Dylan Hung1a2ef082023-07-27 09:58:14 +0800660 if (priv->reset_ctl)
661 reset_assert(priv->reset_ctl);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100662 clk_release_bulk(&priv->clks);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100663
Macpaul Lin199c6252010-12-21 16:59:46 +0800664 return 0;
665}
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100666
667static const struct eth_ops ftgmac100_ops = {
668 .start = ftgmac100_start,
669 .send = ftgmac100_send,
670 .recv = ftgmac100_recv,
671 .stop = ftgmac100_stop,
672 .free_pkt = ftgmac100_free_pkt,
673 .write_hwaddr = ftgmac100_write_hwaddr,
674};
675
676static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100677 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
678 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Dylan Hungc84560a2021-12-09 10:12:25 +0800679 { .compatible = "aspeed,ast2600-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100680 { }
681};
682
683U_BOOT_DRIVER(ftgmac100) = {
684 .name = "ftgmac100",
685 .id = UCLASS_ETH,
686 .of_match = ftgmac100_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700687 .of_to_plat = ftgmac100_of_to_plat,
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100688 .probe = ftgmac100_probe,
689 .remove = ftgmac100_remove,
690 .ops = &ftgmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700691 .priv_auto = sizeof(struct ftgmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700692 .plat_auto = sizeof(struct eth_pdata),
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100693 .flags = DM_FLAG_ALLOC_PRIV_DMA,
694};