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