Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 2 | /* |
| 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 Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2018, IBM Corporation. |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 12 | */ |
| 13 | |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 14 | #include <clk.h> |
Dylan Hung | 1a2ef08 | 2023-07-27 09:58:14 +0800 | [diff] [blame] | 15 | #include <reset.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 16 | #include <cpu_func.h> |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 17 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 18 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 19 | #include <malloc.h> |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 20 | #include <miiphy.h> |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 21 | #include <net.h> |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 22 | #include <wait_bit.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 23 | #include <asm/cache.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 24 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 26 | #include <linux/io.h> |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 27 | #include <linux/iopoll.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 28 | #include <linux/printk.h> |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 29 | |
| 30 | #include "ftgmac100.h" |
| 31 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 32 | /* Min frame ethernet frame size without FCS */ |
| 33 | #define ETH_ZLEN 60 |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 34 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 35 | /* Receive Buffer Size Register - HW default is 0x640 */ |
| 36 | #define FTGMAC100_RBSR_DEFAULT 0x640 |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 37 | |
| 38 | /* PKTBUFSTX/PKTBUFSRX must both be power of 2 */ |
| 39 | #define PKTBUFSTX 4 /* must be power of 2 */ |
| 40 | |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 41 | /* Timeout for transmit */ |
| 42 | #define FTGMAC100_TX_TIMEOUT_MS 1000 |
| 43 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 44 | /* 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 Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 54 | /* |
| 55 | * ftgmac100 model variants |
| 56 | */ |
| 57 | enum ftgmac100_model { |
| 58 | FTGMAC100_MODEL_FARADAY, |
| 59 | FTGMAC100_MODEL_ASPEED, |
| 60 | }; |
| 61 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 62 | /** |
| 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 Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 71 | * @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 Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 75 | * @clks: The bulk of clocks assigned to the device in the DT |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 76 | * @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 Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 78 | */ |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 79 | struct ftgmac100_data { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 80 | struct ftgmac100 *iobase; |
| 81 | |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 82 | struct ftgmac100_txdes txdes[PKTBUFSTX] __aligned(ARCH_DMA_MINALIGN); |
| 83 | struct ftgmac100_rxdes rxdes[PKTBUFSRX] __aligned(ARCH_DMA_MINALIGN); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 84 | int tx_index; |
| 85 | int rx_index; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 86 | |
| 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 Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 92 | |
| 93 | struct clk_bulk clks; |
Dylan Hung | 1a2ef08 | 2023-07-27 09:58:14 +0800 | [diff] [blame] | 94 | struct reset_ctl *reset_ctl; |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 95 | |
| 96 | /* End of RX/TX ring buffer bits. Depend on model */ |
| 97 | u32 rxdes0_edorr_mask; |
| 98 | u32 txdes0_edotr_mask; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* |
| 102 | * struct mii_bus functions |
| 103 | */ |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 104 | static int ftgmac100_mdio_read(struct mii_dev *bus, int phy_addr, int dev_addr, |
| 105 | int reg_addr) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 106 | { |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 107 | struct ftgmac100_data *priv = bus->priv; |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 108 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 109 | int phycr; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 110 | int data; |
| 111 | int ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 112 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 113 | phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) | |
| 114 | FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 115 | FTGMAC100_PHYCR_REGAD(reg_addr) | |
| 116 | FTGMAC100_PHYCR_MIIRD; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 117 | writel(phycr, &ftgmac100->phycr); |
| 118 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 119 | ret = readl_poll_timeout(&ftgmac100->phycr, phycr, |
| 120 | !(phycr & FTGMAC100_PHYCR_MIIRD), |
| 121 | FTGMAC100_MDIO_TIMEOUT_USEC); |
| 122 | if (ret) { |
| 123 | pr_err("%s: mdio read failed (phy:%d reg:%x)\n", |
Zev Weiss | 4e5ca0f | 2022-05-17 15:16:39 -0700 | [diff] [blame] | 124 | bus->name, phy_addr, reg_addr); |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 125 | return ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 126 | } |
| 127 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 128 | data = readl(&ftgmac100->phydata); |
| 129 | |
| 130 | return FTGMAC100_PHYDATA_MIIRDATA(data); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 131 | } |
| 132 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 133 | static int ftgmac100_mdio_write(struct mii_dev *bus, int phy_addr, int dev_addr, |
| 134 | int reg_addr, u16 value) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 135 | { |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 136 | struct ftgmac100_data *priv = bus->priv; |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 137 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 138 | int phycr; |
| 139 | int data; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 140 | int ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 141 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 142 | phycr = FTGMAC100_PHYCR_MDC_CYCTHR(MDC_CYCTHR) | |
| 143 | FTGMAC100_PHYCR_PHYAD(phy_addr) | |
| 144 | FTGMAC100_PHYCR_REGAD(reg_addr) | |
| 145 | FTGMAC100_PHYCR_MIIWR; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 146 | data = FTGMAC100_PHYDATA_MIIWDATA(value); |
| 147 | |
| 148 | writel(data, &ftgmac100->phydata); |
| 149 | writel(phycr, &ftgmac100->phycr); |
| 150 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 151 | ret = readl_poll_timeout(&ftgmac100->phycr, phycr, |
| 152 | !(phycr & FTGMAC100_PHYCR_MIIWR), |
| 153 | FTGMAC100_MDIO_TIMEOUT_USEC); |
| 154 | if (ret) { |
| 155 | pr_err("%s: mdio write failed (phy:%d reg:%x)\n", |
Zev Weiss | 4e5ca0f | 2022-05-17 15:16:39 -0700 | [diff] [blame] | 156 | bus->name, phy_addr, reg_addr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 159 | return ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 162 | static int ftgmac100_mdio_init(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 163 | { |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 164 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 165 | struct mii_dev *bus; |
| 166 | int ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 167 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 168 | bus = mdio_alloc(); |
| 169 | if (!bus) |
| 170 | return -ENOMEM; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 171 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 172 | bus->read = ftgmac100_mdio_read; |
| 173 | bus->write = ftgmac100_mdio_write; |
| 174 | bus->priv = priv; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 175 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 176 | ret = mdio_register_seq(bus, dev_seq(dev)); |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 177 | if (ret) { |
| 178 | free(bus); |
| 179 | return ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 180 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 181 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 182 | priv->bus = bus; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 183 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 184 | return 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 187 | static int ftgmac100_phy_adjust_link(struct ftgmac100_data *priv) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 188 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 189 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 190 | struct phy_device *phydev = priv->phydev; |
| 191 | u32 maccr; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 192 | |
Samuel Mendoza-Jonas | cf4c4f9 | 2022-08-08 21:46:05 +0930 | [diff] [blame] | 193 | if (!phydev->link && priv->phy_mode != PHY_INTERFACE_MODE_NCSI) { |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 194 | dev_err(phydev->dev, "No link\n"); |
| 195 | return -EREMOTEIO; |
| 196 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 197 | |
| 198 | /* read MAC control register and clear related bits */ |
| 199 | maccr = readl(&ftgmac100->maccr) & |
| 200 | ~(FTGMAC100_MACCR_GIGA_MODE | |
| 201 | FTGMAC100_MACCR_FAST_MODE | |
| 202 | FTGMAC100_MACCR_FULLDUP); |
| 203 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 204 | if (phy_interface_is_rgmii(phydev) && phydev->speed == 1000) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 205 | maccr |= FTGMAC100_MACCR_GIGA_MODE; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 206 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 207 | if (phydev->speed == 100) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 208 | maccr |= FTGMAC100_MACCR_FAST_MODE; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 209 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 210 | if (phydev->duplex) |
| 211 | maccr |= FTGMAC100_MACCR_FULLDUP; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 212 | |
| 213 | /* update MII config into maccr */ |
| 214 | writel(maccr, &ftgmac100->maccr); |
| 215 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int ftgmac100_phy_init(struct udevice *dev) |
| 220 | { |
| 221 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 222 | struct phy_device *phydev; |
| 223 | int ret; |
| 224 | |
Jacky Chou | 7c01153 | 2024-06-28 15:14:45 +0800 | [diff] [blame] | 225 | if (IS_ENABLED(CONFIG_DM_MDIO) && priv->phy_mode != PHY_INTERFACE_MODE_NCSI) |
Dylan Hung | 2bec372 | 2021-12-09 10:12:24 +0800 | [diff] [blame] | 226 | phydev = dm_eth_phy_connect(dev); |
| 227 | else |
| 228 | phydev = phy_connect(priv->bus, priv->phy_addr, dev, priv->phy_mode); |
| 229 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 230 | if (!phydev) |
| 231 | return -ENODEV; |
| 232 | |
Samuel Mendoza-Jonas | cf4c4f9 | 2022-08-08 21:46:05 +0930 | [diff] [blame] | 233 | if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI) |
| 234 | phydev->supported &= PHY_GBIT_FEATURES; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 235 | if (priv->max_speed) { |
| 236 | ret = phy_set_supported(phydev, priv->max_speed); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | } |
| 240 | phydev->advertising = phydev->supported; |
| 241 | priv->phydev = phydev; |
| 242 | phy_config(phydev); |
| 243 | |
| 244 | return 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Reset MAC |
| 249 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 250 | static void ftgmac100_reset(struct ftgmac100_data *priv) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 251 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 252 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 253 | |
| 254 | debug("%s()\n", __func__); |
| 255 | |
Cédric Le Goater | cef951c | 2018-10-29 07:06:32 +0100 | [diff] [blame] | 256 | setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 257 | |
| 258 | while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST) |
| 259 | ; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Set MAC address |
| 264 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 265 | static int ftgmac100_set_mac(struct ftgmac100_data *priv, |
| 266 | const unsigned char *mac) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 267 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 268 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 269 | unsigned int maddr = mac[0] << 8 | mac[1]; |
| 270 | unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]; |
| 271 | |
| 272 | debug("%s(%x %x)\n", __func__, maddr, laddr); |
| 273 | |
| 274 | writel(maddr, &ftgmac100->mac_madr); |
| 275 | writel(laddr, &ftgmac100->mac_ladr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 276 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 277 | return 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
Hongwei Zhang | 55ae590 | 2020-12-10 18:11:09 -0500 | [diff] [blame] | 281 | * Get MAC address |
| 282 | */ |
| 283 | static int ftgmac100_get_mac(struct ftgmac100_data *priv, |
| 284 | unsigned char *mac) |
| 285 | { |
| 286 | struct ftgmac100 *ftgmac100 = priv->iobase; |
| 287 | unsigned int maddr = readl(&ftgmac100->mac_madr); |
| 288 | unsigned int laddr = readl(&ftgmac100->mac_ladr); |
| 289 | |
| 290 | debug("%s(%x %x)\n", __func__, maddr, laddr); |
| 291 | |
| 292 | mac[0] = (maddr >> 8) & 0xff; |
| 293 | mac[1] = maddr & 0xff; |
| 294 | mac[2] = (laddr >> 24) & 0xff; |
| 295 | mac[3] = (laddr >> 16) & 0xff; |
| 296 | mac[4] = (laddr >> 8) & 0xff; |
| 297 | mac[5] = laddr & 0xff; |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 303 | * disable transmitter, receiver |
| 304 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 305 | static void ftgmac100_stop(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 306 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 307 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 308 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 309 | |
| 310 | debug("%s()\n", __func__); |
| 311 | |
| 312 | writel(0, &ftgmac100->maccr); |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 313 | |
Samuel Mendoza-Jonas | cf4c4f9 | 2022-08-08 21:46:05 +0930 | [diff] [blame] | 314 | if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI) |
| 315 | phy_shutdown(priv->phydev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 316 | } |
| 317 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 318 | static int ftgmac100_start(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 319 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 320 | struct eth_pdata *plat = dev_get_plat(dev); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 321 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 322 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 323 | struct phy_device *phydev = priv->phydev; |
Jacky Chou | a3e259b | 2024-06-28 17:38:50 +0800 | [diff] [blame^] | 324 | unsigned int maccr, dblac, desc_size; |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 325 | ulong start, end; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 326 | int ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 327 | int i; |
| 328 | |
| 329 | debug("%s()\n", __func__); |
| 330 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 331 | ftgmac100_reset(priv); |
| 332 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 333 | /* set the ethernet address */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 334 | ftgmac100_set_mac(priv, plat->enetaddr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 335 | |
| 336 | /* disable all interrupts */ |
| 337 | writel(0, &ftgmac100->ier); |
| 338 | |
| 339 | /* initialize descriptors */ |
| 340 | priv->tx_index = 0; |
| 341 | priv->rx_index = 0; |
| 342 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 343 | for (i = 0; i < PKTBUFSTX; i++) { |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 344 | priv->txdes[i].txdes3 = 0; |
| 345 | priv->txdes[i].txdes0 = 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 346 | } |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 347 | priv->txdes[PKTBUFSTX - 1].txdes0 = priv->txdes0_edotr_mask; |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 348 | |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 349 | start = ((ulong)&priv->txdes[0]) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 350 | end = start + roundup(sizeof(priv->txdes), ARCH_DMA_MINALIGN); |
| 351 | flush_dcache_range(start, end); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 352 | |
| 353 | for (i = 0; i < PKTBUFSRX; i++) { |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 354 | priv->rxdes[i].rxdes3 = (unsigned int)net_rx_packets[i]; |
| 355 | priv->rxdes[i].rxdes0 = 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 356 | } |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 357 | priv->rxdes[PKTBUFSRX - 1].rxdes0 = priv->rxdes0_edorr_mask; |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 358 | |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 359 | start = ((ulong)&priv->rxdes[0]) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 360 | end = start + roundup(sizeof(priv->rxdes), ARCH_DMA_MINALIGN); |
| 361 | flush_dcache_range(start, end); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 362 | |
| 363 | /* transmit ring */ |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 364 | writel((u32)priv->txdes, &ftgmac100->txr_badr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 365 | |
| 366 | /* receive ring */ |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 367 | writel((u32)priv->rxdes, &ftgmac100->rxr_badr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 368 | |
Jacky Chou | a3e259b | 2024-06-28 17:38:50 +0800 | [diff] [blame^] | 369 | /* Configure TX/RX decsriptor size |
| 370 | * This size is calculated based on cache line. |
| 371 | */ |
| 372 | desc_size = ARCH_DMA_MINALIGN / FTGMAC100_DESC_UNIT; |
| 373 | /* The descriptor size is at least 2 descriptor units. */ |
| 374 | if (desc_size < 2) |
| 375 | desc_size = 2; |
| 376 | dblac = readl(&ftgmac100->dblac) & ~GENMASK(19, 12); |
| 377 | dblac |= FTGMAC100_DBLAC_RXDES_SIZE(desc_size) | FTGMAC100_DBLAC_TXDES_SIZE(desc_size); |
| 378 | writel(dblac, &ftgmac100->dblac); |
| 379 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 380 | /* poll receive descriptor automatically */ |
| 381 | writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc); |
| 382 | |
| 383 | /* config receive buffer size register */ |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 384 | writel(FTGMAC100_RBSR_SIZE(FTGMAC100_RBSR_DEFAULT), &ftgmac100->rbsr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 385 | |
| 386 | /* enable transmitter, receiver */ |
| 387 | maccr = FTGMAC100_MACCR_TXMAC_EN | |
| 388 | FTGMAC100_MACCR_RXMAC_EN | |
| 389 | FTGMAC100_MACCR_TXDMA_EN | |
| 390 | FTGMAC100_MACCR_RXDMA_EN | |
| 391 | FTGMAC100_MACCR_CRC_APD | |
| 392 | FTGMAC100_MACCR_FULLDUP | |
| 393 | FTGMAC100_MACCR_RX_RUNT | |
| 394 | FTGMAC100_MACCR_RX_BROADPKT; |
| 395 | |
| 396 | writel(maccr, &ftgmac100->maccr); |
| 397 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 398 | ret = phy_startup(phydev); |
| 399 | if (ret) { |
| 400 | dev_err(phydev->dev, "Could not start PHY\n"); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | ret = ftgmac100_phy_adjust_link(priv); |
| 405 | if (ret) { |
| 406 | dev_err(phydev->dev, "Could not adjust link\n"); |
| 407 | return ret; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 408 | } |
| 409 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 410 | printf("%s: link up, %d Mbps %s-duplex mac:%pM\n", phydev->dev->name, |
| 411 | phydev->speed, phydev->duplex ? "full" : "half", plat->enetaddr); |
| 412 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 416 | static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 417 | { |
| 418 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 419 | struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index]; |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 420 | ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 421 | ulong des_end = des_start + |
| 422 | roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 423 | |
Jacky Chou | 69d5707 | 2024-06-27 14:26:00 +0800 | [diff] [blame] | 424 | /* |
| 425 | * Make sure there are no stale data in write-back over this area, which |
| 426 | * might get written into the memory while the ftgmac100 also writes |
| 427 | * into the same memory area. |
| 428 | */ |
| 429 | flush_dcache_range((ulong)net_rx_packets[priv->rx_index], |
| 430 | (ulong)net_rx_packets[priv->rx_index] + PKTSIZE_ALIGN); |
| 431 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 432 | /* Release buffer to DMA and flush descriptor */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 433 | curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY; |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 434 | flush_dcache_range(des_start, des_end); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 435 | |
| 436 | /* Move to next descriptor */ |
| 437 | priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX; |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 442 | /* |
| 443 | * Get a data block via Ethernet |
| 444 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 445 | static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 446 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 447 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 448 | struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index]; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 449 | unsigned short rxlen; |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 450 | ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 451 | ulong des_end = des_start + |
| 452 | roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN); |
| 453 | ulong data_start = curr_des->rxdes3; |
| 454 | ulong data_end; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 455 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 456 | invalidate_dcache_range(des_start, des_end); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 457 | |
| 458 | if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY)) |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 459 | return -EAGAIN; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 460 | |
| 461 | if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR | |
| 462 | FTGMAC100_RXDES0_CRC_ERR | |
| 463 | FTGMAC100_RXDES0_FTL | |
| 464 | FTGMAC100_RXDES0_RUNT | |
| 465 | FTGMAC100_RXDES0_RX_ODD_NB)) { |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 466 | return -EAGAIN; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0); |
| 470 | |
| 471 | debug("%s(): RX buffer %d, %x received\n", |
| 472 | __func__, priv->rx_index, rxlen); |
| 473 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 474 | /* Invalidate received data */ |
| 475 | data_end = data_start + roundup(rxlen, ARCH_DMA_MINALIGN); |
| 476 | invalidate_dcache_range(data_start, data_end); |
| 477 | *packetp = (uchar *)data_start; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 478 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 479 | return rxlen; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 480 | } |
| 481 | |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 482 | static u32 ftgmac100_read_txdesc(const void *desc) |
| 483 | { |
| 484 | const struct ftgmac100_txdes *txdes = desc; |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 485 | ulong des_start = ((ulong)txdes) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 486 | ulong des_end = des_start + roundup(sizeof(*txdes), ARCH_DMA_MINALIGN); |
| 487 | |
| 488 | invalidate_dcache_range(des_start, des_end); |
| 489 | |
| 490 | return txdes->txdes0; |
| 491 | } |
| 492 | |
| 493 | BUILD_WAIT_FOR_BIT(ftgmac100_txdone, u32, ftgmac100_read_txdesc) |
| 494 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 495 | /* |
| 496 | * Send a data block via Ethernet |
| 497 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 498 | static int ftgmac100_send(struct udevice *dev, void *packet, int length) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 499 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 500 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 501 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 502 | struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index]; |
Cédric Le Goater | 0404e9f | 2019-11-28 13:37:04 +0100 | [diff] [blame] | 503 | ulong des_start = ((ulong)curr_des) & ~(ARCH_DMA_MINALIGN - 1); |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 504 | ulong des_end = des_start + |
| 505 | roundup(sizeof(*curr_des), ARCH_DMA_MINALIGN); |
| 506 | ulong data_start; |
| 507 | ulong data_end; |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 508 | int rc; |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 509 | |
| 510 | invalidate_dcache_range(des_start, des_end); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 511 | |
| 512 | if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) { |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 513 | dev_err(dev, "no TX descriptor available\n"); |
| 514 | return -EPERM; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | debug("%s(%x, %x)\n", __func__, (int)packet, length); |
| 518 | |
| 519 | length = (length < ETH_ZLEN) ? ETH_ZLEN : length; |
| 520 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 521 | curr_des->txdes3 = (unsigned int)packet; |
| 522 | |
| 523 | /* Flush data to be sent */ |
| 524 | data_start = curr_des->txdes3; |
| 525 | data_end = data_start + roundup(length, ARCH_DMA_MINALIGN); |
| 526 | flush_dcache_range(data_start, data_end); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 527 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 528 | /* Only one segment on TXBUF */ |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 529 | curr_des->txdes0 &= priv->txdes0_edotr_mask; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 530 | curr_des->txdes0 |= FTGMAC100_TXDES0_FTS | |
| 531 | FTGMAC100_TXDES0_LTS | |
| 532 | FTGMAC100_TXDES0_TXBUF_SIZE(length) | |
| 533 | FTGMAC100_TXDES0_TXDMA_OWN ; |
| 534 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 535 | /* Flush modified buffer descriptor */ |
| 536 | flush_dcache_range(des_start, des_end); |
| 537 | |
| 538 | /* Start transmit */ |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 539 | writel(1, &ftgmac100->txpd); |
| 540 | |
Cédric Le Goater | 9bcb665 | 2018-10-29 07:06:35 +0100 | [diff] [blame] | 541 | rc = wait_for_bit_ftgmac100_txdone(curr_des, |
| 542 | FTGMAC100_TXDES0_TXDMA_OWN, false, |
| 543 | FTGMAC100_TX_TIMEOUT_MS, true); |
| 544 | if (rc) |
| 545 | return rc; |
| 546 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 547 | debug("%s(): packet sent\n", __func__); |
| 548 | |
Cédric Le Goater | 3174dfb | 2018-10-29 07:06:34 +0100 | [diff] [blame] | 549 | /* Move to next descriptor */ |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 550 | priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX; |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 555 | static int ftgmac100_write_hwaddr(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 556 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 557 | struct eth_pdata *pdata = dev_get_plat(dev); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 558 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 559 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 560 | return ftgmac100_set_mac(priv, pdata->enetaddr); |
| 561 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 562 | |
Hongwei Zhang | 55ae590 | 2020-12-10 18:11:09 -0500 | [diff] [blame] | 563 | static int ftgmac_read_hwaddr(struct udevice *dev) |
| 564 | { |
| 565 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 566 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 567 | |
| 568 | return ftgmac100_get_mac(priv, pdata->enetaddr); |
| 569 | } |
| 570 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 571 | static int ftgmac100_of_to_plat(struct udevice *dev) |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 572 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 573 | struct eth_pdata *pdata = dev_get_plat(dev); |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 574 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 575 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 576 | pdata->iobase = dev_read_addr(dev); |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 577 | |
| 578 | pdata->phy_interface = dev_read_phy_mode(dev); |
Marek Behún | 48631e4 | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 579 | if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 580 | return -EINVAL; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 581 | |
| 582 | pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0); |
| 583 | |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 584 | if (dev_get_driver_data(dev) == FTGMAC100_MODEL_ASPEED) { |
| 585 | priv->rxdes0_edorr_mask = BIT(30); |
| 586 | priv->txdes0_edotr_mask = BIT(30); |
| 587 | } else { |
| 588 | priv->rxdes0_edorr_mask = BIT(15); |
| 589 | priv->txdes0_edotr_mask = BIT(15); |
| 590 | } |
| 591 | |
Dylan Hung | 1a2ef08 | 2023-07-27 09:58:14 +0800 | [diff] [blame] | 592 | priv->reset_ctl = devm_reset_control_get_optional(dev, NULL); |
| 593 | |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 594 | return clk_get_bulk(dev, &priv->clks); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 595 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 596 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 597 | static int ftgmac100_probe(struct udevice *dev) |
| 598 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 599 | struct eth_pdata *pdata = dev_get_plat(dev); |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 600 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 601 | int ret; |
Macpaul Lin | c56c5a3 | 2011-09-20 19:54:32 +0000 | [diff] [blame] | 602 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 603 | priv->iobase = (struct ftgmac100 *)pdata->iobase; |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 604 | priv->phy_mode = pdata->phy_interface; |
| 605 | priv->max_speed = pdata->max_speed; |
| 606 | priv->phy_addr = 0; |
| 607 | |
Samuel Mendoza-Jonas | cf4c4f9 | 2022-08-08 21:46:05 +0930 | [diff] [blame] | 608 | if (dev_read_bool(dev, "use-ncsi")) |
| 609 | priv->phy_mode = PHY_INTERFACE_MODE_NCSI; |
| 610 | |
Thirupathaiah Annapureddy | 22bb377 | 2020-08-17 17:08:26 -0700 | [diff] [blame] | 611 | #ifdef CONFIG_PHY_ADDR |
| 612 | priv->phy_addr = CONFIG_PHY_ADDR; |
| 613 | #endif |
| 614 | |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 615 | ret = clk_enable_bulk(&priv->clks); |
| 616 | if (ret) |
| 617 | goto out; |
| 618 | |
Dylan Hung | 1a2ef08 | 2023-07-27 09:58:14 +0800 | [diff] [blame] | 619 | if (priv->reset_ctl) { |
| 620 | ret = reset_deassert(priv->reset_ctl); |
| 621 | if (ret) |
| 622 | goto out; |
| 623 | } |
| 624 | |
Dylan Hung | 2bec372 | 2021-12-09 10:12:24 +0800 | [diff] [blame] | 625 | /* |
| 626 | * If DM MDIO is enabled, the MDIO bus will be initialized later in |
| 627 | * dm_eth_phy_connect |
| 628 | */ |
Samuel Mendoza-Jonas | cf4c4f9 | 2022-08-08 21:46:05 +0930 | [diff] [blame] | 629 | if (priv->phy_mode != PHY_INTERFACE_MODE_NCSI && |
| 630 | !IS_ENABLED(CONFIG_DM_MDIO)) { |
Dylan Hung | 2bec372 | 2021-12-09 10:12:24 +0800 | [diff] [blame] | 631 | ret = ftgmac100_mdio_init(dev); |
| 632 | if (ret) { |
| 633 | dev_err(dev, "Failed to initialize mdiobus: %d\n", ret); |
| 634 | goto out; |
| 635 | } |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | ret = ftgmac100_phy_init(dev); |
| 639 | if (ret) { |
| 640 | dev_err(dev, "Failed to initialize PHY: %d\n", ret); |
| 641 | goto out; |
| 642 | } |
| 643 | |
Hongwei Zhang | 55ae590 | 2020-12-10 18:11:09 -0500 | [diff] [blame] | 644 | ftgmac_read_hwaddr(dev); |
| 645 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 646 | out: |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 647 | if (ret) |
| 648 | clk_release_bulk(&priv->clks); |
| 649 | |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 650 | return ret; |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 651 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 652 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 653 | static int ftgmac100_remove(struct udevice *dev) |
| 654 | { |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 655 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 656 | |
| 657 | free(priv->phydev); |
| 658 | mdio_unregister(priv->bus); |
| 659 | mdio_free(priv->bus); |
Dylan Hung | 1a2ef08 | 2023-07-27 09:58:14 +0800 | [diff] [blame] | 660 | if (priv->reset_ctl) |
| 661 | reset_assert(priv->reset_ctl); |
Cédric Le Goater | 6afa3f1 | 2018-10-29 07:06:36 +0100 | [diff] [blame] | 662 | clk_release_bulk(&priv->clks); |
Cédric Le Goater | 7f8482a | 2018-10-29 07:06:33 +0100 | [diff] [blame] | 663 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 664 | return 0; |
| 665 | } |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 666 | |
| 667 | static const struct eth_ops ftgmac100_ops = { |
| 668 | .start = ftgmac100_start, |
| 669 | .send = ftgmac100_send, |
| 670 | .recv = ftgmac100_recv, |
| 671 | .stop = ftgmac100_stop, |
| 672 | .free_pkt = ftgmac100_free_pkt, |
| 673 | .write_hwaddr = ftgmac100_write_hwaddr, |
| 674 | }; |
| 675 | |
| 676 | static const struct udevice_id ftgmac100_ids[] = { |
Cédric Le Goater | 35efcbb | 2018-10-29 07:06:38 +0100 | [diff] [blame] | 677 | { .compatible = "faraday,ftgmac100", .data = FTGMAC100_MODEL_FARADAY }, |
| 678 | { .compatible = "aspeed,ast2500-mac", .data = FTGMAC100_MODEL_ASPEED }, |
Dylan Hung | c84560a | 2021-12-09 10:12:25 +0800 | [diff] [blame] | 679 | { .compatible = "aspeed,ast2600-mac", .data = FTGMAC100_MODEL_ASPEED }, |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 680 | { } |
| 681 | }; |
| 682 | |
| 683 | U_BOOT_DRIVER(ftgmac100) = { |
| 684 | .name = "ftgmac100", |
| 685 | .id = UCLASS_ETH, |
| 686 | .of_match = ftgmac100_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 687 | .of_to_plat = ftgmac100_of_to_plat, |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 688 | .probe = ftgmac100_probe, |
| 689 | .remove = ftgmac100_remove, |
| 690 | .ops = &ftgmac100_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 691 | .priv_auto = sizeof(struct ftgmac100_data), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 692 | .plat_auto = sizeof(struct eth_pdata), |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 693 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 694 | }; |