blob: 9b536fd5ab890dbbbbda76fbe6fde7fc7c57bb83 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Macpaul Linb3dbf4a52010-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 Goaterf95de0b2018-10-29 07:06:31 +010010 *
11 * Copyright (C) 2018, IBM Corporation.
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080012 */
13
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <common.h>
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010015#include <clk.h>
Dylan Hung607e7fa2023-07-27 09:58:14 +080016#include <reset.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070017#include <cpu_func.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010018#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <malloc.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010021#include <miiphy.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080022#include <net.h>
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010023#include <wait_bit.h>
Simon Glass90526e92020-05-10 11:39:56 -060024#include <asm/cache.h>
Simon Glass336d4612020-02-03 07:36:16 -070025#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010027#include <linux/io.h>
Cédric Le Goater538e75d2018-10-29 07:06:33 +010028#include <linux/iopoll.h>
Simon Glass1e94b462023-09-14 18:21:46 -060029#include <linux/printk.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080030
31#include "ftgmac100.h"
32
Cédric Le Goatere7668492018-10-29 07:06:34 +010033/* Min frame ethernet frame size without FCS */
34#define ETH_ZLEN 60
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080035
Cédric Le Goatere7668492018-10-29 07:06:34 +010036/* Receive Buffer Size Register - HW default is 0x640 */
37#define FTGMAC100_RBSR_DEFAULT 0x640
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080038
39/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
40#define PKTBUFSTX 4 /* must be power of 2 */
41
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010042/* Timeout for transmit */
43#define FTGMAC100_TX_TIMEOUT_MS 1000
44
Cédric Le Goater538e75d2018-10-29 07:06:33 +010045/* Timeout for a mdio read/write operation */
46#define FTGMAC100_MDIO_TIMEOUT_USEC 10000
47
48/*
49 * MDC clock cycle threshold
50 *
51 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
52 */
53#define MDC_CYCTHR 0x34
54
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010055/*
56 * ftgmac100 model variants
57 */
58enum ftgmac100_model {
59 FTGMAC100_MODEL_FARADAY,
60 FTGMAC100_MODEL_ASPEED,
61};
62
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010063/**
64 * struct ftgmac100_data - private data for the FTGMAC100 driver
65 *
66 * @iobase: The base address of the hardware registers
67 * @txdes: The array of transmit descriptors
68 * @rxdes: The array of receive descriptors
69 * @tx_index: Transmit descriptor index in @txdes
70 * @rx_index: Receive descriptor index in @rxdes
71 * @phy_addr: The PHY interface address to use
Cédric Le Goater538e75d2018-10-29 07:06:33 +010072 * @phydev: The PHY device backing the MAC
73 * @bus: The mdio bus
74 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
75 * @max_speed: Maximum speed of Ethernet connection supported by MAC
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010076 * @clks: The bulk of clocks assigned to the device in the DT
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010077 * @rxdes0_edorr_mask: The bit number identifying the end of the RX ring buffer
78 * @txdes0_edotr_mask: The bit number identifying the end of the TX ring buffer
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010079 */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080080struct ftgmac100_data {
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010081 struct ftgmac100 *iobase;
82
Cédric Le Goater08b3e902019-11-28 13:37:04 +010083 struct ftgmac100_txdes txdes[PKTBUFSTX] __aligned(ARCH_DMA_MINALIGN);
84 struct ftgmac100_rxdes rxdes[PKTBUFSRX] __aligned(ARCH_DMA_MINALIGN);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080085 int tx_index;
86 int rx_index;
Cédric Le Goater538e75d2018-10-29 07:06:33 +010087
88 u32 phy_addr;
89 struct phy_device *phydev;
90 struct mii_dev *bus;
91 u32 phy_mode;
92 u32 max_speed;
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010093
94 struct clk_bulk clks;
Dylan Hung607e7fa2023-07-27 09:58:14 +080095 struct reset_ctl *reset_ctl;
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010096
97 /* End of RX/TX ring buffer bits. Depend on model */
98 u32 rxdes0_edorr_mask;
99 u32 txdes0_edotr_mask;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800100};
101
102/*
103 * struct mii_bus functions
104 */
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100105static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
106 int reg_addr)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800107{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100108 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100109 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800110 int phycr;
111 int data;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100112 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800113
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100114 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
115 FTGMAC100_PHYCR_PHYAD(phy_addr) |
116 FTGMAC100_PHYCR_REGAD(reg_addr) |
117 FTGMAC100_PHYCR_MIIRD;
118 writel(phycr, &ftgmac100->phycr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800119
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100120 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
121 !(phycr & FTGMAC100_PHYCR_MIIRD),
122 FTGMAC100_MDIO_TIMEOUT_USEC);
123 if (ret) {
124 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
Zev Weissf44bf732022-05-17 15:16:39 -0700125 bus->name, phy_addr, reg_addr);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100126 return ret;
127 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800128
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100129 data = readl(&ftgmac100->phydata);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800130
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100131 return FTGMAC100_PHYDATA_MIIRDATA(data);
132}
133
134static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
135 int reg_addr, u16 value)
136{
137 struct ftgmac100_data *priv = bus->priv;
138 struct ftgmac100 *ftgmac100 = priv->iobase;
139 int phycr;
140 int data;
141 int ret;
142
143 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
144 FTGMAC100_PHYCR_PHYAD(phy_addr) |
145 FTGMAC100_PHYCR_REGAD(reg_addr) |
146 FTGMAC100_PHYCR_MIIWR;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800147 data = FTGMAC100_PHYDATA_MIIWDATA(value);
148
149 writel(data, &ftgmac100->phydata);
150 writel(phycr, &ftgmac100->phycr);
151
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100152 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
153 !(phycr & FTGMAC100_PHYCR_MIIWR),
154 FTGMAC100_MDIO_TIMEOUT_USEC);
155 if (ret) {
156 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
Zev Weissf44bf732022-05-17 15:16:39 -0700157 bus->name, phy_addr, reg_addr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800158 }
159
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100160 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800161}
162
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100163static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800164{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100165 struct ftgmac100_data *priv = dev_get_priv(dev);
166 struct mii_dev *bus;
167 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800168
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100169 bus = mdio_alloc();
170 if (!bus)
171 return -ENOMEM;
172
173 bus->read = ftgmac100_mdio_read;
174 bus->write = ftgmac100_mdio_write;
175 bus->priv = priv;
176
Simon Glass8b85dfc2020-12-16 21:20:07 -0700177 ret = mdio_register_seq(bus, dev_seq(dev));
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100178 if (ret) {
179 free(bus);
180 return ret;
181 }
182
183 priv->bus = bus;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800184
185 return 0;
186}
187
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100188static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800189{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100190 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100191 struct phy_device *phydev = priv->phydev;
192 u32 maccr;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800193
Samuel Mendoza-Jonas3b400e82022-08-08 21:46:05 +0930194 if (!phydev->link && priv->phy_mode != PHY_INTERFACE_MODE_NCSI) {
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100195 dev_err(phydev->dev, "No link\n");
196 return -EREMOTEIO;
197 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800198
199 /* read MAC control register and clear related bits */
200 maccr = readl(&ftgmac100->maccr) &
201 ~(FTGMAC100_MACCR_GIGA_MODE |
202 FTGMAC100_MACCR_FAST_MODE |
203 FTGMAC100_MACCR_FULLDUP);
204
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100205 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800206 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800207
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100208 if (phydev->speed == 100)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800209 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800210
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100211 if (phydev->duplex)
212 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800213
214 /* update MII config into maccr */
215 writel(maccr, &ftgmac100->maccr);
216
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100217 return 0;
218}
219
220static int ftgmac100_phy_init(struct udevice *dev)
221{
222 struct ftgmac100_data *priv = dev_get_priv(dev);
223 struct phy_device *phydev;
224 int ret;
225
Dylan Hung9c27ce72021-12-09 10:12:24 +0800226 if (IS_ENABLED(CONFIG_DM_MDIO))
227 phydev = dm_eth_phy_connect(dev);
228 else
229 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
230
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100231 if (!phydev)
232 return -ENODEV;
233
Samuel Mendoza-Jonas3b400e82022-08-08 21:46:05 +0930234 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
235 phydev->supported &= PHY_GBIT_FEATURES;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100236 if (priv->max_speed) {
237 ret = phy_set_supported(phydev, priv->max_speed);
238 if (ret)
239 return ret;
240 }
241 phydev->advertising = phydev->supported;
242 priv->phydev = phydev;
243 phy_config(phydev);
244
245 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800246}
247
248/*
249 * Reset MAC
250 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100251static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800252{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100253 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800254
255 debug("%s()\n", __func__);
256
Cédric Le Goater591ffd92018-10-29 07:06:32 +0100257 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800258
259 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
260 ;
261}
262
263/*
264 * Set MAC address
265 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100266static int ftgmac100_set_mac(struct ftgmac100_data *priv,
267 const unsigned char *mac)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800268{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100269 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800270 unsigned int maddr = mac[0] << 8 | mac[1];
271 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
272
273 debug("%s(%x %x)\n", __func__, maddr, laddr);
274
275 writel(maddr, &ftgmac100->mac_madr);
276 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800277
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100278 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800279}
280
281/*
Hongwei Zhang0be3d1f2020-12-10 18:11:09 -0500282 * Get MAC address
283 */
284static int ftgmac100_get_mac(struct ftgmac100_data *priv,
285 unsigned char *mac)
286{
287 struct ftgmac100 *ftgmac100 = priv->iobase;
288 unsigned int maddr = readl(&ftgmac100->mac_madr);
289 unsigned int laddr = readl(&ftgmac100->mac_ladr);
290
291 debug("%s(%x %x)\n", __func__, maddr, laddr);
292
293 mac[0] = (maddr >> 8) & 0xff;
294 mac[1] = maddr & 0xff;
295 mac[2] = (laddr >> 24) & 0xff;
296 mac[3] = (laddr >> 16) & 0xff;
297 mac[4] = (laddr >> 8) & 0xff;
298 mac[5] = laddr & 0xff;
299
300 return 0;
301}
302
303/*
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800304 * disable transmitter, receiver
305 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100306static void ftgmac100_stop(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800307{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100308 struct ftgmac100_data *priv = dev_get_priv(dev);
309 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800310
311 debug("%s()\n", __func__);
312
313 writel(0, &ftgmac100->maccr);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100314
Samuel Mendoza-Jonas3b400e82022-08-08 21:46:05 +0930315 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI)
316 phy_shutdown(priv->phydev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800317}
318
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100319static int ftgmac100_start(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800320{
Simon Glassc69cda22020-12-03 16:55:20 -0700321 struct eth_pdata *plat = dev_get_plat(dev);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100322 struct ftgmac100_data *priv = dev_get_priv(dev);
323 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100324 struct phy_device *phydev = priv->phydev;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800325 unsigned int maccr;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100326 ulong start, end;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100327 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800328 int i;
329
330 debug("%s()\n", __func__);
331
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100332 ftgmac100_reset(priv);
333
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800334 /* set the ethernet address */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100335 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800336
337 /* disable all interrupts */
338 writel(0, &ftgmac100->ier);
339
340 /* initialize descriptors */
341 priv->tx_index = 0;
342 priv->rx_index = 0;
343
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800344 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100345 priv->txdes[i].txdes3 = 0;
346 priv->txdes[i].txdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800347 }
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100348 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100349
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100350 start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100351 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
352 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800353
354 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100355 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
356 priv->rxdes[i].rxdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800357 }
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100358 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100359
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100360 start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100361 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
362 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800363
364 /* transmit ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100365 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800366
367 /* receive ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100368 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800369
370 /* poll receive descriptor automatically */
371 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
372
373 /* config receive buffer size register */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100374 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800375
376 /* enable transmitter, receiver */
377 maccr = FTGMAC100_MACCR_TXMAC_EN |
378 FTGMAC100_MACCR_RXMAC_EN |
379 FTGMAC100_MACCR_TXDMA_EN |
380 FTGMAC100_MACCR_RXDMA_EN |
381 FTGMAC100_MACCR_CRC_APD |
382 FTGMAC100_MACCR_FULLDUP |
383 FTGMAC100_MACCR_RX_RUNT |
384 FTGMAC100_MACCR_RX_BROADPKT;
385
386 writel(maccr, &ftgmac100->maccr);
387
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100388 ret = phy_startup(phydev);
389 if (ret) {
390 dev_err(phydev->dev, "Could not start PHY\n");
391 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800392 }
393
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100394 ret = ftgmac100_phy_adjust_link(priv);
395 if (ret) {
396 dev_err(phydev->dev, "Could not adjust link\n");
397 return ret;
398 }
399
400 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
401 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
402
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800403 return 0;
404}
405
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100406static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
407{
408 struct ftgmac100_data *priv = dev_get_priv(dev);
409 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100410 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100411 ulong des_end = des_start +
412 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100413
Cédric Le Goatere7668492018-10-29 07:06:34 +0100414 /* Release buffer to DMA and flush descriptor */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100415 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100416 flush_dcache_range(des_start, des_end);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100417
418 /* Move to next descriptor */
419 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
420
421 return 0;
422}
423
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800424/*
425 * Get a data block via Ethernet
426 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100427static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800428{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100429 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100430 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800431 unsigned short rxlen;
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100432 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100433 ulong des_end = des_start +
434 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
435 ulong data_start = curr_des->rxdes3;
436 ulong data_end;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800437
Cédric Le Goatere7668492018-10-29 07:06:34 +0100438 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800439
440 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goatere7668492018-10-29 07:06:34 +0100441 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800442
443 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
444 FTGMAC100_RXDES0_CRC_ERR |
445 FTGMAC100_RXDES0_FTL |
446 FTGMAC100_RXDES0_RUNT |
447 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100448 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800449 }
450
451 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
452
453 debug("%s(): RX buffer %d, %x received\n",
454 __func__, priv->rx_index, rxlen);
455
Cédric Le Goatere7668492018-10-29 07:06:34 +0100456 /* Invalidate received data */
457 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
458 invalidate_dcache_range(data_start, data_end);
459 *packetp = (uchar *)data_start;
Kuo-Jung Sua8f9cd12013-05-07 14:33:51 +0800460
Cédric Le Goatere7668492018-10-29 07:06:34 +0100461 return rxlen;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800462}
463
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100464static u32 ftgmac100_read_txdesc(const void *desc)
465{
466 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100467 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100468 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
469
470 invalidate_dcache_range(des_start, des_end);
471
472 return txdes->txdes0;
473}
474
475BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
476
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800477/*
478 * Send a data block via Ethernet
479 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100480static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800481{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100482 struct ftgmac100_data *priv = dev_get_priv(dev);
483 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800484 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100485 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100486 ulong des_end = des_start +
487 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
488 ulong data_start;
489 ulong data_end;
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100490 int rc;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100491
492 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800493
494 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100495 dev_err(dev, "no TX descriptor available\n");
496 return -EPERM;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800497 }
498
499 debug("%s(%x, %x)\n", __func__, (int)packet, length);
500
501 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
502
Cédric Le Goatere7668492018-10-29 07:06:34 +0100503 curr_des->txdes3 = (unsigned int)packet;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800504
Cédric Le Goatere7668492018-10-29 07:06:34 +0100505 /* Flush data to be sent */
506 data_start = curr_des->txdes3;
507 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
508 flush_dcache_range(data_start, data_end);
509
510 /* Only one segment on TXBUF */
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100511 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800512 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
513 FTGMAC100_TXDES0_LTS |
514 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
515 FTGMAC100_TXDES0_TXDMA_OWN ;
516
Cédric Le Goatere7668492018-10-29 07:06:34 +0100517 /* Flush modified buffer descriptor */
518 flush_dcache_range(des_start, des_end);
519
520 /* Start transmit */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800521 writel(1, &ftgmac100->txpd);
522
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100523 rc = wait_for_bit_ftgmac100_txdone(curr_des,
524 FTGMAC100_TXDES0_TXDMA_OWN, false,
525 FTGMAC100_TX_TIMEOUT_MS, true);
526 if (rc)
527 return rc;
528
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800529 debug("%s(): packet sent\n", __func__);
530
Cédric Le Goatere7668492018-10-29 07:06:34 +0100531 /* Move to next descriptor */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800532 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
533
534 return 0;
535}
536
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100537static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800538{
Simon Glassc69cda22020-12-03 16:55:20 -0700539 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100540 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800541
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100542 return ftgmac100_set_mac(priv, pdata->enetaddr);
543}
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800544
Hongwei Zhang0be3d1f2020-12-10 18:11:09 -0500545static int ftgmac_read_hwaddr(struct udevice *dev)
546{
547 struct eth_pdata *pdata = dev_get_plat(dev);
548 struct ftgmac100_data *priv = dev_get_priv(dev);
549
550 return ftgmac100_get_mac(priv, pdata->enetaddr);
551}
552
Simon Glassd1998a92020-12-03 16:55:21 -0700553static int ftgmac100_of_to_plat(struct udevice *dev)
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100554{
Simon Glassc69cda22020-12-03 16:55:20 -0700555 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100556 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800557
Masahiro Yamada25484932020-07-17 14:36:48 +0900558 pdata->iobase = dev_read_addr(dev);
Marek Behún123ca112022-04-07 00:33:01 +0200559
560 pdata->phy_interface = dev_read_phy_mode(dev);
Marek Behúnffb0f6f2022-04-07 00:33:03 +0200561 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100562 return -EINVAL;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100563
564 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
565
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100566 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
567 priv->rxdes0_edorr_mask = BIT(30);
568 priv->txdes0_edotr_mask = BIT(30);
569 } else {
570 priv->rxdes0_edorr_mask = BIT(15);
571 priv->txdes0_edotr_mask = BIT(15);
572 }
573
Dylan Hung607e7fa2023-07-27 09:58:14 +0800574 priv->reset_ctl = devm_reset_control_get_optional(dev, NULL);
575
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100576 return clk_get_bulk(dev, &priv->clks);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800577}
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100578
579static int ftgmac100_probe(struct udevice *dev)
580{
Simon Glassc69cda22020-12-03 16:55:20 -0700581 struct eth_pdata *pdata = dev_get_plat(dev);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100582 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100583 int ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100584
585 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100586 priv->phy_mode = pdata->phy_interface;
587 priv->max_speed = pdata->max_speed;
588 priv->phy_addr = 0;
589
Samuel Mendoza-Jonas3b400e82022-08-08 21:46:05 +0930590 if (dev_read_bool(dev, "use-ncsi"))
591 priv->phy_mode = PHY_INTERFACE_MODE_NCSI;
592
Thirupathaiah Annapureddy66e036b2020-08-17 17:08:26 -0700593#ifdef CONFIG_PHY_ADDR
594 priv->phy_addr = CONFIG_PHY_ADDR;
595#endif
596
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100597 ret = clk_enable_bulk(&priv->clks);
598 if (ret)
599 goto out;
600
Dylan Hung607e7fa2023-07-27 09:58:14 +0800601 if (priv->reset_ctl) {
602 ret = reset_deassert(priv->reset_ctl);
603 if (ret)
604 goto out;
605 }
606
Dylan Hung9c27ce72021-12-09 10:12:24 +0800607 /*
608 * If DM MDIO is enabled, the MDIO bus will be initialized later in
609 * dm_eth_phy_connect
610 */
Samuel Mendoza-Jonas3b400e82022-08-08 21:46:05 +0930611 if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI &&
612 !IS_ENABLED(CONFIG_DM_MDIO)) {
Dylan Hung9c27ce72021-12-09 10:12:24 +0800613 ret = ftgmac100_mdio_init(dev);
614 if (ret) {
615 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
616 goto out;
617 }
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100618 }
619
620 ret = ftgmac100_phy_init(dev);
621 if (ret) {
622 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
623 goto out;
624 }
625
Hongwei Zhang0be3d1f2020-12-10 18:11:09 -0500626 ftgmac_read_hwaddr(dev);
627
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100628out:
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100629 if (ret)
630 clk_release_bulk(&priv->clks);
631
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100632 return ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100633}
634
635static int ftgmac100_remove(struct udevice *dev)
636{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100637 struct ftgmac100_data *priv = dev_get_priv(dev);
638
639 free(priv->phydev);
640 mdio_unregister(priv->bus);
641 mdio_free(priv->bus);
Dylan Hung607e7fa2023-07-27 09:58:14 +0800642 if (priv->reset_ctl)
643 reset_assert(priv->reset_ctl);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100644 clk_release_bulk(&priv->clks);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100645
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100646 return 0;
647}
648
649static const struct eth_ops ftgmac100_ops = {
650 .start = ftgmac100_start,
651 .send = ftgmac100_send,
652 .recv = ftgmac100_recv,
653 .stop = ftgmac100_stop,
654 .free_pkt = ftgmac100_free_pkt,
655 .write_hwaddr = ftgmac100_write_hwaddr,
656};
657
658static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100659 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
660 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Dylan Hungac4fda72021-12-09 10:12:25 +0800661 { .compatible = "aspeed,ast2600-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100662 { }
663};
664
665U_BOOT_DRIVER(ftgmac100) = {
666 .name = "ftgmac100",
667 .id = UCLASS_ETH,
668 .of_match = ftgmac100_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700669 .of_to_plat = ftgmac100_of_to_plat,
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100670 .probe = ftgmac100_probe,
671 .remove = ftgmac100_remove,
672 .ops = &ftgmac100_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700673 .priv_auto = sizeof(struct ftgmac100_data),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700674 .plat_auto = sizeof(struct eth_pdata),
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100675 .flags = DM_FLAG_ALLOC_PRIV_DMA,
676};