Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Michal Simek <monstr@monstr.eu> |
| 3 | * Copyright (C) 2011 PetaLogix |
| 4 | * Copyright (C) 2010 Xilinx, Inc. All rights reserved. |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <config.h> |
| 10 | #include <common.h> |
| 11 | #include <net.h> |
| 12 | #include <malloc.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <phy.h> |
| 15 | #include <miiphy.h> |
| 16 | |
| 17 | #if !defined(CONFIG_PHYLIB) |
| 18 | # error AXI_ETHERNET requires PHYLIB |
| 19 | #endif |
| 20 | |
| 21 | /* Link setup */ |
| 22 | #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */ |
| 23 | #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */ |
| 24 | #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */ |
| 25 | #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */ |
| 26 | |
| 27 | /* Interrupt Status/Enable/Mask Registers bit definitions */ |
| 28 | #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */ |
| 29 | #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */ |
| 30 | |
| 31 | /* Receive Configuration Word 1 (RCW1) Register bit definitions */ |
| 32 | #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */ |
| 33 | |
| 34 | /* Transmitter Configuration (TC) Register bit definitions */ |
| 35 | #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */ |
| 36 | |
| 37 | #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF |
| 38 | |
| 39 | /* MDIO Management Configuration (MC) Register bit definitions */ |
| 40 | #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/ |
| 41 | |
| 42 | /* MDIO Management Control Register (MCR) Register bit definitions */ |
| 43 | #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */ |
| 44 | #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */ |
| 45 | #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */ |
| 46 | #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */ |
| 47 | #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */ |
| 48 | #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */ |
| 49 | #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */ |
| 50 | #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */ |
| 51 | |
| 52 | #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */ |
| 53 | |
| 54 | /* DMA macros */ |
| 55 | /* Bitmasks of XAXIDMA_CR_OFFSET register */ |
| 56 | #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */ |
| 57 | #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */ |
| 58 | |
| 59 | /* Bitmasks of XAXIDMA_SR_OFFSET register */ |
| 60 | #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */ |
| 61 | |
| 62 | /* Bitmask for interrupts */ |
| 63 | #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */ |
| 64 | #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */ |
| 65 | #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */ |
| 66 | |
| 67 | /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */ |
| 68 | #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */ |
| 69 | #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */ |
| 70 | |
| 71 | #define DMAALIGN 128 |
| 72 | |
| 73 | static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN))); |
| 74 | |
| 75 | /* Reflect dma offsets */ |
| 76 | struct axidma_reg { |
| 77 | u32 control; /* DMACR */ |
| 78 | u32 status; /* DMASR */ |
| 79 | u32 current; /* CURDESC */ |
| 80 | u32 reserved; |
| 81 | u32 tail; /* TAILDESC */ |
| 82 | }; |
| 83 | |
| 84 | /* Private driver structures */ |
| 85 | struct axidma_priv { |
| 86 | struct axidma_reg *dmatx; |
| 87 | struct axidma_reg *dmarx; |
| 88 | int phyaddr; |
Michal Simek | 6cb55e7 | 2015-12-09 14:39:42 +0100 | [diff] [blame] | 89 | struct axi_regs *iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 90 | struct phy_device *phydev; |
| 91 | struct mii_dev *bus; |
| 92 | }; |
| 93 | |
| 94 | /* BD descriptors */ |
| 95 | struct axidma_bd { |
| 96 | u32 next; /* Next descriptor pointer */ |
| 97 | u32 reserved1; |
| 98 | u32 phys; /* Buffer address */ |
| 99 | u32 reserved2; |
| 100 | u32 reserved3; |
| 101 | u32 reserved4; |
| 102 | u32 cntrl; /* Control */ |
| 103 | u32 status; /* Status */ |
| 104 | u32 app0; |
| 105 | u32 app1; /* TX start << 16 | insert */ |
| 106 | u32 app2; /* TX csum seed */ |
| 107 | u32 app3; |
| 108 | u32 app4; |
| 109 | u32 sw_id_offset; |
| 110 | u32 reserved5; |
| 111 | u32 reserved6; |
| 112 | }; |
| 113 | |
| 114 | /* Static BDs - driver uses only one BD */ |
| 115 | static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN))); |
| 116 | static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN))); |
| 117 | |
| 118 | struct axi_regs { |
| 119 | u32 reserved[3]; |
| 120 | u32 is; /* 0xC: Interrupt status */ |
| 121 | u32 reserved2; |
| 122 | u32 ie; /* 0x14: Interrupt enable */ |
| 123 | u32 reserved3[251]; |
| 124 | u32 rcw1; /* 0x404: Rx Configuration Word 1 */ |
| 125 | u32 tc; /* 0x408: Tx Configuration */ |
| 126 | u32 reserved4; |
| 127 | u32 emmc; /* 0x410: EMAC mode configuration */ |
| 128 | u32 reserved5[59]; |
| 129 | u32 mdio_mc; /* 0x500: MII Management Config */ |
| 130 | u32 mdio_mcr; /* 0x504: MII Management Control */ |
| 131 | u32 mdio_mwd; /* 0x508: MII Management Write Data */ |
| 132 | u32 mdio_mrd; /* 0x50C: MII Management Read Data */ |
| 133 | u32 reserved6[124]; |
| 134 | u32 uaw0; /* 0x700: Unicast address word 0 */ |
| 135 | u32 uaw1; /* 0x704: Unicast address word 1 */ |
| 136 | }; |
| 137 | |
| 138 | /* Use MII register 1 (MII status register) to detect PHY */ |
| 139 | #define PHY_DETECT_REG 1 |
| 140 | |
| 141 | /* |
| 142 | * Mask used to verify certain PHY features (or register contents) |
| 143 | * in the register above: |
| 144 | * 0x1000: 10Mbps full duplex support |
| 145 | * 0x0800: 10Mbps half duplex support |
| 146 | * 0x0008: Auto-negotiation support |
| 147 | */ |
| 148 | #define PHY_DETECT_MASK 0x1808 |
| 149 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 150 | static inline int mdio_wait(struct axi_regs *regs) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 151 | { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 152 | u32 timeout = 200; |
| 153 | |
| 154 | /* Wait till MDIO interface is ready to accept a new transaction. */ |
| 155 | while (timeout && (!(in_be32(®s->mdio_mcr) |
| 156 | & XAE_MDIO_MCR_READY_MASK))) { |
| 157 | timeout--; |
| 158 | udelay(1); |
| 159 | } |
| 160 | if (!timeout) { |
| 161 | printf("%s: Timeout\n", __func__); |
| 162 | return 1; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 167 | static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum, |
| 168 | u16 *val) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 169 | { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 170 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 171 | u32 mdioctrlreg = 0; |
| 172 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 173 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 174 | return 1; |
| 175 | |
| 176 | mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) & |
| 177 | XAE_MDIO_MCR_PHYAD_MASK) | |
| 178 | ((registernum << XAE_MDIO_MCR_REGAD_SHIFT) |
| 179 | & XAE_MDIO_MCR_REGAD_MASK) | |
| 180 | XAE_MDIO_MCR_INITIATE_MASK | |
| 181 | XAE_MDIO_MCR_OP_READ_MASK; |
| 182 | |
| 183 | out_be32(®s->mdio_mcr, mdioctrlreg); |
| 184 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 185 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 186 | return 1; |
| 187 | |
| 188 | /* Read data */ |
| 189 | *val = in_be32(®s->mdio_mrd); |
| 190 | return 0; |
| 191 | } |
| 192 | |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 193 | static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum, |
| 194 | u32 data) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 195 | { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 196 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 197 | u32 mdioctrlreg = 0; |
| 198 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 199 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 200 | return 1; |
| 201 | |
| 202 | mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) & |
| 203 | XAE_MDIO_MCR_PHYAD_MASK) | |
| 204 | ((registernum << XAE_MDIO_MCR_REGAD_SHIFT) |
| 205 | & XAE_MDIO_MCR_REGAD_MASK) | |
| 206 | XAE_MDIO_MCR_INITIATE_MASK | |
| 207 | XAE_MDIO_MCR_OP_WRITE_MASK; |
| 208 | |
| 209 | /* Write data */ |
| 210 | out_be32(®s->mdio_mwd, data); |
| 211 | |
| 212 | out_be32(®s->mdio_mcr, mdioctrlreg); |
| 213 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 214 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 215 | return 1; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Setting axi emac and phy to proper setting */ |
| 221 | static int setup_phy(struct eth_device *dev) |
| 222 | { |
| 223 | u16 phyreg; |
| 224 | u32 i, speed, emmc_reg, ret; |
| 225 | struct axidma_priv *priv = dev->priv; |
Michal Simek | 6cb55e7 | 2015-12-09 14:39:42 +0100 | [diff] [blame] | 226 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 227 | struct phy_device *phydev; |
| 228 | |
| 229 | u32 supported = SUPPORTED_10baseT_Half | |
| 230 | SUPPORTED_10baseT_Full | |
| 231 | SUPPORTED_100baseT_Half | |
| 232 | SUPPORTED_100baseT_Full | |
| 233 | SUPPORTED_1000baseT_Half | |
| 234 | SUPPORTED_1000baseT_Full; |
| 235 | |
| 236 | if (priv->phyaddr == -1) { |
| 237 | /* Detect the PHY address */ |
| 238 | for (i = 31; i >= 0; i--) { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 239 | ret = phyread(priv, i, PHY_DETECT_REG, &phyreg); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 240 | if (!ret && (phyreg != 0xFFFF) && |
| 241 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 242 | /* Found a valid PHY address */ |
| 243 | priv->phyaddr = i; |
| 244 | debug("axiemac: Found valid phy address, %x\n", |
Michal Simek | 2f1e065 | 2015-12-09 10:54:53 +0100 | [diff] [blame] | 245 | i); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /* Interface - look at tsec */ |
| 252 | phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0); |
| 253 | |
| 254 | phydev->supported &= supported; |
| 255 | phydev->advertising = phydev->supported; |
| 256 | priv->phydev = phydev; |
| 257 | phy_config(phydev); |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 258 | if (phy_startup(phydev)) { |
| 259 | printf("axiemac: could not initialize PHY %s\n", |
| 260 | phydev->dev->name); |
| 261 | return 0; |
| 262 | } |
Michal Simek | 5848f13 | 2013-11-21 16:15:51 +0100 | [diff] [blame] | 263 | if (!phydev->link) { |
| 264 | printf("%s: No link.\n", phydev->dev->name); |
| 265 | return 0; |
| 266 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 267 | |
| 268 | switch (phydev->speed) { |
| 269 | case 1000: |
| 270 | speed = XAE_EMMC_LINKSPD_1000; |
| 271 | break; |
| 272 | case 100: |
| 273 | speed = XAE_EMMC_LINKSPD_100; |
| 274 | break; |
| 275 | case 10: |
| 276 | speed = XAE_EMMC_LINKSPD_10; |
| 277 | break; |
| 278 | default: |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* Setup the emac for the phy speed */ |
| 283 | emmc_reg = in_be32(®s->emmc); |
| 284 | emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK; |
| 285 | emmc_reg |= speed; |
| 286 | |
| 287 | /* Write new speed setting out to Axi Ethernet */ |
| 288 | out_be32(®s->emmc, emmc_reg); |
| 289 | |
| 290 | /* |
| 291 | * Setting the operating speed of the MAC needs a delay. There |
| 292 | * doesn't seem to be register to poll, so please consider this |
| 293 | * during your application design. |
| 294 | */ |
| 295 | udelay(1); |
| 296 | |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | /* STOP DMA transfers */ |
| 301 | static void axiemac_halt(struct eth_device *dev) |
| 302 | { |
| 303 | struct axidma_priv *priv = dev->priv; |
| 304 | u32 temp; |
| 305 | |
| 306 | /* Stop the hardware */ |
| 307 | temp = in_be32(&priv->dmatx->control); |
| 308 | temp &= ~XAXIDMA_CR_RUNSTOP_MASK; |
| 309 | out_be32(&priv->dmatx->control, temp); |
| 310 | |
| 311 | temp = in_be32(&priv->dmarx->control); |
| 312 | temp &= ~XAXIDMA_CR_RUNSTOP_MASK; |
| 313 | out_be32(&priv->dmarx->control, temp); |
| 314 | |
| 315 | debug("axiemac: Halted\n"); |
| 316 | } |
| 317 | |
| 318 | static int axi_ethernet_init(struct eth_device *dev) |
| 319 | { |
| 320 | struct axi_regs *regs = (struct axi_regs *)dev->iobase; |
| 321 | u32 timeout = 200; |
| 322 | |
| 323 | /* |
| 324 | * Check the status of the MgtRdy bit in the interrupt status |
| 325 | * registers. This must be done to allow the MGT clock to become stable |
| 326 | * for the Sgmii and 1000BaseX PHY interfaces. No other register reads |
| 327 | * will be valid until this bit is valid. |
| 328 | * The bit is always a 1 for all other PHY interfaces. |
| 329 | */ |
| 330 | while (timeout && (!(in_be32(®s->is) & XAE_INT_MGTRDY_MASK))) { |
| 331 | timeout--; |
| 332 | udelay(1); |
| 333 | } |
| 334 | if (!timeout) { |
| 335 | printf("%s: Timeout\n", __func__); |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | /* Stop the device and reset HW */ |
| 340 | /* Disable interrupts */ |
| 341 | out_be32(®s->ie, 0); |
| 342 | |
| 343 | /* Disable the receiver */ |
| 344 | out_be32(®s->rcw1, in_be32(®s->rcw1) & ~XAE_RCW1_RX_MASK); |
| 345 | |
| 346 | /* |
| 347 | * Stopping the receiver in mid-packet causes a dropped packet |
| 348 | * indication from HW. Clear it. |
| 349 | */ |
| 350 | /* Set the interrupt status register to clear the interrupt */ |
| 351 | out_be32(®s->is, XAE_INT_RXRJECT_MASK); |
| 352 | |
| 353 | /* Setup HW */ |
| 354 | /* Set default MDIO divisor */ |
| 355 | out_be32(®s->mdio_mc, XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK); |
| 356 | |
| 357 | debug("axiemac: InitHw done\n"); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | static int axiemac_setup_mac(struct eth_device *dev) |
| 362 | { |
| 363 | struct axi_regs *regs = (struct axi_regs *)dev->iobase; |
| 364 | |
| 365 | /* Set the MAC address */ |
| 366 | int val = ((dev->enetaddr[3] << 24) | (dev->enetaddr[2] << 16) | |
| 367 | (dev->enetaddr[1] << 8) | (dev->enetaddr[0])); |
| 368 | out_be32(®s->uaw0, val); |
| 369 | |
| 370 | val = (dev->enetaddr[5] << 8) | dev->enetaddr[4] ; |
| 371 | val |= in_be32(®s->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK; |
| 372 | out_be32(®s->uaw1, val); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* Reset DMA engine */ |
| 377 | static void axi_dma_init(struct eth_device *dev) |
| 378 | { |
| 379 | struct axidma_priv *priv = dev->priv; |
| 380 | u32 timeout = 500; |
| 381 | |
| 382 | /* Reset the engine so the hardware starts from a known state */ |
| 383 | out_be32(&priv->dmatx->control, XAXIDMA_CR_RESET_MASK); |
| 384 | out_be32(&priv->dmarx->control, XAXIDMA_CR_RESET_MASK); |
| 385 | |
| 386 | /* At the initialization time, hardware should finish reset quickly */ |
| 387 | while (timeout--) { |
| 388 | /* Check transmit/receive channel */ |
| 389 | /* Reset is done when the reset bit is low */ |
Michal Simek | 5aa4539 | 2015-10-28 11:00:47 +0100 | [diff] [blame] | 390 | if (!((in_be32(&priv->dmatx->control) | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 391 | in_be32(&priv->dmarx->control)) |
Michal Simek | 5aa4539 | 2015-10-28 11:00:47 +0100 | [diff] [blame] | 392 | & XAXIDMA_CR_RESET_MASK)) { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 393 | break; |
| 394 | } |
| 395 | } |
| 396 | if (!timeout) |
| 397 | printf("%s: Timeout\n", __func__); |
| 398 | } |
| 399 | |
| 400 | static int axiemac_init(struct eth_device *dev, bd_t * bis) |
| 401 | { |
| 402 | struct axidma_priv *priv = dev->priv; |
| 403 | struct axi_regs *regs = (struct axi_regs *)dev->iobase; |
| 404 | u32 temp; |
| 405 | |
| 406 | debug("axiemac: Init started\n"); |
| 407 | /* |
| 408 | * Initialize AXIDMA engine. AXIDMA engine must be initialized before |
| 409 | * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is |
| 410 | * reset, and since AXIDMA reset line is connected to AxiEthernet, this |
| 411 | * would ensure a reset of AxiEthernet. |
| 412 | */ |
| 413 | axi_dma_init(dev); |
| 414 | |
| 415 | /* Initialize AxiEthernet hardware. */ |
| 416 | if (axi_ethernet_init(dev)) |
| 417 | return -1; |
| 418 | |
| 419 | /* Disable all RX interrupts before RxBD space setup */ |
| 420 | temp = in_be32(&priv->dmarx->control); |
| 421 | temp &= ~XAXIDMA_IRQ_ALL_MASK; |
| 422 | out_be32(&priv->dmarx->control, temp); |
| 423 | |
| 424 | /* Start DMA RX channel. Now it's ready to receive data.*/ |
| 425 | out_be32(&priv->dmarx->current, (u32)&rx_bd); |
| 426 | |
| 427 | /* Setup the BD. */ |
| 428 | memset(&rx_bd, 0, sizeof(rx_bd)); |
| 429 | rx_bd.next = (u32)&rx_bd; |
| 430 | rx_bd.phys = (u32)&rxframe; |
| 431 | rx_bd.cntrl = sizeof(rxframe); |
| 432 | /* Flush the last BD so DMA core could see the updates */ |
| 433 | flush_cache((u32)&rx_bd, sizeof(rx_bd)); |
| 434 | |
| 435 | /* It is necessary to flush rxframe because if you don't do it |
| 436 | * then cache can contain uninitialized data */ |
| 437 | flush_cache((u32)&rxframe, sizeof(rxframe)); |
| 438 | |
| 439 | /* Start the hardware */ |
| 440 | temp = in_be32(&priv->dmarx->control); |
| 441 | temp |= XAXIDMA_CR_RUNSTOP_MASK; |
| 442 | out_be32(&priv->dmarx->control, temp); |
| 443 | |
| 444 | /* Rx BD is ready - start */ |
| 445 | out_be32(&priv->dmarx->tail, (u32)&rx_bd); |
| 446 | |
| 447 | /* Enable TX */ |
| 448 | out_be32(®s->tc, XAE_TC_TX_MASK); |
| 449 | /* Enable RX */ |
| 450 | out_be32(®s->rcw1, XAE_RCW1_RX_MASK); |
| 451 | |
| 452 | /* PHY setup */ |
| 453 | if (!setup_phy(dev)) { |
| 454 | axiemac_halt(dev); |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | debug("axiemac: Init complete\n"); |
| 459 | return 0; |
| 460 | } |
| 461 | |
Stephan Linz | 958ea37 | 2012-05-22 12:18:11 +0000 | [diff] [blame] | 462 | static int axiemac_send(struct eth_device *dev, void *ptr, int len) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 463 | { |
| 464 | struct axidma_priv *priv = dev->priv; |
| 465 | u32 timeout; |
| 466 | |
| 467 | if (len > PKTSIZE_ALIGN) |
| 468 | len = PKTSIZE_ALIGN; |
| 469 | |
| 470 | /* Flush packet to main memory to be trasfered by DMA */ |
| 471 | flush_cache((u32)ptr, len); |
| 472 | |
| 473 | /* Setup Tx BD */ |
| 474 | memset(&tx_bd, 0, sizeof(tx_bd)); |
| 475 | /* At the end of the ring, link the last BD back to the top */ |
| 476 | tx_bd.next = (u32)&tx_bd; |
| 477 | tx_bd.phys = (u32)ptr; |
| 478 | /* Save len */ |
| 479 | tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK | |
| 480 | XAXIDMA_BD_CTRL_TXEOF_MASK; |
| 481 | |
| 482 | /* Flush the last BD so DMA core could see the updates */ |
| 483 | flush_cache((u32)&tx_bd, sizeof(tx_bd)); |
| 484 | |
| 485 | if (in_be32(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) { |
| 486 | u32 temp; |
| 487 | out_be32(&priv->dmatx->current, (u32)&tx_bd); |
| 488 | /* Start the hardware */ |
| 489 | temp = in_be32(&priv->dmatx->control); |
| 490 | temp |= XAXIDMA_CR_RUNSTOP_MASK; |
| 491 | out_be32(&priv->dmatx->control, temp); |
| 492 | } |
| 493 | |
| 494 | /* Start transfer */ |
| 495 | out_be32(&priv->dmatx->tail, (u32)&tx_bd); |
| 496 | |
| 497 | /* Wait for transmission to complete */ |
| 498 | debug("axiemac: Waiting for tx to be done\n"); |
| 499 | timeout = 200; |
Michal Simek | 5aa4539 | 2015-10-28 11:00:47 +0100 | [diff] [blame] | 500 | while (timeout && (!(in_be32(&priv->dmatx->status) & |
| 501 | (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 502 | timeout--; |
| 503 | udelay(1); |
| 504 | } |
| 505 | if (!timeout) { |
| 506 | printf("%s: Timeout\n", __func__); |
| 507 | return 1; |
| 508 | } |
| 509 | |
| 510 | debug("axiemac: Sending complete\n"); |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int isrxready(struct eth_device *dev) |
| 515 | { |
| 516 | u32 status; |
| 517 | struct axidma_priv *priv = dev->priv; |
| 518 | |
| 519 | /* Read pending interrupts */ |
| 520 | status = in_be32(&priv->dmarx->status); |
| 521 | |
| 522 | /* Acknowledge pending interrupts */ |
| 523 | out_be32(&priv->dmarx->status, status & XAXIDMA_IRQ_ALL_MASK); |
| 524 | |
| 525 | /* |
| 526 | * If Reception done interrupt is asserted, call RX call back function |
| 527 | * to handle the processed BDs and then raise the according flag. |
| 528 | */ |
| 529 | if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK))) |
| 530 | return 1; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static int axiemac_recv(struct eth_device *dev) |
| 536 | { |
| 537 | u32 length; |
| 538 | struct axidma_priv *priv = dev->priv; |
| 539 | u32 temp; |
| 540 | |
| 541 | /* Wait for an incoming packet */ |
| 542 | if (!isrxready(dev)) |
| 543 | return 0; |
| 544 | |
| 545 | debug("axiemac: RX data ready\n"); |
| 546 | |
| 547 | /* Disable IRQ for a moment till packet is handled */ |
| 548 | temp = in_be32(&priv->dmarx->control); |
| 549 | temp &= ~XAXIDMA_IRQ_ALL_MASK; |
| 550 | out_be32(&priv->dmarx->control, temp); |
| 551 | |
| 552 | length = rx_bd.app4 & 0xFFFF; /* max length mask */ |
| 553 | #ifdef DEBUG |
| 554 | print_buffer(&rxframe, &rxframe[0], 1, length, 16); |
| 555 | #endif |
| 556 | /* Pass the received frame up for processing */ |
| 557 | if (length) |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 558 | net_process_received_packet(rxframe, length); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 559 | |
| 560 | #ifdef DEBUG |
| 561 | /* It is useful to clear buffer to be sure that it is consistent */ |
| 562 | memset(rxframe, 0, sizeof(rxframe)); |
| 563 | #endif |
| 564 | /* Setup RxBD */ |
| 565 | /* Clear the whole buffer and setup it again - all flags are cleared */ |
| 566 | memset(&rx_bd, 0, sizeof(rx_bd)); |
| 567 | rx_bd.next = (u32)&rx_bd; |
| 568 | rx_bd.phys = (u32)&rxframe; |
| 569 | rx_bd.cntrl = sizeof(rxframe); |
| 570 | |
| 571 | /* Write bd to HW */ |
| 572 | flush_cache((u32)&rx_bd, sizeof(rx_bd)); |
| 573 | |
| 574 | /* It is necessary to flush rxframe because if you don't do it |
| 575 | * then cache will contain previous packet */ |
| 576 | flush_cache((u32)&rxframe, sizeof(rxframe)); |
| 577 | |
| 578 | /* Rx BD is ready - start again */ |
| 579 | out_be32(&priv->dmarx->tail, (u32)&rx_bd); |
| 580 | |
| 581 | debug("axiemac: RX completed, framelength = %d\n", length); |
| 582 | |
| 583 | return length; |
| 584 | } |
| 585 | |
| 586 | static int axiemac_miiphy_read(const char *devname, uchar addr, |
| 587 | uchar reg, ushort *val) |
| 588 | { |
| 589 | struct eth_device *dev = eth_get_dev(); |
| 590 | u32 ret; |
| 591 | |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 592 | ret = phyread(dev->priv, addr, reg, val); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 593 | debug("axiemac: Read MII 0x%x, 0x%x, 0x%x\n", addr, reg, *val); |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | static int axiemac_miiphy_write(const char *devname, uchar addr, |
| 598 | uchar reg, ushort val) |
| 599 | { |
| 600 | struct eth_device *dev = eth_get_dev(); |
| 601 | |
| 602 | debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, val); |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame^] | 603 | return phywrite(dev->priv, addr, reg, val); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int axiemac_bus_reset(struct mii_dev *bus) |
| 607 | { |
| 608 | debug("axiemac: Bus reset\n"); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | int xilinx_axiemac_initialize(bd_t *bis, unsigned long base_addr, |
| 613 | unsigned long dma_addr) |
| 614 | { |
| 615 | struct eth_device *dev; |
| 616 | struct axidma_priv *priv; |
| 617 | |
| 618 | dev = calloc(1, sizeof(struct eth_device)); |
| 619 | if (dev == NULL) |
| 620 | return -1; |
| 621 | |
| 622 | dev->priv = calloc(1, sizeof(struct axidma_priv)); |
| 623 | if (dev->priv == NULL) { |
| 624 | free(dev); |
| 625 | return -1; |
| 626 | } |
| 627 | priv = dev->priv; |
| 628 | |
| 629 | sprintf(dev->name, "aximac.%lx", base_addr); |
| 630 | |
| 631 | dev->iobase = base_addr; |
Michal Simek | 6cb55e7 | 2015-12-09 14:39:42 +0100 | [diff] [blame] | 632 | priv->iobase = (struct axi_regs *)base_addr; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 633 | priv->dmatx = (struct axidma_reg *)dma_addr; |
| 634 | /* RX channel offset is 0x30 */ |
| 635 | priv->dmarx = (struct axidma_reg *)(dma_addr + 0x30); |
| 636 | dev->init = axiemac_init; |
| 637 | dev->halt = axiemac_halt; |
| 638 | dev->send = axiemac_send; |
| 639 | dev->recv = axiemac_recv; |
| 640 | dev->write_hwaddr = axiemac_setup_mac; |
| 641 | |
| 642 | #ifdef CONFIG_PHY_ADDR |
| 643 | priv->phyaddr = CONFIG_PHY_ADDR; |
| 644 | #else |
| 645 | priv->phyaddr = -1; |
| 646 | #endif |
| 647 | |
| 648 | eth_register(dev); |
| 649 | |
| 650 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) |
| 651 | miiphy_register(dev->name, axiemac_miiphy_read, axiemac_miiphy_write); |
| 652 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 653 | priv->bus->reset = axiemac_bus_reset; |
| 654 | #endif |
| 655 | return 1; |
| 656 | } |