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 | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 14 | #include <dm.h> |
| 15 | #include <miiphy.h> |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <net.h> |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 18 | #include <linux/io.h> |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 19 | #include <asm/dma-mapping.h> |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 20 | |
| 21 | #include "ftgmac100.h" |
| 22 | |
| 23 | #define ETH_ZLEN 60 |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 24 | #define CFG_XBUF_SIZE 1536 |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 25 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 26 | /* RBSR - hw default init value is also 0x640 */ |
| 27 | #define RBSR_DEFAULT_VALUE 0x640 |
| 28 | |
| 29 | /* PKTBUFSTX/PKTBUFSRX must both be power of 2 */ |
| 30 | #define PKTBUFSTX 4 /* must be power of 2 */ |
| 31 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 32 | /** |
| 33 | * struct ftgmac100_data - private data for the FTGMAC100 driver |
| 34 | * |
| 35 | * @iobase: The base address of the hardware registers |
| 36 | * @txdes: The array of transmit descriptors |
| 37 | * @rxdes: The array of receive descriptors |
| 38 | * @tx_index: Transmit descriptor index in @txdes |
| 39 | * @rx_index: Receive descriptor index in @rxdes |
| 40 | * @phy_addr: The PHY interface address to use |
| 41 | */ |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 42 | struct ftgmac100_data { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 43 | struct ftgmac100 *iobase; |
| 44 | |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 45 | ulong txdes_dma; |
| 46 | struct ftgmac100_txdes *txdes; |
| 47 | ulong rxdes_dma; |
| 48 | struct ftgmac100_rxdes *rxdes; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 49 | int tx_index; |
| 50 | int rx_index; |
| 51 | int phy_addr; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * struct mii_bus functions |
| 56 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 57 | static int ftgmac100_mdiobus_read(struct ftgmac100_data *priv, int phy_addr, |
| 58 | int regnum) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 59 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 60 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 61 | int phycr; |
| 62 | int i; |
| 63 | |
| 64 | phycr = readl(&ftgmac100->phycr); |
| 65 | |
| 66 | /* preserve MDC cycle threshold */ |
| 67 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 68 | |
| 69 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
| 70 | | FTGMAC100_PHYCR_REGAD(regnum) |
| 71 | | FTGMAC100_PHYCR_MIIRD; |
| 72 | |
| 73 | writel(phycr, &ftgmac100->phycr); |
| 74 | |
| 75 | for (i = 0; i < 10; i++) { |
| 76 | phycr = readl(&ftgmac100->phycr); |
| 77 | |
| 78 | if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) { |
| 79 | int data; |
| 80 | |
| 81 | data = readl(&ftgmac100->phydata); |
| 82 | return FTGMAC100_PHYDATA_MIIRDATA(data); |
| 83 | } |
| 84 | |
| 85 | mdelay(10); |
| 86 | } |
| 87 | |
| 88 | debug("mdio read timed out\n"); |
| 89 | return -1; |
| 90 | } |
| 91 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 92 | static int ftgmac100_mdiobus_write(struct ftgmac100_data *priv, int phy_addr, |
| 93 | int regnum, u16 value) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 94 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 95 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 96 | int phycr; |
| 97 | int data; |
| 98 | int i; |
| 99 | |
| 100 | phycr = readl(&ftgmac100->phycr); |
| 101 | |
| 102 | /* preserve MDC cycle threshold */ |
| 103 | phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK; |
| 104 | |
| 105 | phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
| 106 | | FTGMAC100_PHYCR_REGAD(regnum) |
| 107 | | FTGMAC100_PHYCR_MIIWR; |
| 108 | |
| 109 | data = FTGMAC100_PHYDATA_MIIWDATA(value); |
| 110 | |
| 111 | writel(data, &ftgmac100->phydata); |
| 112 | writel(phycr, &ftgmac100->phycr); |
| 113 | |
| 114 | for (i = 0; i < 10; i++) { |
| 115 | phycr = readl(&ftgmac100->phycr); |
| 116 | |
| 117 | if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0) { |
| 118 | debug("(phycr & FTGMAC100_PHYCR_MIIWR) == 0: " \ |
| 119 | "phy_addr: %x\n", phy_addr); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | mdelay(1); |
| 124 | } |
| 125 | |
| 126 | debug("mdio write timed out\n"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 130 | int ftgmac100_phy_read(struct ftgmac100_data *priv, int addr, int reg, |
| 131 | u16 *value) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 132 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 133 | *value = ftgmac100_mdiobus_read(priv, addr, reg); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 134 | |
| 135 | if (*value == -1) |
| 136 | return -1; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 141 | int ftgmac100_phy_write(struct ftgmac100_data *priv, int addr, int reg, |
| 142 | u16 value) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 143 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 144 | if (ftgmac100_mdiobus_write(priv, addr, reg, value) == -1) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 145 | return -1; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 150 | static int ftgmac100_phy_reset(struct ftgmac100_data *priv, struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 151 | { |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 152 | int i; |
| 153 | u16 status, adv; |
| 154 | |
| 155 | adv = ADVERTISE_CSMA | ADVERTISE_ALL; |
| 156 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 157 | ftgmac100_phy_write(priv, priv->phy_addr, MII_ADVERTISE, adv); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 158 | |
| 159 | printf("%s: Starting autonegotiation...\n", dev->name); |
| 160 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 161 | ftgmac100_phy_write(priv, priv->phy_addr, |
| 162 | MII_BMCR, (BMCR_ANENABLE | BMCR_ANRESTART)); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 163 | |
| 164 | for (i = 0; i < 100000 / 100; i++) { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 165 | ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &status); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 166 | |
| 167 | if (status & BMSR_ANEGCOMPLETE) |
| 168 | break; |
| 169 | mdelay(1); |
| 170 | } |
| 171 | |
| 172 | if (status & BMSR_ANEGCOMPLETE) { |
| 173 | printf("%s: Autonegotiation complete\n", dev->name); |
| 174 | } else { |
| 175 | printf("%s: Autonegotiation timed out (status=0x%04x)\n", |
| 176 | dev->name, status); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 183 | static int ftgmac100_phy_init(struct ftgmac100_data *priv, struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 184 | { |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 185 | int phy_addr; |
| 186 | u16 phy_id, status, adv, lpa, stat_ge; |
| 187 | int media, speed, duplex; |
| 188 | int i; |
| 189 | |
| 190 | /* Check if the PHY is up to snuff... */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 191 | for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) { |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 192 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 193 | ftgmac100_phy_read(priv, phy_addr, MII_PHYSID1, &phy_id); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * When it is unable to found PHY, |
| 197 | * the interface usually return 0xffff or 0x0000 |
| 198 | */ |
| 199 | if (phy_id != 0xffff && phy_id != 0x0) { |
| 200 | printf("%s: found PHY at 0x%02x\n", |
| 201 | dev->name, phy_addr); |
| 202 | priv->phy_addr = phy_addr; |
| 203 | break; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if (phy_id == 0xffff || phy_id == 0x0) { |
| 208 | printf("%s: no PHY present\n", dev->name); |
| 209 | return 0; |
| 210 | } |
| 211 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 212 | ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &status); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 213 | |
| 214 | if (!(status & BMSR_LSTATUS)) { |
| 215 | /* Try to re-negotiate if we don't have link already. */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 216 | ftgmac100_phy_reset(priv, dev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 217 | |
| 218 | for (i = 0; i < 100000 / 100; i++) { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 219 | ftgmac100_phy_read(priv, priv->phy_addr, |
| 220 | MII_BMSR, &status); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 221 | if (status & BMSR_LSTATUS) |
| 222 | break; |
| 223 | udelay(100); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (!(status & BMSR_LSTATUS)) { |
| 228 | printf("%s: link down\n", dev->name); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | #ifdef CONFIG_FTGMAC100_EGIGA |
| 233 | /* 1000 Base-T Status Register */ |
| 234 | ftgmac100_phy_read(dev, priv->phy_addr, |
| 235 | MII_STAT1000, &stat_ge); |
| 236 | |
| 237 | speed = (stat_ge & (LPA_1000FULL | LPA_1000HALF) |
| 238 | ? 1 : 0); |
| 239 | |
| 240 | duplex = ((stat_ge & LPA_1000FULL) |
| 241 | ? 1 : 0); |
| 242 | |
| 243 | if (speed) { /* Speed is 1000 */ |
| 244 | printf("%s: link up, 1000bps %s-duplex\n", |
| 245 | dev->name, duplex ? "full" : "half"); |
| 246 | return 0; |
| 247 | } |
| 248 | #endif |
| 249 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 250 | ftgmac100_phy_read(priv, priv->phy_addr, MII_ADVERTISE, &adv); |
| 251 | ftgmac100_phy_read(priv, priv->phy_addr, MII_LPA, &lpa); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 252 | |
| 253 | media = mii_nway_result(lpa & adv); |
| 254 | speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? 1 : 0); |
| 255 | duplex = (media & ADVERTISE_FULL) ? 1 : 0; |
| 256 | |
| 257 | printf("%s: link up, %sMbps %s-duplex\n", |
| 258 | dev->name, speed ? "100" : "10", duplex ? "full" : "half"); |
| 259 | |
| 260 | return 1; |
| 261 | } |
| 262 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 263 | static int ftgmac100_update_link_speed(struct ftgmac100_data *priv) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 264 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 265 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 266 | unsigned short stat_fe; |
| 267 | unsigned short stat_ge; |
| 268 | unsigned int maccr; |
| 269 | |
| 270 | #ifdef CONFIG_FTGMAC100_EGIGA |
| 271 | /* 1000 Base-T Status Register */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 272 | ftgmac100_phy_read(priv, priv->phy_addr, MII_STAT1000, &stat_ge); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 273 | #endif |
| 274 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 275 | ftgmac100_phy_read(priv, priv->phy_addr, MII_BMSR, &stat_fe); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 276 | |
| 277 | if (!(stat_fe & BMSR_LSTATUS)) /* link status up? */ |
| 278 | return 0; |
| 279 | |
| 280 | /* read MAC control register and clear related bits */ |
| 281 | maccr = readl(&ftgmac100->maccr) & |
| 282 | ~(FTGMAC100_MACCR_GIGA_MODE | |
| 283 | FTGMAC100_MACCR_FAST_MODE | |
| 284 | FTGMAC100_MACCR_FULLDUP); |
| 285 | |
| 286 | #ifdef CONFIG_FTGMAC100_EGIGA |
| 287 | if (stat_ge & LPA_1000FULL) { |
| 288 | /* set gmac for 1000BaseTX and Full Duplex */ |
| 289 | maccr |= FTGMAC100_MACCR_GIGA_MODE | FTGMAC100_MACCR_FULLDUP; |
| 290 | } |
| 291 | |
| 292 | if (stat_ge & LPA_1000HALF) { |
| 293 | /* set gmac for 1000BaseTX and Half Duplex */ |
| 294 | maccr |= FTGMAC100_MACCR_GIGA_MODE; |
| 295 | } |
| 296 | #endif |
| 297 | |
| 298 | if (stat_fe & BMSR_100FULL) { |
| 299 | /* set MII for 100BaseTX and Full Duplex */ |
| 300 | maccr |= FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_FULLDUP; |
| 301 | } |
| 302 | |
| 303 | if (stat_fe & BMSR_10FULL) { |
| 304 | /* set MII for 10BaseT and Full Duplex */ |
| 305 | maccr |= FTGMAC100_MACCR_FULLDUP; |
| 306 | } |
| 307 | |
| 308 | if (stat_fe & BMSR_100HALF) { |
| 309 | /* set MII for 100BaseTX and Half Duplex */ |
| 310 | maccr |= FTGMAC100_MACCR_FAST_MODE; |
| 311 | } |
| 312 | |
| 313 | if (stat_fe & BMSR_10HALF) { |
| 314 | /* set MII for 10BaseT and Half Duplex */ |
| 315 | /* we have already clear these bits, do nothing */ |
| 316 | ; |
| 317 | } |
| 318 | |
| 319 | /* update MII config into maccr */ |
| 320 | writel(maccr, &ftgmac100->maccr); |
| 321 | |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Reset MAC |
| 327 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 328 | static void ftgmac100_reset(struct ftgmac100_data *priv) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 329 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 330 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 331 | |
| 332 | debug("%s()\n", __func__); |
| 333 | |
Cédric Le Goater | cef951c | 2018-10-29 07:06:32 +0100 | [diff] [blame^] | 334 | setbits_le32(&ftgmac100->maccr, FTGMAC100_MACCR_SW_RST); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 335 | |
| 336 | while (readl(&ftgmac100->maccr) & FTGMAC100_MACCR_SW_RST) |
| 337 | ; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Set MAC address |
| 342 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 343 | static int ftgmac100_set_mac(struct ftgmac100_data *priv, |
| 344 | const unsigned char *mac) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 345 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 346 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 347 | unsigned int maddr = mac[0] << 8 | mac[1]; |
| 348 | unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]; |
| 349 | |
| 350 | debug("%s(%x %x)\n", __func__, maddr, laddr); |
| 351 | |
| 352 | writel(maddr, &ftgmac100->mac_madr); |
| 353 | writel(laddr, &ftgmac100->mac_ladr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 354 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 355 | return 0; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /* |
| 359 | * disable transmitter, receiver |
| 360 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 361 | static void ftgmac100_stop(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 362 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 363 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 364 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 365 | |
| 366 | debug("%s()\n", __func__); |
| 367 | |
| 368 | writel(0, &ftgmac100->maccr); |
| 369 | } |
| 370 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 371 | static int ftgmac100_start(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 372 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 373 | struct eth_pdata *plat = dev_get_platdata(dev); |
| 374 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 375 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 376 | struct ftgmac100_txdes *txdes; |
| 377 | struct ftgmac100_rxdes *rxdes; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 378 | unsigned int maccr; |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 379 | void *buf; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 380 | int i; |
| 381 | |
| 382 | debug("%s()\n", __func__); |
| 383 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 384 | ftgmac100_reset(priv); |
| 385 | |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 386 | if (!priv->txdes) { |
| 387 | txdes = dma_alloc_coherent( |
| 388 | sizeof(*txdes) * PKTBUFSTX, &priv->txdes_dma); |
| 389 | if (!txdes) |
| 390 | panic("ftgmac100: out of memory\n"); |
| 391 | memset(txdes, 0, sizeof(*txdes) * PKTBUFSTX); |
| 392 | priv->txdes = txdes; |
| 393 | } |
| 394 | txdes = priv->txdes; |
| 395 | |
| 396 | if (!priv->rxdes) { |
| 397 | rxdes = dma_alloc_coherent( |
| 398 | sizeof(*rxdes) * PKTBUFSRX, &priv->rxdes_dma); |
| 399 | if (!rxdes) |
| 400 | panic("ftgmac100: out of memory\n"); |
| 401 | memset(rxdes, 0, sizeof(*rxdes) * PKTBUFSRX); |
| 402 | priv->rxdes = rxdes; |
| 403 | } |
| 404 | rxdes = priv->rxdes; |
| 405 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 406 | /* set the ethernet address */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 407 | ftgmac100_set_mac(priv, plat->enetaddr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 408 | |
| 409 | /* disable all interrupts */ |
| 410 | writel(0, &ftgmac100->ier); |
| 411 | |
| 412 | /* initialize descriptors */ |
| 413 | priv->tx_index = 0; |
| 414 | priv->rx_index = 0; |
| 415 | |
| 416 | txdes[PKTBUFSTX - 1].txdes0 = FTGMAC100_TXDES0_EDOTR; |
| 417 | rxdes[PKTBUFSRX - 1].rxdes0 = FTGMAC100_RXDES0_EDORR; |
| 418 | |
| 419 | for (i = 0; i < PKTBUFSTX; i++) { |
| 420 | /* TXBUF_BADR */ |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 421 | if (!txdes[i].txdes2) { |
| 422 | buf = memalign(ARCH_DMA_MINALIGN, CFG_XBUF_SIZE); |
| 423 | if (!buf) |
| 424 | panic("ftgmac100: out of memory\n"); |
| 425 | txdes[i].txdes3 = virt_to_phys(buf); |
| 426 | txdes[i].txdes2 = (uint)buf; |
| 427 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 428 | txdes[i].txdes1 = 0; |
| 429 | } |
| 430 | |
| 431 | for (i = 0; i < PKTBUFSRX; i++) { |
| 432 | /* RXBUF_BADR */ |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 433 | if (!rxdes[i].rxdes2) { |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 434 | buf = net_rx_packets[i]; |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 435 | rxdes[i].rxdes3 = virt_to_phys(buf); |
| 436 | rxdes[i].rxdes2 = (uint)buf; |
| 437 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 438 | rxdes[i].rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY; |
| 439 | } |
| 440 | |
| 441 | /* transmit ring */ |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 442 | writel(priv->txdes_dma, &ftgmac100->txr_badr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 443 | |
| 444 | /* receive ring */ |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 445 | writel(priv->rxdes_dma, &ftgmac100->rxr_badr); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 446 | |
| 447 | /* poll receive descriptor automatically */ |
| 448 | writel(FTGMAC100_APTC_RXPOLL_CNT(1), &ftgmac100->aptc); |
| 449 | |
| 450 | /* config receive buffer size register */ |
| 451 | writel(FTGMAC100_RBSR_SIZE(RBSR_DEFAULT_VALUE), &ftgmac100->rbsr); |
| 452 | |
| 453 | /* enable transmitter, receiver */ |
| 454 | maccr = FTGMAC100_MACCR_TXMAC_EN | |
| 455 | FTGMAC100_MACCR_RXMAC_EN | |
| 456 | FTGMAC100_MACCR_TXDMA_EN | |
| 457 | FTGMAC100_MACCR_RXDMA_EN | |
| 458 | FTGMAC100_MACCR_CRC_APD | |
| 459 | FTGMAC100_MACCR_FULLDUP | |
| 460 | FTGMAC100_MACCR_RX_RUNT | |
| 461 | FTGMAC100_MACCR_RX_BROADPKT; |
| 462 | |
| 463 | writel(maccr, &ftgmac100->maccr); |
| 464 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 465 | if (!ftgmac100_phy_init(priv, dev)) { |
| 466 | if (!ftgmac100_update_link_speed(priv)) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 467 | return -1; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 473 | static int ftgmac100_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 474 | { |
| 475 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 476 | struct ftgmac100_rxdes *curr_des = &priv->rxdes[priv->rx_index]; |
| 477 | |
| 478 | /* Release buffer to DMA */ |
| 479 | curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY; |
| 480 | |
| 481 | /* Move to next descriptor */ |
| 482 | priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX; |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 487 | /* |
| 488 | * Get a data block via Ethernet |
| 489 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 490 | static int ftgmac100_recv(struct udevice *dev, int flags, uchar **packetp) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 491 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 492 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 493 | struct ftgmac100_rxdes *curr_des; |
| 494 | unsigned short rxlen; |
| 495 | |
| 496 | curr_des = &priv->rxdes[priv->rx_index]; |
| 497 | |
| 498 | if (!(curr_des->rxdes0 & FTGMAC100_RXDES0_RXPKT_RDY)) |
| 499 | return -1; |
| 500 | |
| 501 | if (curr_des->rxdes0 & (FTGMAC100_RXDES0_RX_ERR | |
| 502 | FTGMAC100_RXDES0_CRC_ERR | |
| 503 | FTGMAC100_RXDES0_FTL | |
| 504 | FTGMAC100_RXDES0_RUNT | |
| 505 | FTGMAC100_RXDES0_RX_ODD_NB)) { |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | rxlen = FTGMAC100_RXDES0_VDBC(curr_des->rxdes0); |
| 510 | |
| 511 | debug("%s(): RX buffer %d, %x received\n", |
| 512 | __func__, priv->rx_index, rxlen); |
| 513 | |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 514 | /* invalidate d-cache */ |
| 515 | dma_map_single((void *)curr_des->rxdes2, rxlen, DMA_FROM_DEVICE); |
| 516 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 517 | /* pass the packet up to the protocol layers. */ |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 518 | net_process_received_packet((void *)curr_des->rxdes2, rxlen); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 519 | |
| 520 | /* release buffer to DMA */ |
| 521 | curr_des->rxdes0 &= ~FTGMAC100_RXDES0_RXPKT_RDY; |
| 522 | |
| 523 | priv->rx_index = (priv->rx_index + 1) % PKTBUFSRX; |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Send a data block via Ethernet |
| 530 | */ |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 531 | static int ftgmac100_send(struct udevice *dev, void *packet, int length) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 532 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 533 | struct ftgmac100_data *priv = dev_get_priv(dev); |
| 534 | struct ftgmac100 *ftgmac100 = priv->iobase; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 535 | struct ftgmac100_txdes *curr_des = &priv->txdes[priv->tx_index]; |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 536 | |
| 537 | if (curr_des->txdes0 & FTGMAC100_TXDES0_TXDMA_OWN) { |
| 538 | debug("%s(): no TX descriptor available\n", __func__); |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | debug("%s(%x, %x)\n", __func__, (int)packet, length); |
| 543 | |
| 544 | length = (length < ETH_ZLEN) ? ETH_ZLEN : length; |
| 545 | |
Kuo-Jung Su | cd19bd3 | 2013-05-07 14:33:51 +0800 | [diff] [blame] | 546 | memcpy((void *)curr_des->txdes2, (void *)packet, length); |
| 547 | dma_map_single((void *)curr_des->txdes2, length, DMA_TO_DEVICE); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 548 | |
| 549 | /* only one descriptor on TXBUF */ |
| 550 | curr_des->txdes0 &= FTGMAC100_TXDES0_EDOTR; |
| 551 | curr_des->txdes0 |= FTGMAC100_TXDES0_FTS | |
| 552 | FTGMAC100_TXDES0_LTS | |
| 553 | FTGMAC100_TXDES0_TXBUF_SIZE(length) | |
| 554 | FTGMAC100_TXDES0_TXDMA_OWN ; |
| 555 | |
| 556 | /* start transmit */ |
| 557 | writel(1, &ftgmac100->txpd); |
| 558 | |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 559 | debug("%s(): packet sent\n", __func__); |
| 560 | |
| 561 | priv->tx_index = (priv->tx_index + 1) % PKTBUFSTX; |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 566 | static int ftgmac100_write_hwaddr(struct udevice *dev) |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 567 | { |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 568 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 569 | struct ftgmac100_data *priv = dev_get_priv(dev); |
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 | return ftgmac100_set_mac(priv, pdata->enetaddr); |
| 572 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 573 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 574 | static int ftgmac100_ofdata_to_platdata(struct udevice *dev) |
| 575 | { |
| 576 | struct eth_pdata *pdata = dev_get_platdata(dev); |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 577 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 578 | pdata->iobase = devfdt_get_addr(dev); |
| 579 | return 0; |
| 580 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 581 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 582 | static int ftgmac100_probe(struct udevice *dev) |
| 583 | { |
| 584 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 585 | struct ftgmac100_data *priv = dev_get_priv(dev); |
Macpaul Lin | c56c5a3 | 2011-09-20 19:54:32 +0000 | [diff] [blame] | 586 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 587 | priv->iobase = (struct ftgmac100 *)pdata->iobase; |
| 588 | return 0; |
| 589 | } |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 590 | |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 591 | static int ftgmac100_remove(struct udevice *dev) |
| 592 | { |
Macpaul Lin | 199c625 | 2010-12-21 16:59:46 +0800 | [diff] [blame] | 593 | return 0; |
| 594 | } |
Cédric Le Goater | 38b33e9 | 2018-10-29 07:06:31 +0100 | [diff] [blame] | 595 | |
| 596 | static const struct eth_ops ftgmac100_ops = { |
| 597 | .start = ftgmac100_start, |
| 598 | .send = ftgmac100_send, |
| 599 | .recv = ftgmac100_recv, |
| 600 | .stop = ftgmac100_stop, |
| 601 | .free_pkt = ftgmac100_free_pkt, |
| 602 | .write_hwaddr = ftgmac100_write_hwaddr, |
| 603 | }; |
| 604 | |
| 605 | static const struct udevice_id ftgmac100_ids[] = { |
| 606 | { .compatible = "faraday,ftgmac100" }, |
| 607 | { } |
| 608 | }; |
| 609 | |
| 610 | U_BOOT_DRIVER(ftgmac100) = { |
| 611 | .name = "ftgmac100", |
| 612 | .id = UCLASS_ETH, |
| 613 | .of_match = ftgmac100_ids, |
| 614 | .ofdata_to_platdata = ftgmac100_ofdata_to_platdata, |
| 615 | .probe = ftgmac100_probe, |
| 616 | .remove = ftgmac100_remove, |
| 617 | .ops = &ftgmac100_ops, |
| 618 | .priv_auto_alloc_size = sizeof(struct ftgmac100_data), |
| 619 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 620 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 621 | }; |