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