Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Michal Simek <monstr@monstr.eu> |
| 4 | * Copyright (C) 2011 PetaLogix |
| 5 | * Copyright (C) 2010 Xilinx, Inc. All rights reserved. |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <config.h> |
| 9 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 13 | #include <net.h> |
| 14 | #include <malloc.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <phy.h> |
| 17 | #include <miiphy.h> |
Siva Durga Prasad Paladugu | 2063362 | 2017-01-06 16:18:50 +0530 | [diff] [blame] | 18 | #include <wait_bit.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 20 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 23 | /* Link setup */ |
| 24 | #define XAE_EMMC_LINKSPEED_MASK 0xC0000000 /* Link speed */ |
| 25 | #define XAE_EMMC_LINKSPD_10 0x00000000 /* Link Speed mask for 10 Mbit */ |
| 26 | #define XAE_EMMC_LINKSPD_100 0x40000000 /* Link Speed mask for 100 Mbit */ |
| 27 | #define XAE_EMMC_LINKSPD_1000 0x80000000 /* Link Speed mask for 1000 Mbit */ |
| 28 | |
| 29 | /* Interrupt Status/Enable/Mask Registers bit definitions */ |
| 30 | #define XAE_INT_RXRJECT_MASK 0x00000008 /* Rx frame rejected */ |
| 31 | #define XAE_INT_MGTRDY_MASK 0x00000080 /* MGT clock Lock */ |
| 32 | |
| 33 | /* Receive Configuration Word 1 (RCW1) Register bit definitions */ |
| 34 | #define XAE_RCW1_RX_MASK 0x10000000 /* Receiver enable */ |
| 35 | |
| 36 | /* Transmitter Configuration (TC) Register bit definitions */ |
| 37 | #define XAE_TC_TX_MASK 0x10000000 /* Transmitter enable */ |
| 38 | |
| 39 | #define XAE_UAW1_UNICASTADDR_MASK 0x0000FFFF |
| 40 | |
| 41 | /* MDIO Management Configuration (MC) Register bit definitions */ |
| 42 | #define XAE_MDIO_MC_MDIOEN_MASK 0x00000040 /* MII management enable*/ |
| 43 | |
| 44 | /* MDIO Management Control Register (MCR) Register bit definitions */ |
| 45 | #define XAE_MDIO_MCR_PHYAD_MASK 0x1F000000 /* Phy Address Mask */ |
| 46 | #define XAE_MDIO_MCR_PHYAD_SHIFT 24 /* Phy Address Shift */ |
| 47 | #define XAE_MDIO_MCR_REGAD_MASK 0x001F0000 /* Reg Address Mask */ |
| 48 | #define XAE_MDIO_MCR_REGAD_SHIFT 16 /* Reg Address Shift */ |
| 49 | #define XAE_MDIO_MCR_OP_READ_MASK 0x00008000 /* Op Code Read Mask */ |
| 50 | #define XAE_MDIO_MCR_OP_WRITE_MASK 0x00004000 /* Op Code Write Mask */ |
| 51 | #define XAE_MDIO_MCR_INITIATE_MASK 0x00000800 /* Ready Mask */ |
| 52 | #define XAE_MDIO_MCR_READY_MASK 0x00000080 /* Ready Mask */ |
| 53 | |
| 54 | #define XAE_MDIO_DIV_DFT 29 /* Default MDIO clock divisor */ |
| 55 | |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 56 | #define XAXIDMA_BD_STS_ACTUAL_LEN_MASK 0x007FFFFF /* Actual len */ |
| 57 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 58 | /* DMA macros */ |
| 59 | /* Bitmasks of XAXIDMA_CR_OFFSET register */ |
| 60 | #define XAXIDMA_CR_RUNSTOP_MASK 0x00000001 /* Start/stop DMA channel */ |
| 61 | #define XAXIDMA_CR_RESET_MASK 0x00000004 /* Reset DMA engine */ |
| 62 | |
| 63 | /* Bitmasks of XAXIDMA_SR_OFFSET register */ |
| 64 | #define XAXIDMA_HALTED_MASK 0x00000001 /* DMA channel halted */ |
| 65 | |
| 66 | /* Bitmask for interrupts */ |
| 67 | #define XAXIDMA_IRQ_IOC_MASK 0x00001000 /* Completion intr */ |
| 68 | #define XAXIDMA_IRQ_DELAY_MASK 0x00002000 /* Delay interrupt */ |
| 69 | #define XAXIDMA_IRQ_ALL_MASK 0x00007000 /* All interrupts */ |
| 70 | |
| 71 | /* Bitmasks of XAXIDMA_BD_CTRL_OFFSET register */ |
| 72 | #define XAXIDMA_BD_CTRL_TXSOF_MASK 0x08000000 /* First tx packet */ |
| 73 | #define XAXIDMA_BD_CTRL_TXEOF_MASK 0x04000000 /* Last tx packet */ |
| 74 | |
| 75 | #define DMAALIGN 128 |
| 76 | |
| 77 | static u8 rxframe[PKTSIZE_ALIGN] __attribute((aligned(DMAALIGN))); |
| 78 | |
| 79 | /* Reflect dma offsets */ |
| 80 | struct axidma_reg { |
| 81 | u32 control; /* DMACR */ |
| 82 | u32 status; /* DMASR */ |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 83 | u32 current; /* CURDESC low 32 bit */ |
| 84 | u32 current_hi; /* CURDESC high 32 bit */ |
| 85 | u32 tail; /* TAILDESC low 32 bit */ |
| 86 | u32 tail_hi; /* TAILDESC high 32 bit */ |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | /* Private driver structures */ |
| 90 | struct axidma_priv { |
| 91 | struct axidma_reg *dmatx; |
| 92 | struct axidma_reg *dmarx; |
| 93 | int phyaddr; |
Michal Simek | 6cb55e7 | 2015-12-09 14:39:42 +0100 | [diff] [blame] | 94 | struct axi_regs *iobase; |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 95 | phy_interface_t interface; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 96 | struct phy_device *phydev; |
| 97 | struct mii_dev *bus; |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 98 | u8 eth_hasnobuf; |
Siva Durga Prasad Paladugu | c3d94f6 | 2019-03-15 17:46:45 +0530 | [diff] [blame] | 99 | int phy_of_handle; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /* BD descriptors */ |
| 103 | struct axidma_bd { |
| 104 | u32 next; /* Next descriptor pointer */ |
| 105 | u32 reserved1; |
| 106 | u32 phys; /* Buffer address */ |
| 107 | u32 reserved2; |
| 108 | u32 reserved3; |
| 109 | u32 reserved4; |
| 110 | u32 cntrl; /* Control */ |
| 111 | u32 status; /* Status */ |
| 112 | u32 app0; |
| 113 | u32 app1; /* TX start << 16 | insert */ |
| 114 | u32 app2; /* TX csum seed */ |
| 115 | u32 app3; |
| 116 | u32 app4; |
| 117 | u32 sw_id_offset; |
| 118 | u32 reserved5; |
| 119 | u32 reserved6; |
| 120 | }; |
| 121 | |
| 122 | /* Static BDs - driver uses only one BD */ |
| 123 | static struct axidma_bd tx_bd __attribute((aligned(DMAALIGN))); |
| 124 | static struct axidma_bd rx_bd __attribute((aligned(DMAALIGN))); |
| 125 | |
| 126 | struct axi_regs { |
| 127 | u32 reserved[3]; |
| 128 | u32 is; /* 0xC: Interrupt status */ |
| 129 | u32 reserved2; |
| 130 | u32 ie; /* 0x14: Interrupt enable */ |
| 131 | u32 reserved3[251]; |
| 132 | u32 rcw1; /* 0x404: Rx Configuration Word 1 */ |
| 133 | u32 tc; /* 0x408: Tx Configuration */ |
| 134 | u32 reserved4; |
| 135 | u32 emmc; /* 0x410: EMAC mode configuration */ |
| 136 | u32 reserved5[59]; |
| 137 | u32 mdio_mc; /* 0x500: MII Management Config */ |
| 138 | u32 mdio_mcr; /* 0x504: MII Management Control */ |
| 139 | u32 mdio_mwd; /* 0x508: MII Management Write Data */ |
| 140 | u32 mdio_mrd; /* 0x50C: MII Management Read Data */ |
| 141 | u32 reserved6[124]; |
| 142 | u32 uaw0; /* 0x700: Unicast address word 0 */ |
| 143 | u32 uaw1; /* 0x704: Unicast address word 1 */ |
| 144 | }; |
| 145 | |
| 146 | /* Use MII register 1 (MII status register) to detect PHY */ |
| 147 | #define PHY_DETECT_REG 1 |
| 148 | |
| 149 | /* |
| 150 | * Mask used to verify certain PHY features (or register contents) |
| 151 | * in the register above: |
| 152 | * 0x1000: 10Mbps full duplex support |
| 153 | * 0x0800: 10Mbps half duplex support |
| 154 | * 0x0008: Auto-negotiation support |
| 155 | */ |
| 156 | #define PHY_DETECT_MASK 0x1808 |
| 157 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 158 | static inline int mdio_wait(struct axi_regs *regs) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 159 | { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 160 | u32 timeout = 200; |
| 161 | |
| 162 | /* Wait till MDIO interface is ready to accept a new transaction. */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 163 | while (timeout && (!(readl(®s->mdio_mcr) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 164 | & XAE_MDIO_MCR_READY_MASK))) { |
| 165 | timeout--; |
| 166 | udelay(1); |
| 167 | } |
| 168 | if (!timeout) { |
| 169 | printf("%s: Timeout\n", __func__); |
| 170 | return 1; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 175 | /** |
| 176 | * axienet_dma_write - Memory mapped Axi DMA register Buffer Descriptor write. |
| 177 | * @bd: pointer to BD descriptor structure |
| 178 | * @desc: Address offset of DMA descriptors |
| 179 | * |
| 180 | * This function writes the value into the corresponding Axi DMA register. |
| 181 | */ |
| 182 | static inline void axienet_dma_write(struct axidma_bd *bd, u32 *desc) |
| 183 | { |
| 184 | #if defined(CONFIG_PHYS_64BIT) |
| 185 | writeq(bd, desc); |
| 186 | #else |
| 187 | writel((u32)bd, desc); |
| 188 | #endif |
| 189 | } |
| 190 | |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame] | 191 | static u32 phyread(struct axidma_priv *priv, u32 phyaddress, u32 registernum, |
| 192 | u16 *val) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 193 | { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame] | 194 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 195 | u32 mdioctrlreg = 0; |
| 196 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 197 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 198 | return 1; |
| 199 | |
| 200 | mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) & |
| 201 | XAE_MDIO_MCR_PHYAD_MASK) | |
| 202 | ((registernum << XAE_MDIO_MCR_REGAD_SHIFT) |
| 203 | & XAE_MDIO_MCR_REGAD_MASK) | |
| 204 | XAE_MDIO_MCR_INITIATE_MASK | |
| 205 | XAE_MDIO_MCR_OP_READ_MASK; |
| 206 | |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 207 | writel(mdioctrlreg, ®s->mdio_mcr); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 208 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 209 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 210 | return 1; |
| 211 | |
| 212 | /* Read data */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 213 | *val = readl(®s->mdio_mrd); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame] | 217 | static u32 phywrite(struct axidma_priv *priv, u32 phyaddress, u32 registernum, |
| 218 | u32 data) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 219 | { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame] | 220 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 221 | u32 mdioctrlreg = 0; |
| 222 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 223 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 224 | return 1; |
| 225 | |
| 226 | mdioctrlreg = ((phyaddress << XAE_MDIO_MCR_PHYAD_SHIFT) & |
| 227 | XAE_MDIO_MCR_PHYAD_MASK) | |
| 228 | ((registernum << XAE_MDIO_MCR_REGAD_SHIFT) |
| 229 | & XAE_MDIO_MCR_REGAD_MASK) | |
| 230 | XAE_MDIO_MCR_INITIATE_MASK | |
| 231 | XAE_MDIO_MCR_OP_WRITE_MASK; |
| 232 | |
| 233 | /* Write data */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 234 | writel(data, ®s->mdio_mwd); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 235 | |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 236 | writel(mdioctrlreg, ®s->mdio_mcr); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 237 | |
Michal Simek | f522187 | 2015-12-09 14:36:31 +0100 | [diff] [blame] | 238 | if (mdio_wait(regs)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 239 | return 1; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 244 | static int axiemac_phy_init(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 245 | { |
| 246 | u16 phyreg; |
Patrick van Gelder | 70ab943 | 2020-06-03 14:18:04 +0200 | [diff] [blame] | 247 | int i; |
| 248 | u32 ret; |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 249 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6cb55e7 | 2015-12-09 14:39:42 +0100 | [diff] [blame] | 250 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 251 | struct phy_device *phydev; |
| 252 | |
| 253 | u32 supported = SUPPORTED_10baseT_Half | |
| 254 | SUPPORTED_10baseT_Full | |
| 255 | SUPPORTED_100baseT_Half | |
| 256 | SUPPORTED_100baseT_Full | |
| 257 | SUPPORTED_1000baseT_Half | |
| 258 | SUPPORTED_1000baseT_Full; |
| 259 | |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 260 | /* Set default MDIO divisor */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 261 | writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc); |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 262 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 263 | if (priv->phyaddr == -1) { |
| 264 | /* Detect the PHY address */ |
| 265 | for (i = 31; i >= 0; i--) { |
Michal Simek | 41beca1 | 2015-12-09 14:44:38 +0100 | [diff] [blame] | 266 | ret = phyread(priv, i, PHY_DETECT_REG, &phyreg); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 267 | if (!ret && (phyreg != 0xFFFF) && |
| 268 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 269 | /* Found a valid PHY address */ |
| 270 | priv->phyaddr = i; |
| 271 | debug("axiemac: Found valid phy address, %x\n", |
Michal Simek | 2f1e065 | 2015-12-09 10:54:53 +0100 | [diff] [blame] | 272 | i); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Interface - look at tsec */ |
Siva Durga Prasad Paladugu | 4934851 | 2016-02-21 15:46:14 +0530 | [diff] [blame] | 279 | phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 280 | |
| 281 | phydev->supported &= supported; |
| 282 | phydev->advertising = phydev->supported; |
| 283 | priv->phydev = phydev; |
Siva Durga Prasad Paladugu | c3d94f6 | 2019-03-15 17:46:45 +0530 | [diff] [blame] | 284 | if (priv->phy_of_handle) |
| 285 | priv->phydev->node = offset_to_ofnode(priv->phy_of_handle); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 286 | phy_config(phydev); |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* Setting axi emac and phy to proper setting */ |
| 292 | static int setup_phy(struct udevice *dev) |
| 293 | { |
Siva Durga Prasad Paladugu | a1c6ed8 | 2016-02-21 15:46:15 +0530 | [diff] [blame] | 294 | u16 temp; |
| 295 | u32 speed, emmc_reg, ret; |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 296 | struct axidma_priv *priv = dev_get_priv(dev); |
| 297 | struct axi_regs *regs = priv->iobase; |
| 298 | struct phy_device *phydev = priv->phydev; |
| 299 | |
Siva Durga Prasad Paladugu | a1c6ed8 | 2016-02-21 15:46:15 +0530 | [diff] [blame] | 300 | if (priv->interface == PHY_INTERFACE_MODE_SGMII) { |
| 301 | /* |
| 302 | * In SGMII cases the isolate bit might set |
| 303 | * after DMA and ethernet resets and hence |
| 304 | * check and clear if set. |
| 305 | */ |
| 306 | ret = phyread(priv, priv->phyaddr, MII_BMCR, &temp); |
| 307 | if (ret) |
| 308 | return 0; |
| 309 | if (temp & BMCR_ISOLATE) { |
| 310 | temp &= ~BMCR_ISOLATE; |
| 311 | ret = phywrite(priv, priv->phyaddr, MII_BMCR, temp); |
| 312 | if (ret) |
| 313 | return 0; |
| 314 | } |
| 315 | } |
| 316 | |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 317 | if (phy_startup(phydev)) { |
| 318 | printf("axiemac: could not initialize PHY %s\n", |
| 319 | phydev->dev->name); |
| 320 | return 0; |
| 321 | } |
Michal Simek | 5848f13 | 2013-11-21 16:15:51 +0100 | [diff] [blame] | 322 | if (!phydev->link) { |
| 323 | printf("%s: No link.\n", phydev->dev->name); |
| 324 | return 0; |
| 325 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 326 | |
| 327 | switch (phydev->speed) { |
| 328 | case 1000: |
| 329 | speed = XAE_EMMC_LINKSPD_1000; |
| 330 | break; |
| 331 | case 100: |
| 332 | speed = XAE_EMMC_LINKSPD_100; |
| 333 | break; |
| 334 | case 10: |
| 335 | speed = XAE_EMMC_LINKSPD_10; |
| 336 | break; |
| 337 | default: |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | /* Setup the emac for the phy speed */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 342 | emmc_reg = readl(®s->emmc); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 343 | emmc_reg &= ~XAE_EMMC_LINKSPEED_MASK; |
| 344 | emmc_reg |= speed; |
| 345 | |
| 346 | /* Write new speed setting out to Axi Ethernet */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 347 | writel(emmc_reg, ®s->emmc); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 348 | |
| 349 | /* |
| 350 | * Setting the operating speed of the MAC needs a delay. There |
| 351 | * doesn't seem to be register to poll, so please consider this |
| 352 | * during your application design. |
| 353 | */ |
| 354 | udelay(1); |
| 355 | |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | /* STOP DMA transfers */ |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 360 | static void axiemac_stop(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 361 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 362 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 363 | u32 temp; |
| 364 | |
| 365 | /* Stop the hardware */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 366 | temp = readl(&priv->dmatx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 367 | temp &= ~XAXIDMA_CR_RUNSTOP_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 368 | writel(temp, &priv->dmatx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 369 | |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 370 | temp = readl(&priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 371 | temp &= ~XAXIDMA_CR_RUNSTOP_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 372 | writel(temp, &priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 373 | |
| 374 | debug("axiemac: Halted\n"); |
| 375 | } |
| 376 | |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 377 | static int axi_ethernet_init(struct axidma_priv *priv) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 378 | { |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 379 | struct axi_regs *regs = priv->iobase; |
Siva Durga Prasad Paladugu | 2063362 | 2017-01-06 16:18:50 +0530 | [diff] [blame] | 380 | int err; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 381 | |
| 382 | /* |
| 383 | * Check the status of the MgtRdy bit in the interrupt status |
| 384 | * registers. This must be done to allow the MGT clock to become stable |
| 385 | * for the Sgmii and 1000BaseX PHY interfaces. No other register reads |
| 386 | * will be valid until this bit is valid. |
| 387 | * The bit is always a 1 for all other PHY interfaces. |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 388 | * Interrupt status and enable registers are not available in non |
| 389 | * processor mode and hence bypass in this mode |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 390 | */ |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 391 | if (!priv->eth_hasnobuf) { |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 392 | err = wait_for_bit_le32(®s->is, XAE_INT_MGTRDY_MASK, |
| 393 | true, 200, false); |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 394 | if (err) { |
| 395 | printf("%s: Timeout\n", __func__); |
| 396 | return 1; |
| 397 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 398 | |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 399 | /* |
| 400 | * Stop the device and reset HW |
| 401 | * Disable interrupts |
| 402 | */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 403 | writel(0, ®s->ie); |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 404 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 405 | |
| 406 | /* Disable the receiver */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 407 | writel(readl(®s->rcw1) & ~XAE_RCW1_RX_MASK, ®s->rcw1); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * Stopping the receiver in mid-packet causes a dropped packet |
| 411 | * indication from HW. Clear it. |
| 412 | */ |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 413 | if (!priv->eth_hasnobuf) { |
| 414 | /* Set the interrupt status register to clear the interrupt */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 415 | writel(XAE_INT_RXRJECT_MASK, ®s->is); |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 416 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 417 | |
| 418 | /* Setup HW */ |
| 419 | /* Set default MDIO divisor */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 420 | writel(XAE_MDIO_DIV_DFT | XAE_MDIO_MC_MDIOEN_MASK, ®s->mdio_mc); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 421 | |
| 422 | debug("axiemac: InitHw done\n"); |
| 423 | return 0; |
| 424 | } |
| 425 | |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 426 | static int axiemac_write_hwaddr(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 427 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 428 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 429 | struct axidma_priv *priv = dev_get_priv(dev); |
| 430 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 431 | |
| 432 | /* Set the MAC address */ |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 433 | int val = ((pdata->enetaddr[3] << 24) | (pdata->enetaddr[2] << 16) | |
| 434 | (pdata->enetaddr[1] << 8) | (pdata->enetaddr[0])); |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 435 | writel(val, ®s->uaw0); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 436 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 437 | val = (pdata->enetaddr[5] << 8) | pdata->enetaddr[4]; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 438 | val |= readl(®s->uaw1) & ~XAE_UAW1_UNICASTADDR_MASK; |
| 439 | writel(val, ®s->uaw1); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /* Reset DMA engine */ |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 444 | static void axi_dma_init(struct axidma_priv *priv) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 445 | { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 446 | u32 timeout = 500; |
| 447 | |
| 448 | /* Reset the engine so the hardware starts from a known state */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 449 | writel(XAXIDMA_CR_RESET_MASK, &priv->dmatx->control); |
| 450 | writel(XAXIDMA_CR_RESET_MASK, &priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 451 | |
| 452 | /* At the initialization time, hardware should finish reset quickly */ |
| 453 | while (timeout--) { |
| 454 | /* Check transmit/receive channel */ |
| 455 | /* Reset is done when the reset bit is low */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 456 | if (!((readl(&priv->dmatx->control) | |
| 457 | readl(&priv->dmarx->control)) |
Michal Simek | 5aa4539 | 2015-10-28 11:00:47 +0100 | [diff] [blame] | 458 | & XAXIDMA_CR_RESET_MASK)) { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | } |
| 462 | if (!timeout) |
| 463 | printf("%s: Timeout\n", __func__); |
| 464 | } |
| 465 | |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 466 | static int axiemac_start(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 467 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 468 | struct axidma_priv *priv = dev_get_priv(dev); |
| 469 | struct axi_regs *regs = priv->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 470 | u32 temp; |
| 471 | |
| 472 | debug("axiemac: Init started\n"); |
| 473 | /* |
| 474 | * Initialize AXIDMA engine. AXIDMA engine must be initialized before |
| 475 | * AxiEthernet. During AXIDMA engine initialization, AXIDMA hardware is |
| 476 | * reset, and since AXIDMA reset line is connected to AxiEthernet, this |
| 477 | * would ensure a reset of AxiEthernet. |
| 478 | */ |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 479 | axi_dma_init(priv); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 480 | |
| 481 | /* Initialize AxiEthernet hardware. */ |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 482 | if (axi_ethernet_init(priv)) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 483 | return -1; |
| 484 | |
| 485 | /* Disable all RX interrupts before RxBD space setup */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 486 | temp = readl(&priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 487 | temp &= ~XAXIDMA_IRQ_ALL_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 488 | writel(temp, &priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 489 | |
| 490 | /* Start DMA RX channel. Now it's ready to receive data.*/ |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 491 | axienet_dma_write(&rx_bd, &priv->dmarx->current); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 492 | |
| 493 | /* Setup the BD. */ |
| 494 | memset(&rx_bd, 0, sizeof(rx_bd)); |
| 495 | rx_bd.next = (u32)&rx_bd; |
| 496 | rx_bd.phys = (u32)&rxframe; |
| 497 | rx_bd.cntrl = sizeof(rxframe); |
| 498 | /* Flush the last BD so DMA core could see the updates */ |
| 499 | flush_cache((u32)&rx_bd, sizeof(rx_bd)); |
| 500 | |
| 501 | /* It is necessary to flush rxframe because if you don't do it |
| 502 | * then cache can contain uninitialized data */ |
| 503 | flush_cache((u32)&rxframe, sizeof(rxframe)); |
| 504 | |
| 505 | /* Start the hardware */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 506 | temp = readl(&priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 507 | temp |= XAXIDMA_CR_RUNSTOP_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 508 | writel(temp, &priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 509 | |
| 510 | /* Rx BD is ready - start */ |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 511 | axienet_dma_write(&rx_bd, &priv->dmarx->tail); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 512 | |
| 513 | /* Enable TX */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 514 | writel(XAE_TC_TX_MASK, ®s->tc); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 515 | /* Enable RX */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 516 | writel(XAE_RCW1_RX_MASK, ®s->rcw1); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 517 | |
| 518 | /* PHY setup */ |
| 519 | if (!setup_phy(dev)) { |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 520 | axiemac_stop(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | debug("axiemac: Init complete\n"); |
| 525 | return 0; |
| 526 | } |
| 527 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 528 | static int axiemac_send(struct udevice *dev, void *ptr, int len) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 529 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 530 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 531 | u32 timeout; |
| 532 | |
| 533 | if (len > PKTSIZE_ALIGN) |
| 534 | len = PKTSIZE_ALIGN; |
| 535 | |
| 536 | /* Flush packet to main memory to be trasfered by DMA */ |
| 537 | flush_cache((u32)ptr, len); |
| 538 | |
| 539 | /* Setup Tx BD */ |
| 540 | memset(&tx_bd, 0, sizeof(tx_bd)); |
| 541 | /* At the end of the ring, link the last BD back to the top */ |
| 542 | tx_bd.next = (u32)&tx_bd; |
| 543 | tx_bd.phys = (u32)ptr; |
| 544 | /* Save len */ |
| 545 | tx_bd.cntrl = len | XAXIDMA_BD_CTRL_TXSOF_MASK | |
| 546 | XAXIDMA_BD_CTRL_TXEOF_MASK; |
| 547 | |
| 548 | /* Flush the last BD so DMA core could see the updates */ |
| 549 | flush_cache((u32)&tx_bd, sizeof(tx_bd)); |
| 550 | |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 551 | if (readl(&priv->dmatx->status) & XAXIDMA_HALTED_MASK) { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 552 | u32 temp; |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 553 | axienet_dma_write(&tx_bd, &priv->dmatx->current); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 554 | /* Start the hardware */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 555 | temp = readl(&priv->dmatx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 556 | temp |= XAXIDMA_CR_RUNSTOP_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 557 | writel(temp, &priv->dmatx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* Start transfer */ |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 561 | axienet_dma_write(&tx_bd, &priv->dmatx->tail); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 562 | |
| 563 | /* Wait for transmission to complete */ |
| 564 | debug("axiemac: Waiting for tx to be done\n"); |
| 565 | timeout = 200; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 566 | while (timeout && (!(readl(&priv->dmatx->status) & |
Michal Simek | 5aa4539 | 2015-10-28 11:00:47 +0100 | [diff] [blame] | 567 | (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK)))) { |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 568 | timeout--; |
| 569 | udelay(1); |
| 570 | } |
| 571 | if (!timeout) { |
| 572 | printf("%s: Timeout\n", __func__); |
| 573 | return 1; |
| 574 | } |
| 575 | |
| 576 | debug("axiemac: Sending complete\n"); |
| 577 | return 0; |
| 578 | } |
| 579 | |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 580 | static int isrxready(struct axidma_priv *priv) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 581 | { |
| 582 | u32 status; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 583 | |
| 584 | /* Read pending interrupts */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 585 | status = readl(&priv->dmarx->status); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 586 | |
| 587 | /* Acknowledge pending interrupts */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 588 | writel(status & XAXIDMA_IRQ_ALL_MASK, &priv->dmarx->status); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * If Reception done interrupt is asserted, call RX call back function |
| 592 | * to handle the processed BDs and then raise the according flag. |
| 593 | */ |
| 594 | if ((status & (XAXIDMA_IRQ_DELAY_MASK | XAXIDMA_IRQ_IOC_MASK))) |
| 595 | return 1; |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 600 | static int axiemac_recv(struct udevice *dev, int flags, uchar **packetp) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 601 | { |
| 602 | u32 length; |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 603 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 604 | u32 temp; |
| 605 | |
| 606 | /* Wait for an incoming packet */ |
Michal Simek | 638c0ef | 2015-12-09 14:53:51 +0100 | [diff] [blame] | 607 | if (!isrxready(priv)) |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 608 | return -1; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 609 | |
| 610 | debug("axiemac: RX data ready\n"); |
| 611 | |
| 612 | /* Disable IRQ for a moment till packet is handled */ |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 613 | temp = readl(&priv->dmarx->control); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 614 | temp &= ~XAXIDMA_IRQ_ALL_MASK; |
Siva Durga Prasad Paladugu | d227c9a | 2017-11-23 12:23:12 +0530 | [diff] [blame] | 615 | writel(temp, &priv->dmarx->control); |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 616 | if (!priv->eth_hasnobuf) |
| 617 | length = rx_bd.app4 & 0xFFFF; /* max length mask */ |
| 618 | else |
| 619 | length = rx_bd.status & XAXIDMA_BD_STS_ACTUAL_LEN_MASK; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 620 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 621 | #ifdef DEBUG |
| 622 | print_buffer(&rxframe, &rxframe[0], 1, length, 16); |
| 623 | #endif |
Michal Simek | 0305be8 | 2015-12-09 14:13:23 +0100 | [diff] [blame] | 624 | |
| 625 | *packetp = rxframe; |
| 626 | return length; |
| 627 | } |
| 628 | |
| 629 | static int axiemac_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 630 | { |
| 631 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 632 | |
| 633 | #ifdef DEBUG |
| 634 | /* It is useful to clear buffer to be sure that it is consistent */ |
| 635 | memset(rxframe, 0, sizeof(rxframe)); |
| 636 | #endif |
| 637 | /* Setup RxBD */ |
| 638 | /* Clear the whole buffer and setup it again - all flags are cleared */ |
| 639 | memset(&rx_bd, 0, sizeof(rx_bd)); |
| 640 | rx_bd.next = (u32)&rx_bd; |
| 641 | rx_bd.phys = (u32)&rxframe; |
| 642 | rx_bd.cntrl = sizeof(rxframe); |
| 643 | |
| 644 | /* Write bd to HW */ |
| 645 | flush_cache((u32)&rx_bd, sizeof(rx_bd)); |
| 646 | |
| 647 | /* It is necessary to flush rxframe because if you don't do it |
| 648 | * then cache will contain previous packet */ |
| 649 | flush_cache((u32)&rxframe, sizeof(rxframe)); |
| 650 | |
| 651 | /* Rx BD is ready - start again */ |
Vipul Kumar | 3b06a0f | 2018-01-23 14:52:35 +0530 | [diff] [blame] | 652 | axienet_dma_write(&rx_bd, &priv->dmarx->tail); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 653 | |
| 654 | debug("axiemac: RX completed, framelength = %d\n", length); |
| 655 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 656 | return 0; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 659 | static int axiemac_miiphy_read(struct mii_dev *bus, int addr, |
| 660 | int devad, int reg) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 661 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 662 | int ret; |
| 663 | u16 value; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 664 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 665 | ret = phyread(bus->priv, addr, reg, &value); |
| 666 | debug("axiemac: Read MII 0x%x, 0x%x, 0x%x, %d\n", addr, reg, |
| 667 | value, ret); |
| 668 | return value; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 671 | static int axiemac_miiphy_write(struct mii_dev *bus, int addr, int devad, |
| 672 | int reg, u16 value) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 673 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 674 | debug("axiemac: Write MII 0x%x, 0x%x, 0x%x\n", addr, reg, value); |
| 675 | return phywrite(bus->priv, addr, reg, value); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 678 | static int axi_emac_probe(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 679 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 680 | struct axidma_priv *priv = dev_get_priv(dev); |
| 681 | int ret; |
| 682 | |
| 683 | priv->bus = mdio_alloc(); |
| 684 | priv->bus->read = axiemac_miiphy_read; |
| 685 | priv->bus->write = axiemac_miiphy_write; |
| 686 | priv->bus->priv = priv; |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 687 | |
Michal Simek | e4dab43 | 2016-12-08 10:25:44 +0100 | [diff] [blame] | 688 | ret = mdio_register_seq(priv->bus, dev->seq); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 689 | if (ret) |
| 690 | return ret; |
| 691 | |
Michal Simek | 0b13ee2 | 2015-12-08 16:10:05 +0100 | [diff] [blame] | 692 | axiemac_phy_init(dev); |
| 693 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 697 | static int axi_emac_remove(struct udevice *dev) |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 698 | { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 699 | struct axidma_priv *priv = dev_get_priv(dev); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 700 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 701 | free(priv->phydev); |
| 702 | mdio_unregister(priv->bus); |
| 703 | mdio_free(priv->bus); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 704 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static const struct eth_ops axi_emac_ops = { |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 709 | .start = axiemac_start, |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 710 | .send = axiemac_send, |
| 711 | .recv = axiemac_recv, |
Michal Simek | 0305be8 | 2015-12-09 14:13:23 +0100 | [diff] [blame] | 712 | .free_pkt = axiemac_free_pkt, |
Michal Simek | 8fbf79f | 2015-12-16 09:18:12 +0100 | [diff] [blame] | 713 | .stop = axiemac_stop, |
| 714 | .write_hwaddr = axiemac_write_hwaddr, |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 715 | }; |
| 716 | |
| 717 | static int axi_emac_ofdata_to_platdata(struct udevice *dev) |
| 718 | { |
| 719 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 720 | struct axidma_priv *priv = dev_get_priv(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 721 | int node = dev_of_offset(dev); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 722 | int offset = 0; |
| 723 | const char *phy_mode; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 724 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 725 | pdata->iobase = dev_read_addr(dev); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 726 | priv->iobase = (struct axi_regs *)pdata->iobase; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 727 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 728 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 729 | "axistream-connected"); |
| 730 | if (offset <= 0) { |
| 731 | printf("%s: axistream is not found\n", __func__); |
| 732 | return -EINVAL; |
| 733 | } |
Siva Durga Prasad Paladugu | 6372e3f | 2017-06-22 11:14:55 +0530 | [diff] [blame] | 734 | priv->dmatx = (struct axidma_reg *)fdtdec_get_addr(gd->fdt_blob, |
| 735 | offset, "reg"); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 736 | if (!priv->dmatx) { |
| 737 | printf("%s: axi_dma register space not found\n", __func__); |
| 738 | return -EINVAL; |
| 739 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 740 | /* RX channel offset is 0x30 */ |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 741 | priv->dmarx = (struct axidma_reg *)((u32)priv->dmatx + 0x30); |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 742 | |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 743 | priv->phyaddr = -1; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 744 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 745 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle"); |
Siva Durga Prasad Paladugu | c3d94f6 | 2019-03-15 17:46:45 +0530 | [diff] [blame] | 746 | if (offset > 0) { |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 747 | priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1); |
Siva Durga Prasad Paladugu | c3d94f6 | 2019-03-15 17:46:45 +0530 | [diff] [blame] | 748 | priv->phy_of_handle = offset; |
| 749 | } |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 750 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 751 | phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 752 | if (phy_mode) |
| 753 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 754 | if (pdata->phy_interface == -1) { |
Michal Simek | 9dbfc0d | 2016-02-08 13:54:05 +0100 | [diff] [blame] | 755 | printf("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 756 | return -EINVAL; |
| 757 | } |
| 758 | priv->interface = pdata->phy_interface; |
| 759 | |
Siva Durga Prasad Paladugu | f802603 | 2017-01-06 16:27:15 +0530 | [diff] [blame] | 760 | priv->eth_hasnobuf = fdtdec_get_bool(gd->fdt_blob, node, |
| 761 | "xlnx,eth-hasnobuf"); |
| 762 | |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 763 | printf("AXI EMAC: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase, |
| 764 | priv->phyaddr, phy_string_for_interface(priv->interface)); |
| 765 | |
| 766 | return 0; |
Michal Simek | 6fc7c45 | 2011-10-06 20:35:35 +0000 | [diff] [blame] | 767 | } |
Michal Simek | 682baac | 2015-12-08 15:44:41 +0100 | [diff] [blame] | 768 | |
| 769 | static const struct udevice_id axi_emac_ids[] = { |
| 770 | { .compatible = "xlnx,axi-ethernet-1.00.a" }, |
| 771 | { } |
| 772 | }; |
| 773 | |
| 774 | U_BOOT_DRIVER(axi_emac) = { |
| 775 | .name = "axi_emac", |
| 776 | .id = UCLASS_ETH, |
| 777 | .of_match = axi_emac_ids, |
| 778 | .ofdata_to_platdata = axi_emac_ofdata_to_platdata, |
| 779 | .probe = axi_emac_probe, |
| 780 | .remove = axi_emac_remove, |
| 781 | .ops = &axi_emac_ops, |
| 782 | .priv_auto_alloc_size = sizeof(struct axidma_priv), |
| 783 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 784 | }; |