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