blob: 3322dcf2aabf4f51b4756d785c11d6b575b8cf19 [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>
Simon Glass1eb69ae2019-11-14 12:57:39 -070016#include <cpu_func.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010017#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070019#include <malloc.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010020#include <miiphy.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080021#include <net.h>
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010022#include <wait_bit.h>
Simon Glass90526e92020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass336d4612020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010025#include <linux/io.h>
Cédric Le Goater538e75d2018-10-29 07:06:33 +010026#include <linux/iopoll.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080027
28#include "ftgmac100.h"
29
Cédric Le Goatere7668492018-10-29 07:06:34 +010030/* Min frame ethernet frame size without FCS */
31#define ETH_ZLEN 60
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080032
Cédric Le Goatere7668492018-10-29 07:06:34 +010033/* Receive Buffer Size Register - HW default is 0x640 */
34#define FTGMAC100_RBSR_DEFAULT 0x640
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080035
36/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
37#define PKTBUFSTX 4 /* must be power of 2 */
38
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010039/* Timeout for transmit */
40#define FTGMAC100_TX_TIMEOUT_MS 1000
41
Cédric Le Goater538e75d2018-10-29 07:06:33 +010042/* Timeout for a mdio read/write operation */
43#define FTGMAC100_MDIO_TIMEOUT_USEC 10000
44
45/*
46 * MDC clock cycle threshold
47 *
48 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
49 */
50#define MDC_CYCTHR 0x34
51
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010052/*
53 * ftgmac100 model variants
54 */
55enum ftgmac100_model {
56 FTGMAC100_MODEL_FARADAY,
57 FTGMAC100_MODEL_ASPEED,
58};
59
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010060/**
61 * struct ftgmac100_data - private data for the FTGMAC100 driver
62 *
63 * @iobase: The base address of the hardware registers
64 * @txdes: The array of transmit descriptors
65 * @rxdes: The array of receive descriptors
66 * @tx_index: Transmit descriptor index in @txdes
67 * @rx_index: Receive descriptor index in @rxdes
68 * @phy_addr: The PHY interface address to use
Cédric Le Goater538e75d2018-10-29 07:06:33 +010069 * @phydev: The PHY device backing the MAC
70 * @bus: The mdio bus
71 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
72 * @max_speed: Maximum speed of Ethernet connection supported by MAC
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010073 * @clks: The bulk of clocks assigned to the device in the DT
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010074 * @rxdes0_edorr_mask: The bit number identifying the end of the RX ring buffer
75 * @txdes0_edotr_mask: The bit number identifying the end of the TX ring buffer
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010076 */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080077struct ftgmac100_data {
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010078 struct ftgmac100 *iobase;
79
Cédric Le Goater08b3e902019-11-28 13:37:04 +010080 struct ftgmac100_txdes txdes[PKTBUFSTX] __aligned(ARCH_DMA_MINALIGN);
81 struct ftgmac100_rxdes rxdes[PKTBUFSRX] __aligned(ARCH_DMA_MINALIGN);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080082 int tx_index;
83 int rx_index;
Cédric Le Goater538e75d2018-10-29 07:06:33 +010084
85 u32 phy_addr;
86 struct phy_device *phydev;
87 struct mii_dev *bus;
88 u32 phy_mode;
89 u32 max_speed;
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010090
91 struct clk_bulk clks;
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +010092
93 /* End of RX/TX ring buffer bits. Depend on model */
94 u32 rxdes0_edorr_mask;
95 u32 txdes0_edotr_mask;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080096};
97
98/*
99 * struct mii_bus functions
100 */
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100101static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
102 int reg_addr)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800103{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100104 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100105 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800106 int phycr;
107 int data;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100108 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800109
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100110 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
111 FTGMAC100_PHYCR_PHYAD(phy_addr) |
112 FTGMAC100_PHYCR_REGAD(reg_addr) |
113 FTGMAC100_PHYCR_MIIRD;
114 writel(phycr, &ftgmac100->phycr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800115
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100116 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
117 !(phycr & FTGMAC100_PHYCR_MIIRD),
118 FTGMAC100_MDIO_TIMEOUT_USEC);
119 if (ret) {
120 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
121 priv->phydev->dev->name, phy_addr, reg_addr);
122 return ret;
123 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800124
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100125 data = readl(&ftgmac100->phydata);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800126
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100127 return FTGMAC100_PHYDATA_MIIRDATA(data);
128}
129
130static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
131 int reg_addr, u16 value)
132{
133 struct ftgmac100_data *priv = bus->priv;
134 struct ftgmac100 *ftgmac100 = priv->iobase;
135 int phycr;
136 int data;
137 int ret;
138
139 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
140 FTGMAC100_PHYCR_PHYAD(phy_addr) |
141 FTGMAC100_PHYCR_REGAD(reg_addr) |
142 FTGMAC100_PHYCR_MIIWR;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800143 data = FTGMAC100_PHYDATA_MIIWDATA(value);
144
145 writel(data, &ftgmac100->phydata);
146 writel(phycr, &ftgmac100->phycr);
147
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100148 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
149 !(phycr & FTGMAC100_PHYCR_MIIWR),
150 FTGMAC100_MDIO_TIMEOUT_USEC);
151 if (ret) {
152 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
153 priv->phydev->dev->name, phy_addr, reg_addr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800154 }
155
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100156 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800157}
158
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100159static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800160{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100161 struct ftgmac100_data *priv = dev_get_priv(dev);
162 struct mii_dev *bus;
163 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800164
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100165 bus = mdio_alloc();
166 if (!bus)
167 return -ENOMEM;
168
169 bus->read = ftgmac100_mdio_read;
170 bus->write = ftgmac100_mdio_write;
171 bus->priv = priv;
172
173 ret = mdio_register_seq(bus, dev->seq);
174 if (ret) {
175 free(bus);
176 return ret;
177 }
178
179 priv->bus = bus;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800180
181 return 0;
182}
183
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100184static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800185{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100186 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100187 struct phy_device *phydev = priv->phydev;
188 u32 maccr;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800189
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100190 if (!phydev->link) {
191 dev_err(phydev->dev, "No link\n");
192 return -EREMOTEIO;
193 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800194
195 /* read MAC control register and clear related bits */
196 maccr = readl(&ftgmac100->maccr) &
197 ~(FTGMAC100_MACCR_GIGA_MODE |
198 FTGMAC100_MACCR_FAST_MODE |
199 FTGMAC100_MACCR_FULLDUP);
200
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100201 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800202 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800203
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100204 if (phydev->speed == 100)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800205 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800206
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100207 if (phydev->duplex)
208 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800209
210 /* update MII config into maccr */
211 writel(maccr, &ftgmac100->maccr);
212
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100213 return 0;
214}
215
216static int ftgmac100_phy_init(struct udevice *dev)
217{
218 struct ftgmac100_data *priv = dev_get_priv(dev);
219 struct phy_device *phydev;
220 int ret;
221
222 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
223 if (!phydev)
224 return -ENODEV;
225
226 phydev->supported &= PHY_GBIT_FEATURES;
227 if (priv->max_speed) {
228 ret = phy_set_supported(phydev, priv->max_speed);
229 if (ret)
230 return ret;
231 }
232 phydev->advertising = phydev->supported;
233 priv->phydev = phydev;
234 phy_config(phydev);
235
236 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800237}
238
239/*
240 * Reset MAC
241 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100242static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800243{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100244 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800245
246 debug("%s()\n", __func__);
247
Cédric Le Goater591ffd92018-10-29 07:06:32 +0100248 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800249
250 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
251 ;
252}
253
254/*
255 * Set MAC address
256 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100257static int ftgmac100_set_mac(struct ftgmac100_data *priv,
258 const unsigned char *mac)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800259{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100260 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800261 unsigned int maddr = mac[0] << 8 | mac[1];
262 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
263
264 debug("%s(%x %x)\n", __func__, maddr, laddr);
265
266 writel(maddr, &ftgmac100->mac_madr);
267 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800268
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100269 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800270}
271
272/*
273 * disable transmitter, receiver
274 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100275static void ftgmac100_stop(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800276{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100277 struct ftgmac100_data *priv = dev_get_priv(dev);
278 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800279
280 debug("%s()\n", __func__);
281
282 writel(0, &ftgmac100->maccr);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100283
284 phy_shutdown(priv->phydev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800285}
286
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100287static int ftgmac100_start(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800288{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100289 struct eth_pdata *plat = dev_get_platdata(dev);
290 struct ftgmac100_data *priv = dev_get_priv(dev);
291 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100292 struct phy_device *phydev = priv->phydev;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800293 unsigned int maccr;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100294 ulong start, end;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100295 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800296 int i;
297
298 debug("%s()\n", __func__);
299
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100300 ftgmac100_reset(priv);
301
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800302 /* set the ethernet address */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100303 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800304
305 /* disable all interrupts */
306 writel(0, &ftgmac100->ier);
307
308 /* initialize descriptors */
309 priv->tx_index = 0;
310 priv->rx_index = 0;
311
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800312 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100313 priv->txdes[i].txdes3 = 0;
314 priv->txdes[i].txdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800315 }
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100316 priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100317
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100318 start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100319 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
320 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800321
322 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100323 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
324 priv->rxdes[i].rxdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800325 }
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100326 priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100327
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100328 start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100329 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
330 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800331
332 /* transmit ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100333 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800334
335 /* receive ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100336 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800337
338 /* poll receive descriptor automatically */
339 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
340
341 /* config receive buffer size register */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100342 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800343
344 /* enable transmitter, receiver */
345 maccr = FTGMAC100_MACCR_TXMAC_EN |
346 FTGMAC100_MACCR_RXMAC_EN |
347 FTGMAC100_MACCR_TXDMA_EN |
348 FTGMAC100_MACCR_RXDMA_EN |
349 FTGMAC100_MACCR_CRC_APD |
350 FTGMAC100_MACCR_FULLDUP |
351 FTGMAC100_MACCR_RX_RUNT |
352 FTGMAC100_MACCR_RX_BROADPKT;
353
354 writel(maccr, &ftgmac100->maccr);
355
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100356 ret = phy_startup(phydev);
357 if (ret) {
358 dev_err(phydev->dev, "Could not start PHY\n");
359 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800360 }
361
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100362 ret = ftgmac100_phy_adjust_link(priv);
363 if (ret) {
364 dev_err(phydev->dev, "Could not adjust link\n");
365 return ret;
366 }
367
368 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
369 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
370
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800371 return 0;
372}
373
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100374static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
375{
376 struct ftgmac100_data *priv = dev_get_priv(dev);
377 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100378 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100379 ulong des_end = des_start +
380 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100381
Cédric Le Goatere7668492018-10-29 07:06:34 +0100382 /* Release buffer to DMA and flush descriptor */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100383 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100384 flush_dcache_range(des_start, des_end);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100385
386 /* Move to next descriptor */
387 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
388
389 return 0;
390}
391
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800392/*
393 * Get a data block via Ethernet
394 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100395static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800396{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100397 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100398 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800399 unsigned short rxlen;
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100400 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100401 ulong des_end = des_start +
402 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
403 ulong data_start = curr_des->rxdes3;
404 ulong data_end;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800405
Cédric Le Goatere7668492018-10-29 07:06:34 +0100406 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800407
408 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goatere7668492018-10-29 07:06:34 +0100409 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800410
411 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
412 FTGMAC100_RXDES0_CRC_ERR |
413 FTGMAC100_RXDES0_FTL |
414 FTGMAC100_RXDES0_RUNT |
415 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100416 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800417 }
418
419 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
420
421 debug("%s(): RX buffer %d, %x received\n",
422 __func__, priv->rx_index, rxlen);
423
Cédric Le Goatere7668492018-10-29 07:06:34 +0100424 /* Invalidate received data */
425 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
426 invalidate_dcache_range(data_start, data_end);
427 *packetp = (uchar *)data_start;
Kuo-Jung Sua8f9cd12013-05-07 14:33:51 +0800428
Cédric Le Goatere7668492018-10-29 07:06:34 +0100429 return rxlen;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800430}
431
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100432static u32 ftgmac100_read_txdesc(const void *desc)
433{
434 const struct ftgmac100_txdes *txdes = desc;
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100435 ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100436 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
437
438 invalidate_dcache_range(des_start, des_end);
439
440 return txdes->txdes0;
441}
442
443BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
444
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800445/*
446 * Send a data block via Ethernet
447 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100448static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800449{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100450 struct ftgmac100_data *priv = dev_get_priv(dev);
451 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800452 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goater08b3e902019-11-28 13:37:04 +0100453 ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100454 ulong des_end = des_start +
455 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
456 ulong data_start;
457 ulong data_end;
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100458 int rc;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100459
460 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800461
462 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100463 dev_err(dev, "no TX descriptor available\n");
464 return -EPERM;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800465 }
466
467 debug("%s(%x, %x)\n", __func__, (int)packet, length);
468
469 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
470
Cédric Le Goatere7668492018-10-29 07:06:34 +0100471 curr_des->txdes3 = (unsigned int)packet;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800472
Cédric Le Goatere7668492018-10-29 07:06:34 +0100473 /* Flush data to be sent */
474 data_start = curr_des->txdes3;
475 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
476 flush_dcache_range(data_start, data_end);
477
478 /* Only one segment on TXBUF */
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100479 curr_des->txdes0 &= priv->txdes0_edotr_mask;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800480 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
481 FTGMAC100_TXDES0_LTS |
482 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
483 FTGMAC100_TXDES0_TXDMA_OWN ;
484
Cédric Le Goatere7668492018-10-29 07:06:34 +0100485 /* Flush modified buffer descriptor */
486 flush_dcache_range(des_start, des_end);
487
488 /* Start transmit */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800489 writel(1, &ftgmac100->txpd);
490
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100491 rc = wait_for_bit_ftgmac100_txdone(curr_des,
492 FTGMAC100_TXDES0_TXDMA_OWN, false,
493 FTGMAC100_TX_TIMEOUT_MS, true);
494 if (rc)
495 return rc;
496
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800497 debug("%s(): packet sent\n", __func__);
498
Cédric Le Goatere7668492018-10-29 07:06:34 +0100499 /* Move to next descriptor */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800500 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
501
502 return 0;
503}
504
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100505static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800506{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100507 struct eth_pdata *pdata = dev_get_platdata(dev);
508 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800509
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100510 return ftgmac100_set_mac(priv, pdata->enetaddr);
511}
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800512
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100513static int ftgmac100_ofdata_to_platdata(struct udevice *dev)
514{
515 struct eth_pdata *pdata = dev_get_platdata(dev);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100516 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100517 const char *phy_mode;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800518
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100519 pdata->iobase = devfdt_get_addr(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100520 pdata->phy_interface = -1;
521 phy_mode = dev_read_string(dev, "phy-mode");
522 if (phy_mode)
523 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
524 if (pdata->phy_interface == -1) {
525 dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
526 return -EINVAL;
527 }
528
529 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
530
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100531 if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) {
532 priv->rxdes0_edorr_mask = BIT(30);
533 priv->txdes0_edotr_mask = BIT(30);
534 } else {
535 priv->rxdes0_edorr_mask = BIT(15);
536 priv->txdes0_edotr_mask = BIT(15);
537 }
538
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100539 return clk_get_bulk(dev, &priv->clks);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800540}
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100541
542static int ftgmac100_probe(struct udevice *dev)
543{
544 struct eth_pdata *pdata = dev_get_platdata(dev);
545 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100546 int ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100547
548 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100549 priv->phy_mode = pdata->phy_interface;
550 priv->max_speed = pdata->max_speed;
551 priv->phy_addr = 0;
552
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100553 ret = clk_enable_bulk(&priv->clks);
554 if (ret)
555 goto out;
556
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100557 ret = ftgmac100_mdio_init(dev);
558 if (ret) {
559 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
560 goto out;
561 }
562
563 ret = ftgmac100_phy_init(dev);
564 if (ret) {
565 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
566 goto out;
567 }
568
569out:
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100570 if (ret)
571 clk_release_bulk(&priv->clks);
572
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100573 return ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100574}
575
576static int ftgmac100_remove(struct udevice *dev)
577{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100578 struct ftgmac100_data *priv = dev_get_priv(dev);
579
580 free(priv->phydev);
581 mdio_unregister(priv->bus);
582 mdio_free(priv->bus);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100583 clk_release_bulk(&priv->clks);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100584
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100585 return 0;
586}
587
588static const struct eth_ops ftgmac100_ops = {
589 .start = ftgmac100_start,
590 .send = ftgmac100_send,
591 .recv = ftgmac100_recv,
592 .stop = ftgmac100_stop,
593 .free_pkt = ftgmac100_free_pkt,
594 .write_hwaddr = ftgmac100_write_hwaddr,
595};
596
597static const struct udevice_id ftgmac100_ids[] = {
Cédric Le Goatere6ddacc2018-10-29 07:06:38 +0100598 { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY },
599 { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED },
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100600 { }
601};
602
603U_BOOT_DRIVER(ftgmac100) = {
604 .name = "ftgmac100",
605 .id = UCLASS_ETH,
606 .of_match = ftgmac100_ids,
607 .ofdata_to_platdata = ftgmac100_ofdata_to_platdata,
608 .probe = ftgmac100_probe,
609 .remove = ftgmac100_remove,
610 .ops = &ftgmac100_ops,
611 .priv_auto_alloc_size = sizeof(struct ftgmac100_data),
612 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
613 .flags = DM_FLAG_ALLOC_PRIV_DMA,
614};