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