blob: 8781e50a48dc81ccc14939b0ea8500bd9f261eb1 [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
Dylan Hung2bec3722021-12-09 10:12:24 +0800225 if (IS_ENABLED(CONFIG_DM_MDIO))
226 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;
Macpaul Lin199c6252010-12-21 16:59:46 +0800324 unsigned int maccr;
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
369 /* poll receive descriptor automatically */
370 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
371
372 /* config receive buffer size register */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100373 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800374
375 /* enable transmitter, receiver */
376 maccr = FTGMAC100_MACCR_TXMAC_EN |
377 FTGMAC100_MACCR_RXMAC_EN |
378 FTGMAC100_MACCR_TXDMA_EN |
379 FTGMAC100_MACCR_RXDMA_EN |
380 FTGMAC100_MACCR_CRC_APD |
381 FTGMAC100_MACCR_FULLDUP |
382 FTGMAC100_MACCR_RX_RUNT |
383 FTGMAC100_MACCR_RX_BROADPKT;
384
385 writel(maccr, &ftgmac100->maccr);
386
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100387 ret = phy_startup(phydev);
388 if (ret) {
389 dev_err(phydev->dev, "Could not start PHY\n");
390 return ret;
391 }
392
393 ret = ftgmac100_phy_adjust_link(priv);
394 if (ret) {
395 dev_err(phydev->dev, "Could not adjust link\n");
396 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800397 }
398
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100399 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
400 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
401
Macpaul Lin199c6252010-12-21 16:59:46 +0800402 return 0;
403}
404
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100405static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
406{
407 struct ftgmac100_data *priv = dev_get_priv(dev);
408 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100409 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100410 ulong des_end = des_start +
411 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100412
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100413 /* Release buffer to DMA and flush descriptor */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100414 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100415 flush_dcache_range(des_start, des_end);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100416
417 /* Move to next descriptor */
418 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
419
420 return 0;
421}
422
Macpaul Lin199c6252010-12-21 16:59:46 +0800423/*
424 * Get a data block via Ethernet
425 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100426static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Lin199c6252010-12-21 16:59:46 +0800427{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100428 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100429 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Lin199c6252010-12-21 16:59:46 +0800430 unsigned short rxlen;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100431 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100432 ulong des_end = des_start +
433 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
434 ulong data_start = curr_des->rxdes3;
435 ulong data_end;
Macpaul Lin199c6252010-12-21 16:59:46 +0800436
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100437 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800438
439 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100440 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800441
442 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
443 FTGMAC100_RXDES0_CRC_ERR |
444 FTGMAC100_RXDES0_FTL |
445 FTGMAC100_RXDES0_RUNT |
446 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100447 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800448 }
449
450 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
451
452 debug("%s(): RX buffer %d, %x received\n",
453 __func__, priv->rx_index, rxlen);
454
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100455 /* Invalidate received data */
456 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
457 invalidate_dcache_range(data_start, data_end);
458 *packetp = (uchar *)data_start;
Macpaul Lin199c6252010-12-21 16:59:46 +0800459
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100460 return rxlen;
Macpaul Lin199c6252010-12-21 16:59:46 +0800461}
462
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100463static u32 ftgmac100_read_txdesc(const void *desc)
464{
465 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100466 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100467 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
468
469 invalidate_dcache_range(des_start, des_end);
470
471 return txdes->txdes0;
472}
473
474BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
475
Macpaul Lin199c6252010-12-21 16:59:46 +0800476/*
477 * Send a data block via Ethernet
478 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100479static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Lin199c6252010-12-21 16:59:46 +0800480{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100481 struct ftgmac100_data *priv = dev_get_priv(dev);
482 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800483 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100484 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100485 ulong des_end = des_start +
486 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
487 ulong data_start;
488 ulong data_end;
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100489 int rc;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100490
491 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800492
493 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100494 dev_err(dev, "no TX descriptor available\n");
495 return -EPERM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800496 }
497
498 debug("%s(%x, %x)\n", __func__, (int)packet, length);
499
500 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
501
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100502 curr_des->txdes3 = (unsigned int)packet;
503
504 /* Flush data to be sent */
505 data_start = curr_des->txdes3;
506 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
507 flush_dcache_range(data_start, data_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800508
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100509 /* Only one segment on TXBUF */
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100510 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +0800511 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
512 FTGMAC100_TXDES0_LTS |
513 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
514 FTGMAC100_TXDES0_TXDMA_OWN ;
515
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100516 /* Flush modified buffer descriptor */
517 flush_dcache_range(des_start, des_end);
518
519 /* Start transmit */
Macpaul Lin199c6252010-12-21 16:59:46 +0800520 writel(1, &ftgmac100->txpd);
521
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100522 rc = wait_for_bit_ftgmac100_txdone(curr_des,
523 FTGMAC100_TXDES0_TXDMA_OWN, false,
524 FTGMAC100_TX_TIMEOUT_MS, true);
525 if (rc)
526 return rc;
527
Macpaul Lin199c6252010-12-21 16:59:46 +0800528 debug("%s(): packet sent\n", __func__);
529
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100530 /* Move to next descriptor */
Macpaul Lin199c6252010-12-21 16:59:46 +0800531 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
532
533 return 0;
534}
535
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100536static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800537{
Simon Glassfa20e932020-12-03 16:55:20 -0700538 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100539 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800540
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100541 return ftgmac100_set_mac(priv, pdata->enetaddr);
542}
Macpaul Lin199c6252010-12-21 16:59:46 +0800543
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500544static int ftgmac_read_hwaddr(struct udevice *dev)
545{
546 struct eth_pdata *pdata = dev_get_plat(dev);
547 struct ftgmac100_data *priv = dev_get_priv(dev);
548
549 return ftgmac100_get_mac(priv, pdata->enetaddr);
550}
551
Simon Glassaad29ae2020-12-03 16:55:21 -0700552static int ftgmac100_of_to_plat(struct udevice *dev)
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100553{
Simon Glassfa20e932020-12-03 16:55:20 -0700554 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100555 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800556
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900557 pdata->iobase = dev_read_addr(dev);
Marek Behúnbc194772022-04-07 00:33:01 +0200558
559 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200560 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100561 return -EINVAL;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100562
563 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
564
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100565 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
566 priv->rxdes0_edorr_mask = BIT(30);
567 priv->txdes0_edotr_mask = BIT(30);
568 } else {
569 priv->rxdes0_edorr_mask = BIT(15);
570 priv->txdes0_edotr_mask = BIT(15);
571 }
572
Dylan Hung1a2ef082023-07-27 09:58:14 +0800573 priv->reset_ctl = devm_reset_control_get_optional(dev, NULL);
574
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100575 return clk_get_bulk(dev, &priv->clks);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100576}
Macpaul Lin199c6252010-12-21 16:59:46 +0800577
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100578static int ftgmac100_probe(struct udevice *dev)
579{
Simon Glassfa20e932020-12-03 16:55:20 -0700580 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100581 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100582 int ret;
Macpaul Linc56c5a32011-09-20 19:54:32 +0000583
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100584 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100585 priv->phy_mode = pdata->phy_interface;
586 priv->max_speed = pdata->max_speed;
587 priv->phy_addr = 0;
588
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930589 if (dev_read_bool(dev, "use-ncsi"))
590 priv->phy_mode = PHY_INTERFACE_MODE_NCSI;
591
Thirupathaiah Annapureddy22bb3772020-08-17 17:08:26 -0700592#ifdef CONFIG_PHY_ADDR
593 priv->phy_addr = CONFIG_PHY_ADDR;
594#endif
595
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100596 ret = clk_enable_bulk(&priv->clks);
597 if (ret)
598 goto out;
599
Dylan Hung1a2ef082023-07-27 09:58:14 +0800600 if (priv->reset_ctl) {
601 ret = reset_deassert(priv->reset_ctl);
602 if (ret)
603 goto out;
604 }
605
Dylan Hung2bec3722021-12-09 10:12:24 +0800606 /*
607 * If DM MDIO is enabled, the MDIO bus will be initialized later in
608 * dm_eth_phy_connect
609 */
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930610 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI &&
611 !IS_ENABLED(CONFIG_DM_MDIO)) {
Dylan Hung2bec3722021-12-09 10:12:24 +0800612 ret = ftgmac100_mdio_init(dev);
613 if (ret) {
614 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
615 goto out;
616 }
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100617 }
618
619 ret = ftgmac100_phy_init(dev);
620 if (ret) {
621 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
622 goto out;
623 }
624
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500625 ftgmac_read_hwaddr(dev);
626
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100627out:
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100628 if (ret)
629 clk_release_bulk(&priv->clks);
630
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100631 return ret;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100632}
Macpaul Lin199c6252010-12-21 16:59:46 +0800633
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100634static int ftgmac100_remove(struct udevice *dev)
635{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100636 struct ftgmac100_data *priv = dev_get_priv(dev);
637
638 free(priv->phydev);
639 mdio_unregister(priv->bus);
640 mdio_free(priv->bus);
Dylan Hung1a2ef082023-07-27 09:58:14 +0800641 if (priv->reset_ctl)
642 reset_assert(priv->reset_ctl);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100643 clk_release_bulk(&priv->clks);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100644
Macpaul Lin199c6252010-12-21 16:59:46 +0800645 return 0;
646}
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100647
648static const struct eth_ops ftgmac100_ops = {
649 .start = ftgmac100_start,
650 .send = ftgmac100_send,
651 .recv = ftgmac100_recv,
652 .stop = ftgmac100_stop,
653 .free_pkt = ftgmac100_free_pkt,
654 .write_hwaddr = ftgmac100_write_hwaddr,
655};
656
657static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100658 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
659 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Dylan Hungc84560a2021-12-09 10:12:25 +0800660 { .compatible = "aspeed,ast2600-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100661 { }
662};
663
664U_BOOT_DRIVER(ftgmac100) = {
665 .name = "ftgmac100",
666 .id = UCLASS_ETH,
667 .of_match = ftgmac100_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700668 .of_to_plat = ftgmac100_of_to_plat,
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100669 .probe = ftgmac100_probe,
670 .remove = ftgmac100_remove,
671 .ops = &ftgmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700672 .priv_auto = sizeof(struct ftgmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700673 .plat_auto = sizeof(struct eth_pdata),
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100674 .flags = DM_FLAG_ALLOC_PRIV_DMA,
675};