blob: 798977616756b9f6d1d60a1bd7af51aa1838cd66 [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
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010014#include <clk.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010015#include <dm.h>
16#include <miiphy.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080017#include <net.h>
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010018#include <wait_bit.h>
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010019#include <linux/io.h>
Cédric Le Goater538e75d2018-10-29 07:06:33 +010020#include <linux/iopoll.h>
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080021
22#include "ftgmac100.h"
23
Cédric Le Goatere7668492018-10-29 07:06:34 +010024/* Min frame ethernet frame size without FCS */
25#define ETH_ZLEN 60
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080026
Cédric Le Goatere7668492018-10-29 07:06:34 +010027/* Receive Buffer Size Register - HW default is 0x640 */
28#define FTGMAC100_RBSR_DEFAULT 0x640
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080029
30/* PKTBUFSTX/PKTBUFSRX must both be power of 2 */
31#define PKTBUFSTX 4 /* must be power of 2 */
32
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +010033/* Timeout for transmit */
34#define FTGMAC100_TX_TIMEOUT_MS 1000
35
Cédric Le Goater538e75d2018-10-29 07:06:33 +010036/* Timeout for a mdio read/write operation */
37#define FTGMAC100_MDIO_TIMEOUT_USEC 10000
38
39/*
40 * MDC clock cycle threshold
41 *
42 * 20us * 100 = 2ms > (1 / 2.5Mhz) * 0x34
43 */
44#define MDC_CYCTHR 0x34
45
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010046/**
47 * struct ftgmac100_data - private data for the FTGMAC100 driver
48 *
49 * @iobase: The base address of the hardware registers
50 * @txdes: The array of transmit descriptors
51 * @rxdes: The array of receive descriptors
52 * @tx_index: Transmit descriptor index in @txdes
53 * @rx_index: Receive descriptor index in @rxdes
54 * @phy_addr: The PHY interface address to use
Cédric Le Goater538e75d2018-10-29 07:06:33 +010055 * @phydev: The PHY device backing the MAC
56 * @bus: The mdio bus
57 * @phy_mode: The mode of the PHY interface (rgmii, rmii, ...)
58 * @max_speed: Maximum speed of Ethernet connection supported by MAC
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010059 * @clks: The bulk of clocks assigned to the device in the DT
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010060 */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080061struct ftgmac100_data {
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010062 struct ftgmac100 *iobase;
63
Cédric Le Goatere7668492018-10-29 07:06:34 +010064 struct ftgmac100_txdes txdes[PKTBUFSTX];
65 struct ftgmac100_rxdes rxdes[PKTBUFSRX];
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080066 int tx_index;
67 int rx_index;
Cédric Le Goater538e75d2018-10-29 07:06:33 +010068
69 u32 phy_addr;
70 struct phy_device *phydev;
71 struct mii_dev *bus;
72 u32 phy_mode;
73 u32 max_speed;
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +010074
75 struct clk_bulk clks;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080076};
77
78/*
79 * struct mii_bus functions
80 */
Cédric Le Goater538e75d2018-10-29 07:06:33 +010081static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr,
82 int reg_addr)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080083{
Cédric Le Goater538e75d2018-10-29 07:06:33 +010084 struct ftgmac100_data *priv = bus->priv;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +010085 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080086 int phycr;
87 int data;
Cédric Le Goater538e75d2018-10-29 07:06:33 +010088 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080089
Cédric Le Goater538e75d2018-10-29 07:06:33 +010090 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
91 FTGMAC100_PHYCR_PHYAD(phy_addr) |
92 FTGMAC100_PHYCR_REGAD(reg_addr) |
93 FTGMAC100_PHYCR_MIIRD;
94 writel(phycr, &ftgmac100->phycr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +080095
Cédric Le Goater538e75d2018-10-29 07:06:33 +010096 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
97 !(phycr & FTGMAC100_PHYCR_MIIRD),
98 FTGMAC100_MDIO_TIMEOUT_USEC);
99 if (ret) {
100 pr_err("%s: mdio read failed (phy:%d reg:%x)\n",
101 priv->phydev->dev->name, phy_addr, reg_addr);
102 return ret;
103 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800104
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100105 data = readl(&ftgmac100->phydata);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800106
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100107 return FTGMAC100_PHYDATA_MIIRDATA(data);
108}
109
110static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr,
111 int reg_addr, u16 value)
112{
113 struct ftgmac100_data *priv = bus->priv;
114 struct ftgmac100 *ftgmac100 = priv->iobase;
115 int phycr;
116 int data;
117 int ret;
118
119 phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) |
120 FTGMAC100_PHYCR_PHYAD(phy_addr) |
121 FTGMAC100_PHYCR_REGAD(reg_addr) |
122 FTGMAC100_PHYCR_MIIWR;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800123 data = FTGMAC100_PHYDATA_MIIWDATA(value);
124
125 writel(data, &ftgmac100->phydata);
126 writel(phycr, &ftgmac100->phycr);
127
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100128 ret = readl_poll_timeout(&ftgmac100->phycr, phycr,
129 !(phycr & FTGMAC100_PHYCR_MIIWR),
130 FTGMAC100_MDIO_TIMEOUT_USEC);
131 if (ret) {
132 pr_err("%s: mdio write failed (phy:%d reg:%x)\n",
133 priv->phydev->dev->name, phy_addr, reg_addr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800134 }
135
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100136 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800137}
138
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100139static int ftgmac100_mdio_init(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800140{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100141 struct ftgmac100_data *priv = dev_get_priv(dev);
142 struct mii_dev *bus;
143 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800144
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100145 bus = mdio_alloc();
146 if (!bus)
147 return -ENOMEM;
148
149 bus->read = ftgmac100_mdio_read;
150 bus->write = ftgmac100_mdio_write;
151 bus->priv = priv;
152
153 ret = mdio_register_seq(bus, dev->seq);
154 if (ret) {
155 free(bus);
156 return ret;
157 }
158
159 priv->bus = bus;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800160
161 return 0;
162}
163
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100164static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800165{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100166 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100167 struct phy_device *phydev = priv->phydev;
168 u32 maccr;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800169
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100170 if (!phydev->link) {
171 dev_err(phydev->dev, "No link\n");
172 return -EREMOTEIO;
173 }
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800174
175 /* read MAC control register and clear related bits */
176 maccr = readl(&ftgmac100->maccr) &
177 ~(FTGMAC100_MACCR_GIGA_MODE |
178 FTGMAC100_MACCR_FAST_MODE |
179 FTGMAC100_MACCR_FULLDUP);
180
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100181 if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800182 maccr |= FTGMAC100_MACCR_GIGA_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800183
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100184 if (phydev->speed == 100)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800185 maccr |= FTGMAC100_MACCR_FAST_MODE;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800186
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100187 if (phydev->duplex)
188 maccr |= FTGMAC100_MACCR_FULLDUP;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800189
190 /* update MII config into maccr */
191 writel(maccr, &ftgmac100->maccr);
192
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100193 return 0;
194}
195
196static int ftgmac100_phy_init(struct udevice *dev)
197{
198 struct ftgmac100_data *priv = dev_get_priv(dev);
199 struct phy_device *phydev;
200 int ret;
201
202 phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode);
203 if (!phydev)
204 return -ENODEV;
205
206 phydev->supported &= PHY_GBIT_FEATURES;
207 if (priv->max_speed) {
208 ret = phy_set_supported(phydev, priv->max_speed);
209 if (ret)
210 return ret;
211 }
212 phydev->advertising = phydev->supported;
213 priv->phydev = phydev;
214 phy_config(phydev);
215
216 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800217}
218
219/*
220 * Reset MAC
221 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100222static void ftgmac100_reset(struct ftgmac100_data *priv)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800223{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100224 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800225
226 debug("%s()\n", __func__);
227
Cédric Le Goater591ffd92018-10-29 07:06:32 +0100228 setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800229
230 while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST)
231 ;
232}
233
234/*
235 * Set MAC address
236 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100237static int ftgmac100_set_mac(struct ftgmac100_data *priv,
238 const unsigned char *mac)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800239{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100240 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800241 unsigned int maddr = mac[0] << 8 | mac[1];
242 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
243
244 debug("%s(%x %x)\n", __func__, maddr, laddr);
245
246 writel(maddr, &ftgmac100->mac_madr);
247 writel(laddr, &ftgmac100->mac_ladr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800248
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100249 return 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800250}
251
252/*
253 * disable transmitter, receiver
254 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100255static void ftgmac100_stop(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800256{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100257 struct ftgmac100_data *priv = dev_get_priv(dev);
258 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800259
260 debug("%s()\n", __func__);
261
262 writel(0, &ftgmac100->maccr);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100263
264 phy_shutdown(priv->phydev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800265}
266
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100267static int ftgmac100_start(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800268{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100269 struct eth_pdata *plat = dev_get_platdata(dev);
270 struct ftgmac100_data *priv = dev_get_priv(dev);
271 struct ftgmac100 *ftgmac100 = priv->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100272 struct phy_device *phydev = priv->phydev;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800273 unsigned int maccr;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100274 ulong start, end;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100275 int ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800276 int i;
277
278 debug("%s()\n", __func__);
279
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100280 ftgmac100_reset(priv);
281
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800282 /* set the ethernet address */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100283 ftgmac100_set_mac(priv, plat->enetaddr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800284
285 /* disable all interrupts */
286 writel(0, &ftgmac100->ier);
287
288 /* initialize descriptors */
289 priv->tx_index = 0;
290 priv->rx_index = 0;
291
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800292 for (i = 0; i < PKTBUFSTX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100293 priv->txdes[i].txdes3 = 0;
294 priv->txdes[i].txdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800295 }
Cédric Le Goatere7668492018-10-29 07:06:34 +0100296 priv->txdes[PKTBUFSTX - 1].txdes0 = FTGMAC100_TXDES0_EDOTR;
297
298 start = (ulong)&priv->txdes[0];
299 end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN);
300 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800301
302 for (i = 0; i < PKTBUFSRX; i++) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100303 priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i];
304 priv->rxdes[i].rxdes0 = 0;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800305 }
Cédric Le Goatere7668492018-10-29 07:06:34 +0100306 priv->rxdes[PKTBUFSRX - 1].rxdes0 = FTGMAC100_RXDES0_EDORR;
307
308 start = (ulong)&priv->rxdes[0];
309 end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN);
310 flush_dcache_range(start, end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800311
312 /* transmit ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100313 writel((u32)priv->txdes, &ftgmac100->txr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800314
315 /* receive ring */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100316 writel((u32)priv->rxdes, &ftgmac100->rxr_badr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800317
318 /* poll receive descriptor automatically */
319 writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc);
320
321 /* config receive buffer size register */
Cédric Le Goatere7668492018-10-29 07:06:34 +0100322 writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800323
324 /* enable transmitter, receiver */
325 maccr = FTGMAC100_MACCR_TXMAC_EN |
326 FTGMAC100_MACCR_RXMAC_EN |
327 FTGMAC100_MACCR_TXDMA_EN |
328 FTGMAC100_MACCR_RXDMA_EN |
329 FTGMAC100_MACCR_CRC_APD |
330 FTGMAC100_MACCR_FULLDUP |
331 FTGMAC100_MACCR_RX_RUNT |
332 FTGMAC100_MACCR_RX_BROADPKT;
333
334 writel(maccr, &ftgmac100->maccr);
335
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100336 ret = phy_startup(phydev);
337 if (ret) {
338 dev_err(phydev->dev, "Could not start PHY\n");
339 return ret;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800340 }
341
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100342 ret = ftgmac100_phy_adjust_link(priv);
343 if (ret) {
344 dev_err(phydev->dev, "Could not adjust link\n");
345 return ret;
346 }
347
348 printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name,
349 phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr);
350
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800351 return 0;
352}
353
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100354static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length)
355{
356 struct ftgmac100_data *priv = dev_get_priv(dev);
357 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Cédric Le Goatere7668492018-10-29 07:06:34 +0100358 ulong des_start = (ulong)curr_des;
359 ulong des_end = des_start +
360 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100361
Cédric Le Goatere7668492018-10-29 07:06:34 +0100362 /* Release buffer to DMA and flush descriptor */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100363 curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100364 flush_dcache_range(des_start, des_end);
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100365
366 /* Move to next descriptor */
367 priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX;
368
369 return 0;
370}
371
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800372/*
373 * Get a data block via Ethernet
374 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100375static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800376{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100377 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goatere7668492018-10-29 07:06:34 +0100378 struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index];
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800379 unsigned short rxlen;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100380 ulong des_start = (ulong)curr_des;
381 ulong des_end = des_start +
382 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
383 ulong data_start = curr_des->rxdes3;
384 ulong data_end;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800385
Cédric Le Goatere7668492018-10-29 07:06:34 +0100386 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800387
388 if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY))
Cédric Le Goatere7668492018-10-29 07:06:34 +0100389 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800390
391 if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR |
392 FTGMAC100_RXDES0_CRC_ERR |
393 FTGMAC100_RXDES0_FTL |
394 FTGMAC100_RXDES0_RUNT |
395 FTGMAC100_RXDES0_RX_ODD_NB)) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100396 return -EAGAIN;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800397 }
398
399 rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0);
400
401 debug("%s(): RX buffer %d, %x received\n",
402 __func__, priv->rx_index, rxlen);
403
Cédric Le Goatere7668492018-10-29 07:06:34 +0100404 /* Invalidate received data */
405 data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN);
406 invalidate_dcache_range(data_start, data_end);
407 *packetp = (uchar *)data_start;
Kuo-Jung Sua8f9cd12013-05-07 14:33:51 +0800408
Cédric Le Goatere7668492018-10-29 07:06:34 +0100409 return rxlen;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800410}
411
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100412static u32 ftgmac100_read_txdesc(const void *desc)
413{
414 const struct ftgmac100_txdes *txdes = desc;
415 ulong des_start = (ulong)txdes;
416 ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN);
417
418 invalidate_dcache_range(des_start, des_end);
419
420 return txdes->txdes0;
421}
422
423BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc)
424
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800425/*
426 * Send a data block via Ethernet
427 */
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100428static int ftgmac100_send(struct udevice *dev, void *packet, int length)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800429{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100430 struct ftgmac100_data *priv = dev_get_priv(dev);
431 struct ftgmac100 *ftgmac100 = priv->iobase;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800432 struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index];
Cédric Le Goatere7668492018-10-29 07:06:34 +0100433 ulong des_start = (ulong)curr_des;
434 ulong des_end = des_start +
435 roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN);
436 ulong data_start;
437 ulong data_end;
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100438 int rc;
Cédric Le Goatere7668492018-10-29 07:06:34 +0100439
440 invalidate_dcache_range(des_start, des_end);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800441
442 if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) {
Cédric Le Goatere7668492018-10-29 07:06:34 +0100443 dev_err(dev, "no TX descriptor available\n");
444 return -EPERM;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800445 }
446
447 debug("%s(%x, %x)\n", __func__, (int)packet, length);
448
449 length = (length < ETH_ZLEN) ? ETH_ZLEN : length;
450
Cédric Le Goatere7668492018-10-29 07:06:34 +0100451 curr_des->txdes3 = (unsigned int)packet;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800452
Cédric Le Goatere7668492018-10-29 07:06:34 +0100453 /* Flush data to be sent */
454 data_start = curr_des->txdes3;
455 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
456 flush_dcache_range(data_start, data_end);
457
458 /* Only one segment on TXBUF */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800459 curr_des->txdes0 &= FTGMAC100_TXDES0_EDOTR;
460 curr_des->txdes0 |= FTGMAC100_TXDES0_FTS |
461 FTGMAC100_TXDES0_LTS |
462 FTGMAC100_TXDES0_TXBUF_SIZE(length) |
463 FTGMAC100_TXDES0_TXDMA_OWN ;
464
Cédric Le Goatere7668492018-10-29 07:06:34 +0100465 /* Flush modified buffer descriptor */
466 flush_dcache_range(des_start, des_end);
467
468 /* Start transmit */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800469 writel(1, &ftgmac100->txpd);
470
Cédric Le Goaterd0e0b842018-10-29 07:06:35 +0100471 rc = wait_for_bit_ftgmac100_txdone(curr_des,
472 FTGMAC100_TXDES0_TXDMA_OWN, false,
473 FTGMAC100_TX_TIMEOUT_MS, true);
474 if (rc)
475 return rc;
476
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800477 debug("%s(): packet sent\n", __func__);
478
Cédric Le Goatere7668492018-10-29 07:06:34 +0100479 /* Move to next descriptor */
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800480 priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX;
481
482 return 0;
483}
484
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100485static int ftgmac100_write_hwaddr(struct udevice *dev)
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800486{
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100487 struct eth_pdata *pdata = dev_get_platdata(dev);
488 struct ftgmac100_data *priv = dev_get_priv(dev);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800489
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100490 return ftgmac100_set_mac(priv, pdata->enetaddr);
491}
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800492
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100493static int ftgmac100_ofdata_to_platdata(struct udevice *dev)
494{
495 struct eth_pdata *pdata = dev_get_platdata(dev);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100496 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100497 const char *phy_mode;
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800498
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100499 pdata->iobase = devfdt_get_addr(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100500 pdata->phy_interface = -1;
501 phy_mode = dev_read_string(dev, "phy-mode");
502 if (phy_mode)
503 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
504 if (pdata->phy_interface == -1) {
505 dev_err(dev, "Invalid PHY interface '%s'\n", phy_mode);
506 return -EINVAL;
507 }
508
509 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
510
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100511 return clk_get_bulk(dev, &priv->clks);
Macpaul Linb3dbf4a52010-12-21 16:59:46 +0800512}
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100513
514static int ftgmac100_probe(struct udevice *dev)
515{
516 struct eth_pdata *pdata = dev_get_platdata(dev);
517 struct ftgmac100_data *priv = dev_get_priv(dev);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100518 int ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100519
520 priv->iobase = (struct ftgmac100 *)pdata->iobase;
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100521 priv->phy_mode = pdata->phy_interface;
522 priv->max_speed = pdata->max_speed;
523 priv->phy_addr = 0;
524
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100525 ret = clk_enable_bulk(&priv->clks);
526 if (ret)
527 goto out;
528
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100529 ret = ftgmac100_mdio_init(dev);
530 if (ret) {
531 dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
532 goto out;
533 }
534
535 ret = ftgmac100_phy_init(dev);
536 if (ret) {
537 dev_err(dev, "Failed to initialize PHY: %d\n", ret);
538 goto out;
539 }
540
541out:
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100542 if (ret)
543 clk_release_bulk(&priv->clks);
544
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100545 return ret;
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100546}
547
548static int ftgmac100_remove(struct udevice *dev)
549{
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100550 struct ftgmac100_data *priv = dev_get_priv(dev);
551
552 free(priv->phydev);
553 mdio_unregister(priv->bus);
554 mdio_free(priv->bus);
Cédric Le Goater1c0c61e2018-10-29 07:06:36 +0100555 clk_release_bulk(&priv->clks);
Cédric Le Goater538e75d2018-10-29 07:06:33 +0100556
Cédric Le Goaterf95de0b2018-10-29 07:06:31 +0100557 return 0;
558}
559
560static const struct eth_ops ftgmac100_ops = {
561 .start = ftgmac100_start,
562 .send = ftgmac100_send,
563 .recv = ftgmac100_recv,
564 .stop = ftgmac100_stop,
565 .free_pkt = ftgmac100_free_pkt,
566 .write_hwaddr = ftgmac100_write_hwaddr,
567};
568
569static const struct udevice_id ftgmac100_ids[] = {
570 { .compatible = "faraday,ftgmac100" },
571 { }
572};
573
574U_BOOT_DRIVER(ftgmac100) = {
575 .name = "ftgmac100",
576 .id = UCLASS_ETH,
577 .of_match = ftgmac100_ids,
578 .ofdata_to_platdata = ftgmac100_ofdata_to_platdata,
579 .probe = ftgmac100_probe,
580 .remove = ftgmac100_remove,
581 .ops = &ftgmac100_ops,
582 .priv_auto_alloc_size = sizeof(struct ftgmac100_data),
583 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
584 .flags = DM_FLAG_ALLOC_PRIV_DMA,
585};