blob: 587d3658fa9fb7b03d859449b8849739e01e3c0a [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
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <common.h>
Cédric Le Goater6afa3f12018-10-29 07:06:36 +010015#include <clk.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;
Cédric Le Goater35efcbb2018-10-29 07:06:38 +010094
95 /* End of RX/TX ring buffer bits. Depend on model */
96 u32 rxdes0_edorr_mask;
97 u32 txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +080098};
99
100/*
101 * struct mii_bus functions
102 */
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100103static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
104 int reg_addr)
Macpaul Lin199c6252010-12-21 16:59:46 +0800105{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100106 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100107 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800108 int phycr;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100109 int data;
110 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800111
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100112 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
113 FTGMAC100_PHYCR_PHYAD(phy_addr) |
114 FTGMAC100_PHYCR_REGAD(reg_addr) |
115 FTGMAC100_PHYCR_MIIRD;
Macpaul Lin199c6252010-12-21 16:59:46 +0800116 writel(phycr, &ftgmac100->phycr);
117
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100118 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
119 !(phycr & FTGMAC100_PHYCR_MIIRD),
120 FTGMAC100_MDIO_TIMEOUT_USEC);
121 if (ret) {
122 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
Zev Weiss4e5ca0f2022-05-17 15:16:39 -0700123 bus->name, phy_addr, reg_addr);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100124 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800125 }
126
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100127 data = readl(&ftgmac100->phydata);
128
129 return FTGMAC100_PHYDATA_MIIRDATA(data);
Macpaul Lin199c6252010-12-21 16:59:46 +0800130}
131
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100132static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
133 int reg_addr, u16 value)
Macpaul Lin199c6252010-12-21 16:59:46 +0800134{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100135 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100136 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800137 int phycr;
138 int data;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100139 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800140
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100141 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
142 FTGMAC100_PHYCR_PHYAD(phy_addr) |
143 FTGMAC100_PHYCR_REGAD(reg_addr) |
144 FTGMAC100_PHYCR_MIIWR;
Macpaul Lin199c6252010-12-21 16:59:46 +0800145 data = FTGMAC100_PHYDATA_MIIWDATA(value);
146
147 writel(data, &ftgmac100->phydata);
148 writel(phycr, &ftgmac100->phycr);
149
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100150 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
151 !(phycr & FTGMAC100_PHYCR_MIIWR),
152 FTGMAC100_MDIO_TIMEOUT_USEC);
153 if (ret) {
154 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
Zev Weiss4e5ca0f2022-05-17 15:16:39 -0700155 bus->name, phy_addr, reg_addr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800156 }
157
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100158 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800159}
160
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100161static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800162{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100163 struct ftgmac100_data *priv = dev_get_priv(dev);
164 struct mii_dev *bus;
165 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800166
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100167 bus = mdio_alloc();
168 if (!bus)
169 return -ENOMEM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800170
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100171 bus->read = ftgmac100_mdio_read;
172 bus->write = ftgmac100_mdio_write;
173 bus->priv = priv;
Macpaul Lin199c6252010-12-21 16:59:46 +0800174
Simon Glass75e534b2020-12-16 21:20:07 -0700175 ret = mdio_register_seq(bus, dev_seq(dev));
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100176 if (ret) {
177 free(bus);
178 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800179 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800180
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100181 priv->bus = bus;
Macpaul Lin199c6252010-12-21 16:59:46 +0800182
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100183 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800184}
185
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100186static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800187{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100188 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100189 struct phy_device *phydev = priv->phydev;
190 u32 maccr;
Macpaul Lin199c6252010-12-21 16:59:46 +0800191
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930192 if (!phydev->link && priv->phy_mode != PHY_INTERFACE_MODE_NCSI) {
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100193 dev_err(phydev->dev, "No link\n");
194 return -EREMOTEIO;
195 }
Macpaul Lin199c6252010-12-21 16:59:46 +0800196
197 /* read MAC control register and clear related bits */
198 maccr = readl(&ftgmac100->maccr) &
199 ~(FTGMAC100_MACCR_GIGA_MODE |
200 FTGMAC100_MACCR_FAST_MODE |
201 FTGMAC100_MACCR_FULLDUP);
202
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100203 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Lin199c6252010-12-21 16:59:46 +0800204 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800205
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100206 if (phydev->speed == 100)
Macpaul Lin199c6252010-12-21 16:59:46 +0800207 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Lin199c6252010-12-21 16:59:46 +0800208
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100209 if (phydev->duplex)
210 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Lin199c6252010-12-21 16:59:46 +0800211
212 /* update MII config into maccr */
213 writel(maccr, &ftgmac100->maccr);
214
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100215 return 0;
216}
217
218static int ftgmac100_phy_init(struct udevice *dev)
219{
220 struct ftgmac100_data *priv = dev_get_priv(dev);
221 struct phy_device *phydev;
222 int ret;
223
Dylan Hung2bec3722021-12-09 10:12:24 +0800224 if (IS_ENABLED(CONFIG_DM_MDIO))
225 phydev = dm_eth_phy_connect(dev);
226 else
227 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
228
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100229 if (!phydev)
230 return -ENODEV;
231
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930232 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
233 phydev->supported &= PHY_GBIT_FEATURES;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100234 if (priv->max_speed) {
235 ret = phy_set_supported(phydev, priv->max_speed);
236 if (ret)
237 return ret;
238 }
239 phydev->advertising = phydev->supported;
240 priv->phydev = phydev;
241 phy_config(phydev);
242
243 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800244}
245
246/*
247 * Reset MAC
248 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100249static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Lin199c6252010-12-21 16:59:46 +0800250{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100251 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800252
253 debug("%s()\n", __func__);
254
Cédric Le Goatercef951c2018-10-29 07:06:32 +0100255 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Lin199c6252010-12-21 16:59:46 +0800256
257 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
258 ;
259}
260
261/*
262 * Set MAC address
263 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100264static int ftgmac100_set_mac(struct ftgmac100_data *priv,
265 const unsigned char *mac)
Macpaul Lin199c6252010-12-21 16:59:46 +0800266{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100267 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800268 unsigned int maddr = mac[0] << 8 | mac[1];
269 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
270
271 debug("%s(%x %x)\n", __func__, maddr, laddr);
272
273 writel(maddr, &ftgmac100->mac_madr);
274 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800275
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100276 return 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800277}
278
279/*
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500280 * Get MAC address
281 */
282static int ftgmac100_get_mac(struct ftgmac100_data *priv,
283 unsigned char *mac)
284{
285 struct ftgmac100 *ftgmac100 = priv->iobase;
286 unsigned int maddr = readl(&ftgmac100->mac_madr);
287 unsigned int laddr = readl(&ftgmac100->mac_ladr);
288
289 debug("%s(%x %x)\n", __func__, maddr, laddr);
290
291 mac[0] = (maddr >> 8) & 0xff;
292 mac[1] = maddr & 0xff;
293 mac[2] = (laddr >> 24) & 0xff;
294 mac[3] = (laddr >> 16) & 0xff;
295 mac[4] = (laddr >> 8) & 0xff;
296 mac[5] = laddr & 0xff;
297
298 return 0;
299}
300
301/*
Macpaul Lin199c6252010-12-21 16:59:46 +0800302 * disable transmitter, receiver
303 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100304static void ftgmac100_stop(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800305{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100306 struct ftgmac100_data *priv = dev_get_priv(dev);
307 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800308
309 debug("%s()\n", __func__);
310
311 writel(0, &ftgmac100->maccr);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100312
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930313 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
314 phy_shutdown(priv->phydev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800315}
316
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100317static int ftgmac100_start(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800318{
Simon Glassfa20e932020-12-03 16:55:20 -0700319 struct eth_pdata *plat = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100320 struct ftgmac100_data *priv = dev_get_priv(dev);
321 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100322 struct phy_device *phydev = priv->phydev;
Macpaul Lin199c6252010-12-21 16:59:46 +0800323 unsigned int maccr;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100324 ulong start, end;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100325 int ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800326 int i;
327
328 debug("%s()\n", __func__);
329
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100330 ftgmac100_reset(priv);
331
Macpaul Lin199c6252010-12-21 16:59:46 +0800332 /* set the ethernet address */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100333 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800334
335 /* disable all interrupts */
336 writel(0, &ftgmac100->ier);
337
338 /* initialize descriptors */
339 priv->tx_index = 0;
340 priv->rx_index = 0;
341
Macpaul Lin199c6252010-12-21 16:59:46 +0800342 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100343 priv->txdes[i].txdes3 = 0;
344 priv->txdes[i].txdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800345 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100346 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100347
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100348 start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100349 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
350 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800351
352 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100353 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
354 priv->rxdes[i].rxdes0 = 0;
Macpaul Lin199c6252010-12-21 16:59:46 +0800355 }
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100356 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100357
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100358 start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100359 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
360 flush_dcache_range(start, end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800361
362 /* transmit ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100363 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800364
365 /* receive ring */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100366 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800367
368 /* poll receive descriptor automatically */
369 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
370
371 /* config receive buffer size register */
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100372 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Lin199c6252010-12-21 16:59:46 +0800373
374 /* enable transmitter, receiver */
375 maccr = FTGMAC100_MACCR_TXMAC_EN |
376 FTGMAC100_MACCR_RXMAC_EN |
377 FTGMAC100_MACCR_TXDMA_EN |
378 FTGMAC100_MACCR_RXDMA_EN |
379 FTGMAC100_MACCR_CRC_APD |
380 FTGMAC100_MACCR_FULLDUP |
381 FTGMAC100_MACCR_RX_RUNT |
382 FTGMAC100_MACCR_RX_BROADPKT;
383
384 writel(maccr, &ftgmac100->maccr);
385
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100386 ret = phy_startup(phydev);
387 if (ret) {
388 dev_err(phydev->dev, "Could not start PHY\n");
389 return ret;
390 }
391
392 ret = ftgmac100_phy_adjust_link(priv);
393 if (ret) {
394 dev_err(phydev->dev, "Could not adjust link\n");
395 return ret;
Macpaul Lin199c6252010-12-21 16:59:46 +0800396 }
397
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100398 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
399 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
400
Macpaul Lin199c6252010-12-21 16:59:46 +0800401 return 0;
402}
403
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100404static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
405{
406 struct ftgmac100_data *priv = dev_get_priv(dev);
407 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100408 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100409 ulong des_end = des_start +
410 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100411
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100412 /* Release buffer to DMA and flush descriptor */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100413 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100414 flush_dcache_range(des_start, des_end);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100415
416 /* Move to next descriptor */
417 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
418
419 return 0;
420}
421
Macpaul Lin199c6252010-12-21 16:59:46 +0800422/*
423 * Get a data block via Ethernet
424 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100425static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Lin199c6252010-12-21 16:59:46 +0800426{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100427 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100428 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Lin199c6252010-12-21 16:59:46 +0800429 unsigned short rxlen;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100430 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100431 ulong des_end = des_start +
432 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
433 ulong data_start = curr_des->rxdes3;
434 ulong data_end;
Macpaul Lin199c6252010-12-21 16:59:46 +0800435
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100436 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800437
438 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100439 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800440
441 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
442 FTGMAC100_RXDES0_CRC_ERR |
443 FTGMAC100_RXDES0_FTL |
444 FTGMAC100_RXDES0_RUNT |
445 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100446 return -EAGAIN;
Macpaul Lin199c6252010-12-21 16:59:46 +0800447 }
448
449 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
450
451 debug("%s(): RX buffer %d, %x received\n",
452 __func__, priv->rx_index, rxlen);
453
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100454 /* Invalidate received data */
455 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
456 invalidate_dcache_range(data_start, data_end);
457 *packetp = (uchar *)data_start;
Macpaul Lin199c6252010-12-21 16:59:46 +0800458
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100459 return rxlen;
Macpaul Lin199c6252010-12-21 16:59:46 +0800460}
461
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100462static u32 ftgmac100_read_txdesc(const void *desc)
463{
464 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100465 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100466 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
467
468 invalidate_dcache_range(des_start, des_end);
469
470 return txdes->txdes0;
471}
472
473BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
474
Macpaul Lin199c6252010-12-21 16:59:46 +0800475/*
476 * Send a data block via Ethernet
477 */
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100478static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Lin199c6252010-12-21 16:59:46 +0800479{
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100480 struct ftgmac100_data *priv = dev_get_priv(dev);
481 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Lin199c6252010-12-21 16:59:46 +0800482 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater0404e9f2019-11-28 13:37:04 +0100483 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100484 ulong des_end = des_start +
485 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
486 ulong data_start;
487 ulong data_end;
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100488 int rc;
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100489
490 invalidate_dcache_range(des_start, des_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800491
492 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100493 dev_err(dev, "no TX descriptor available\n");
494 return -EPERM;
Macpaul Lin199c6252010-12-21 16:59:46 +0800495 }
496
497 debug("%s(%x, %x)\n", __func__, (int)packet, length);
498
499 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
500
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100501 curr_des->txdes3 = (unsigned int)packet;
502
503 /* Flush data to be sent */
504 data_start = curr_des->txdes3;
505 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
506 flush_dcache_range(data_start, data_end);
Macpaul Lin199c6252010-12-21 16:59:46 +0800507
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100508 /* Only one segment on TXBUF */
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100509 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Lin199c6252010-12-21 16:59:46 +0800510 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
511 FTGMAC100_TXDES0_LTS |
512 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
513 FTGMAC100_TXDES0_TXDMA_OWN ;
514
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100515 /* Flush modified buffer descriptor */
516 flush_dcache_range(des_start, des_end);
517
518 /* Start transmit */
Macpaul Lin199c6252010-12-21 16:59:46 +0800519 writel(1, &ftgmac100->txpd);
520
Cédric Le Goater9bcb6652018-10-29 07:06:35 +0100521 rc = wait_for_bit_ftgmac100_txdone(curr_des,
522 FTGMAC100_TXDES0_TXDMA_OWN, false,
523 FTGMAC100_TX_TIMEOUT_MS, true);
524 if (rc)
525 return rc;
526
Macpaul Lin199c6252010-12-21 16:59:46 +0800527 debug("%s(): packet sent\n", __func__);
528
Cédric Le Goater3174dfb2018-10-29 07:06:34 +0100529 /* Move to next descriptor */
Macpaul Lin199c6252010-12-21 16:59:46 +0800530 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
531
532 return 0;
533}
534
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100535static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Lin199c6252010-12-21 16:59:46 +0800536{
Simon Glassfa20e932020-12-03 16:55:20 -0700537 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100538 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800539
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100540 return ftgmac100_set_mac(priv, pdata->enetaddr);
541}
Macpaul Lin199c6252010-12-21 16:59:46 +0800542
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500543static int ftgmac_read_hwaddr(struct udevice *dev)
544{
545 struct eth_pdata *pdata = dev_get_plat(dev);
546 struct ftgmac100_data *priv = dev_get_priv(dev);
547
548 return ftgmac100_get_mac(priv, pdata->enetaddr);
549}
550
Simon Glassaad29ae2020-12-03 16:55:21 -0700551static int ftgmac100_of_to_plat(struct udevice *dev)
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100552{
Simon Glassfa20e932020-12-03 16:55:20 -0700553 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100554 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Lin199c6252010-12-21 16:59:46 +0800555
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900556 pdata->iobase = dev_read_addr(dev);
Marek Behúnbc194772022-04-07 00:33:01 +0200557
558 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behún48631e42022-04-07 00:33:03 +0200559 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100560 return -EINVAL;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100561
562 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
563
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100564 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
565 priv->rxdes0_edorr_mask = BIT(30);
566 priv->txdes0_edotr_mask = BIT(30);
567 } else {
568 priv->rxdes0_edorr_mask = BIT(15);
569 priv->txdes0_edotr_mask = BIT(15);
570 }
571
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100572 return clk_get_bulk(dev, &priv->clks);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100573}
Macpaul Lin199c6252010-12-21 16:59:46 +0800574
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100575static int ftgmac100_probe(struct udevice *dev)
576{
Simon Glassfa20e932020-12-03 16:55:20 -0700577 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100578 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100579 int ret;
Macpaul Linc56c5a32011-09-20 19:54:32 +0000580
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100581 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100582 priv->phy_mode = pdata->phy_interface;
583 priv->max_speed = pdata->max_speed;
584 priv->phy_addr = 0;
585
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930586 if (dev_read_bool(dev, "use-ncsi"))
587 priv->phy_mode = PHY_INTERFACE_MODE_NCSI;
588
Thirupathaiah Annapureddy22bb3772020-08-17 17:08:26 -0700589#ifdef CONFIG_PHY_ADDR
590 priv->phy_addr = CONFIG_PHY_ADDR;
591#endif
592
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100593 ret = clk_enable_bulk(&priv->clks);
594 if (ret)
595 goto out;
596
Dylan Hung2bec3722021-12-09 10:12:24 +0800597 /*
598 * If DM MDIO is enabled, the MDIO bus will be initialized later in
599 * dm_eth_phy_connect
600 */
Samuel Mendoza-Jonascf4c4f92022-08-08 21:46:05 +0930601 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI &&
602 !IS_ENABLED(CONFIG_DM_MDIO)) {
Dylan Hung2bec3722021-12-09 10:12:24 +0800603 ret = ftgmac100_mdio_init(dev);
604 if (ret) {
605 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
606 goto out;
607 }
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100608 }
609
610 ret = ftgmac100_phy_init(dev);
611 if (ret) {
612 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
613 goto out;
614 }
615
Hongwei Zhang55ae5902020-12-10 18:11:09 -0500616 ftgmac_read_hwaddr(dev);
617
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100618out:
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100619 if (ret)
620 clk_release_bulk(&priv->clks);
621
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100622 return ret;
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100623}
Macpaul Lin199c6252010-12-21 16:59:46 +0800624
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100625static int ftgmac100_remove(struct udevice *dev)
626{
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100627 struct ftgmac100_data *priv = dev_get_priv(dev);
628
629 free(priv->phydev);
630 mdio_unregister(priv->bus);
631 mdio_free(priv->bus);
Cédric Le Goater6afa3f12018-10-29 07:06:36 +0100632 clk_release_bulk(&priv->clks);
Cédric Le Goater7f8482a2018-10-29 07:06:33 +0100633
Macpaul Lin199c6252010-12-21 16:59:46 +0800634 return 0;
635}
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100636
637static const struct eth_ops ftgmac100_ops = {
638 .start = ftgmac100_start,
639 .send = ftgmac100_send,
640 .recv = ftgmac100_recv,
641 .stop = ftgmac100_stop,
642 .free_pkt = ftgmac100_free_pkt,
643 .write_hwaddr = ftgmac100_write_hwaddr,
644};
645
646static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goater35efcbb2018-10-29 07:06:38 +0100647 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
648 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Dylan Hungc84560a2021-12-09 10:12:25 +0800649 { .compatible = "aspeed,ast2600-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100650 { }
651};
652
653U_BOOT_DRIVER(ftgmac100) = {
654 .name = "ftgmac100",
655 .id = UCLASS_ETH,
656 .of_match = ftgmac100_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700657 .of_to_plat = ftgmac100_of_to_plat,
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100658 .probe = ftgmac100_probe,
659 .remove = ftgmac100_remove,
660 .ops = &ftgmac100_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700661 .priv_auto = sizeof(struct ftgmac100_data),
Simon Glass71fa5b42020-12-03 16:55:18 -0700662 .plat_auto = sizeof(struct eth_pdata),
Cédric Le Goater38b33e92018-10-29 07:06:31 +0100663 .flags = DM_FLAG_ALLOC_PRIV_DMA,
664};