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