Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2011 Michal Simek |
| 3 | * |
| 4 | * Michal SIMEK <monstr@monstr.eu> |
| 5 | * |
| 6 | * Based on Xilinx gmac driver: |
| 7 | * (C) Copyright 2011 Xilinx |
| 8 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <net.h> |
Michal Simek | 2fd2489 | 2014-04-25 14:17:38 +0200 | [diff] [blame] | 14 | #include <netdev.h> |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 15 | #include <config.h> |
Michal Simek | f88a686 | 2014-02-24 11:16:30 +0100 | [diff] [blame] | 16 | #include <fdtdec.h> |
| 17 | #include <libfdt.h> |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <phy.h> |
| 21 | #include <miiphy.h> |
| 22 | #include <watchdog.h> |
Siva Durga Prasad Paladugu | 96f4f14 | 2014-12-06 12:57:53 +0530 | [diff] [blame] | 23 | #include <asm/system.h> |
David Andrey | 01fbf31 | 2013-04-05 17:24:24 +0200 | [diff] [blame] | 24 | #include <asm/arch/hardware.h> |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 25 | #include <asm/arch/sys_proto.h> |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 26 | |
| 27 | #if !defined(CONFIG_PHYLIB) |
| 28 | # error XILINX_GEM_ETHERNET requires PHYLIB |
| 29 | #endif |
| 30 | |
| 31 | /* Bit/mask specification */ |
| 32 | #define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */ |
| 33 | #define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */ |
| 34 | #define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */ |
| 35 | #define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */ |
| 36 | #define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */ |
| 37 | |
| 38 | #define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */ |
| 39 | #define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */ |
| 40 | #define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */ |
| 41 | |
| 42 | #define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */ |
| 43 | #define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */ |
| 44 | #define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */ |
| 45 | |
| 46 | /* Wrap bit, last descriptor */ |
| 47 | #define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000 |
| 48 | #define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */ |
| 49 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 50 | #define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */ |
| 51 | #define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */ |
| 52 | #define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */ |
| 53 | #define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */ |
| 54 | |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 55 | #define ZYNQ_GEM_NWCFG_SPEED100 0x000000001 /* 100 Mbps operation */ |
| 56 | #define ZYNQ_GEM_NWCFG_SPEED1000 0x000000400 /* 1Gbps operation */ |
| 57 | #define ZYNQ_GEM_NWCFG_FDEN 0x000000002 /* Full Duplex mode */ |
| 58 | #define ZYNQ_GEM_NWCFG_FSREM 0x000020000 /* FCS removal */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 59 | #define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x000080000 /* Div pclk by 32, 80MHz */ |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 60 | #define ZYNQ_GEM_NWCFG_MDCCLKDIV2 0x0000c0000 /* Div pclk by 48, 120MHz */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 61 | |
Siva Durga Prasad Paladugu | 8a584c8 | 2014-07-08 15:31:03 +0530 | [diff] [blame] | 62 | #ifdef CONFIG_ARM64 |
| 63 | # define ZYNQ_GEM_DBUS_WIDTH (1 << 21) /* 64 bit bus */ |
| 64 | #else |
| 65 | # define ZYNQ_GEM_DBUS_WIDTH (0 << 21) /* 32 bit bus */ |
| 66 | #endif |
| 67 | |
| 68 | #define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_DBUS_WIDTH | \ |
| 69 | ZYNQ_GEM_NWCFG_FDEN | \ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 70 | ZYNQ_GEM_NWCFG_FSREM | \ |
| 71 | ZYNQ_GEM_NWCFG_MDCCLKDIV) |
| 72 | |
| 73 | #define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */ |
| 74 | |
| 75 | #define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */ |
| 76 | /* Use full configured addressable space (8 Kb) */ |
| 77 | #define ZYNQ_GEM_DMACR_RXSIZE 0x00000300 |
| 78 | /* Use full configured addressable space (4 Kb) */ |
| 79 | #define ZYNQ_GEM_DMACR_TXSIZE 0x00000400 |
| 80 | /* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */ |
| 81 | #define ZYNQ_GEM_DMACR_RXBUF 0x00180000 |
| 82 | |
| 83 | #define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \ |
| 84 | ZYNQ_GEM_DMACR_RXSIZE | \ |
| 85 | ZYNQ_GEM_DMACR_TXSIZE | \ |
| 86 | ZYNQ_GEM_DMACR_RXBUF) |
| 87 | |
Michal Simek | f97d7e8 | 2013-04-22 14:41:09 +0200 | [diff] [blame] | 88 | /* Use MII register 1 (MII status register) to detect PHY */ |
| 89 | #define PHY_DETECT_REG 1 |
| 90 | |
| 91 | /* Mask used to verify certain PHY features (or register contents) |
| 92 | * in the register above: |
| 93 | * 0x1000: 10Mbps full duplex support |
| 94 | * 0x0800: 10Mbps half duplex support |
| 95 | * 0x0008: Auto-negotiation support |
| 96 | */ |
| 97 | #define PHY_DETECT_MASK 0x1808 |
| 98 | |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 99 | /* TX BD status masks */ |
| 100 | #define ZYNQ_GEM_TXBUF_FRMLEN_MASK 0x000007ff |
| 101 | #define ZYNQ_GEM_TXBUF_EXHAUSTED 0x08000000 |
| 102 | #define ZYNQ_GEM_TXBUF_UNDERRUN 0x10000000 |
| 103 | |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 104 | /* Clock frequencies for different speeds */ |
| 105 | #define ZYNQ_GEM_FREQUENCY_10 2500000UL |
| 106 | #define ZYNQ_GEM_FREQUENCY_100 25000000UL |
| 107 | #define ZYNQ_GEM_FREQUENCY_1000 125000000UL |
| 108 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 109 | /* Device registers */ |
| 110 | struct zynq_gem_regs { |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 111 | u32 nwctrl; /* 0x0 - Network Control reg */ |
| 112 | u32 nwcfg; /* 0x4 - Network Config reg */ |
| 113 | u32 nwsr; /* 0x8 - Network Status reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 114 | u32 reserved1; |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 115 | u32 dmacr; /* 0x10 - DMA Control reg */ |
| 116 | u32 txsr; /* 0x14 - TX Status reg */ |
| 117 | u32 rxqbase; /* 0x18 - RX Q Base address reg */ |
| 118 | u32 txqbase; /* 0x1c - TX Q Base address reg */ |
| 119 | u32 rxsr; /* 0x20 - RX Status reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 120 | u32 reserved2[2]; |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 121 | u32 idr; /* 0x2c - Interrupt Disable reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 122 | u32 reserved3; |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 123 | u32 phymntnc; /* 0x34 - Phy Maintaince reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 124 | u32 reserved4[18]; |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 125 | u32 hashl; /* 0x80 - Hash Low address reg */ |
| 126 | u32 hashh; /* 0x84 - Hash High address reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 127 | #define LADDR_LOW 0 |
| 128 | #define LADDR_HIGH 1 |
Michal Simek | 97a51a0 | 2015-10-05 11:49:43 +0200 | [diff] [blame] | 129 | u32 laddr[4][LADDR_HIGH + 1]; /* 0x8c - Specific1 addr low/high reg */ |
| 130 | u32 match[4]; /* 0xa8 - Type ID1 Match reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 131 | u32 reserved6[18]; |
Michal Simek | 0ebf404 | 2015-10-05 12:49:48 +0200 | [diff] [blame] | 132 | #define STAT_SIZE 44 |
| 133 | u32 stat[STAT_SIZE]; /* 0x100 - Octects transmitted Low reg */ |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | /* BD descriptors */ |
| 137 | struct emac_bd { |
| 138 | u32 addr; /* Next descriptor pointer */ |
| 139 | u32 status; |
| 140 | }; |
| 141 | |
Siva Durga Prasad Paladugu | eda9d30 | 2015-04-15 12:15:01 +0530 | [diff] [blame] | 142 | #define RX_BUF 32 |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 143 | /* Page table entries are set to 1MB, or multiples of 1MB |
| 144 | * (not < 1MB). driver uses less bd's so use 1MB bdspace. |
| 145 | */ |
| 146 | #define BD_SPACE 0x100000 |
| 147 | /* BD separation space */ |
Michal Simek | ff47587 | 2015-08-17 09:45:53 +0200 | [diff] [blame^] | 148 | #define BD_SEPRN_SPACE (RX_BUF * sizeof(struct emac_bd)) |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 149 | |
| 150 | /* Initialized, rxbd_current, rx_first_buf must be 0 after init */ |
| 151 | struct zynq_gem_priv { |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 152 | struct emac_bd *tx_bd; |
| 153 | struct emac_bd *rx_bd; |
| 154 | char *rxbuffers; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 155 | u32 rxbd_current; |
| 156 | u32 rx_first_buf; |
| 157 | int phyaddr; |
David Andrey | 01fbf31 | 2013-04-05 17:24:24 +0200 | [diff] [blame] | 158 | u32 emio; |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 159 | int init; |
Michal Simek | 16ce6de | 2015-10-07 16:42:56 +0200 | [diff] [blame] | 160 | phy_interface_t interface; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 161 | struct phy_device *phydev; |
| 162 | struct mii_dev *bus; |
| 163 | }; |
| 164 | |
| 165 | static inline int mdio_wait(struct eth_device *dev) |
| 166 | { |
| 167 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
Michal Simek | 4c8b7bf | 2012-10-16 17:37:11 +0200 | [diff] [blame] | 168 | u32 timeout = 20000; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 169 | |
| 170 | /* Wait till MDIO interface is ready to accept a new transaction. */ |
| 171 | while (--timeout) { |
| 172 | if (readl(®s->nwsr) & ZYNQ_GEM_NWSR_MDIOIDLE_MASK) |
| 173 | break; |
| 174 | WATCHDOG_RESET(); |
| 175 | } |
| 176 | |
| 177 | if (!timeout) { |
| 178 | printf("%s: Timeout\n", __func__); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static u32 phy_setup_op(struct eth_device *dev, u32 phy_addr, u32 regnum, |
| 186 | u32 op, u16 *data) |
| 187 | { |
| 188 | u32 mgtcr; |
| 189 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
| 190 | |
| 191 | if (mdio_wait(dev)) |
| 192 | return 1; |
| 193 | |
| 194 | /* Construct mgtcr mask for the operation */ |
| 195 | mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op | |
| 196 | (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) | |
| 197 | (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data; |
| 198 | |
| 199 | /* Write mgtcr and wait for completion */ |
| 200 | writel(mgtcr, ®s->phymntnc); |
| 201 | |
| 202 | if (mdio_wait(dev)) |
| 203 | return 1; |
| 204 | |
| 205 | if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK) |
| 206 | *data = readl(®s->phymntnc); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static u32 phyread(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 *val) |
| 212 | { |
Michal Simek | 198e9a4 | 2015-10-07 16:34:51 +0200 | [diff] [blame] | 213 | u32 ret; |
| 214 | |
| 215 | ret = phy_setup_op(dev, phy_addr, regnum, |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 216 | ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val); |
Michal Simek | 198e9a4 | 2015-10-07 16:34:51 +0200 | [diff] [blame] | 217 | |
| 218 | if (!ret) |
| 219 | debug("%s: phy_addr %d, regnum 0x%x, val 0x%x\n", __func__, |
| 220 | phy_addr, regnum, *val); |
| 221 | |
| 222 | return ret; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static u32 phywrite(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 data) |
| 226 | { |
Michal Simek | 198e9a4 | 2015-10-07 16:34:51 +0200 | [diff] [blame] | 227 | debug("%s: phy_addr %d, regnum 0x%x, data 0x%x\n", __func__, phy_addr, |
| 228 | regnum, data); |
| 229 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 230 | return phy_setup_op(dev, phy_addr, regnum, |
| 231 | ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data); |
| 232 | } |
| 233 | |
Michal Simek | f97d7e8 | 2013-04-22 14:41:09 +0200 | [diff] [blame] | 234 | static void phy_detection(struct eth_device *dev) |
| 235 | { |
| 236 | int i; |
| 237 | u16 phyreg; |
| 238 | struct zynq_gem_priv *priv = dev->priv; |
| 239 | |
| 240 | if (priv->phyaddr != -1) { |
| 241 | phyread(dev, priv->phyaddr, PHY_DETECT_REG, &phyreg); |
| 242 | if ((phyreg != 0xFFFF) && |
| 243 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 244 | /* Found a valid PHY address */ |
| 245 | debug("Default phy address %d is valid\n", |
| 246 | priv->phyaddr); |
| 247 | return; |
| 248 | } else { |
| 249 | debug("PHY address is not setup correctly %d\n", |
| 250 | priv->phyaddr); |
| 251 | priv->phyaddr = -1; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | debug("detecting phy address\n"); |
| 256 | if (priv->phyaddr == -1) { |
| 257 | /* detect the PHY address */ |
| 258 | for (i = 31; i >= 0; i--) { |
| 259 | phyread(dev, i, PHY_DETECT_REG, &phyreg); |
| 260 | if ((phyreg != 0xFFFF) && |
| 261 | ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) { |
| 262 | /* Found a valid PHY address */ |
| 263 | priv->phyaddr = i; |
| 264 | debug("Found valid phy address, %d\n", i); |
| 265 | return; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | printf("PHY is not detected\n"); |
| 270 | } |
| 271 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 272 | static int zynq_gem_setup_mac(struct eth_device *dev) |
| 273 | { |
| 274 | u32 i, macaddrlow, macaddrhigh; |
| 275 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
| 276 | |
| 277 | /* Set the MAC bits [31:0] in BOT */ |
| 278 | macaddrlow = dev->enetaddr[0]; |
| 279 | macaddrlow |= dev->enetaddr[1] << 8; |
| 280 | macaddrlow |= dev->enetaddr[2] << 16; |
| 281 | macaddrlow |= dev->enetaddr[3] << 24; |
| 282 | |
| 283 | /* Set MAC bits [47:32] in TOP */ |
| 284 | macaddrhigh = dev->enetaddr[4]; |
| 285 | macaddrhigh |= dev->enetaddr[5] << 8; |
| 286 | |
| 287 | for (i = 0; i < 4; i++) { |
| 288 | writel(0, ®s->laddr[i][LADDR_LOW]); |
| 289 | writel(0, ®s->laddr[i][LADDR_HIGH]); |
| 290 | /* Do not use MATCHx register */ |
| 291 | writel(0, ®s->match[i]); |
| 292 | } |
| 293 | |
| 294 | writel(macaddrlow, ®s->laddr[0][LADDR_LOW]); |
| 295 | writel(macaddrhigh, ®s->laddr[0][LADDR_HIGH]); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int zynq_gem_init(struct eth_device *dev, bd_t * bis) |
| 301 | { |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 302 | u32 i; |
| 303 | unsigned long clk_rate = 0; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 304 | struct phy_device *phydev; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 305 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
| 306 | struct zynq_gem_priv *priv = dev->priv; |
| 307 | const u32 supported = SUPPORTED_10baseT_Half | |
| 308 | SUPPORTED_10baseT_Full | |
| 309 | SUPPORTED_100baseT_Half | |
| 310 | SUPPORTED_100baseT_Full | |
| 311 | SUPPORTED_1000baseT_Half | |
| 312 | SUPPORTED_1000baseT_Full; |
| 313 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 314 | if (!priv->init) { |
| 315 | /* Disable all interrupts */ |
| 316 | writel(0xFFFFFFFF, ®s->idr); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 317 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 318 | /* Disable the receiver & transmitter */ |
| 319 | writel(0, ®s->nwctrl); |
| 320 | writel(0, ®s->txsr); |
| 321 | writel(0, ®s->rxsr); |
| 322 | writel(0, ®s->phymntnc); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 323 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 324 | /* Clear the Hash registers for the mac address |
| 325 | * pointed by AddressPtr |
| 326 | */ |
| 327 | writel(0x0, ®s->hashl); |
| 328 | /* Write bits [63:32] in TOP */ |
| 329 | writel(0x0, ®s->hashh); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 330 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 331 | /* Clear all counters */ |
Michal Simek | 0ebf404 | 2015-10-05 12:49:48 +0200 | [diff] [blame] | 332 | for (i = 0; i < STAT_SIZE; i++) |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 333 | readl(®s->stat[i]); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 334 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 335 | /* Setup RxBD space */ |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 336 | memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd)); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 337 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 338 | for (i = 0; i < RX_BUF; i++) { |
| 339 | priv->rx_bd[i].status = 0xF0000000; |
| 340 | priv->rx_bd[i].addr = |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 341 | ((ulong)(priv->rxbuffers) + |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 342 | (i * PKTSIZE_ALIGN)); |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 343 | } |
| 344 | /* WRAP bit to last BD */ |
| 345 | priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK; |
| 346 | /* Write RxBDs to IP */ |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 347 | writel((ulong)priv->rx_bd, ®s->rxqbase); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 348 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 349 | /* Setup for DMA Configuration register */ |
| 350 | writel(ZYNQ_GEM_DMACR_INIT, ®s->dmacr); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 351 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 352 | /* Setup for Network Control register, MDIO, Rx and Tx enable */ |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 353 | setbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 354 | |
Michal Simek | 0586875 | 2013-01-24 13:04:12 +0100 | [diff] [blame] | 355 | priv->init++; |
| 356 | } |
| 357 | |
Michal Simek | f97d7e8 | 2013-04-22 14:41:09 +0200 | [diff] [blame] | 358 | phy_detection(dev); |
| 359 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 360 | /* interface - look at tsec */ |
Michal Simek | c1a9fa4 | 2014-02-25 10:25:38 +0100 | [diff] [blame] | 361 | phydev = phy_connect(priv->bus, priv->phyaddr, dev, |
Michal Simek | 16ce6de | 2015-10-07 16:42:56 +0200 | [diff] [blame] | 362 | priv->interface); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 363 | |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 364 | phydev->supported = supported | ADVERTISED_Pause | |
| 365 | ADVERTISED_Asym_Pause; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 366 | phydev->advertising = phydev->supported; |
| 367 | priv->phydev = phydev; |
| 368 | phy_config(phydev); |
| 369 | phy_startup(phydev); |
| 370 | |
Michal Simek | 4ed4aa2 | 2013-11-12 14:25:29 +0100 | [diff] [blame] | 371 | if (!phydev->link) { |
| 372 | printf("%s: No link.\n", phydev->dev->name); |
| 373 | return -1; |
| 374 | } |
| 375 | |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 376 | switch (phydev->speed) { |
| 377 | case SPEED_1000: |
| 378 | writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED1000, |
| 379 | ®s->nwcfg); |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 380 | clk_rate = ZYNQ_GEM_FREQUENCY_1000; |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 381 | break; |
| 382 | case SPEED_100: |
| 383 | clrsetbits_le32(®s->nwcfg, ZYNQ_GEM_NWCFG_SPEED1000, |
| 384 | ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED100); |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 385 | clk_rate = ZYNQ_GEM_FREQUENCY_100; |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 386 | break; |
| 387 | case SPEED_10: |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 388 | clk_rate = ZYNQ_GEM_FREQUENCY_10; |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 389 | break; |
| 390 | } |
David Andrey | 01fbf31 | 2013-04-05 17:24:24 +0200 | [diff] [blame] | 391 | |
| 392 | /* Change the rclk and clk only not using EMIO interface */ |
| 393 | if (!priv->emio) |
| 394 | zynq_slcr_gem_clk_setup(dev->iobase != |
Soren Brinkmann | 97598fc | 2013-11-21 13:39:01 -0800 | [diff] [blame] | 395 | ZYNQ_GEM_BASEADDR0, clk_rate); |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 396 | |
| 397 | setbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK | |
| 398 | ZYNQ_GEM_NWCTRL_TXEN_MASK); |
| 399 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int zynq_gem_send(struct eth_device *dev, void *ptr, int len) |
| 404 | { |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 405 | u32 addr, size; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 406 | struct zynq_gem_priv *priv = dev->priv; |
| 407 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 408 | |
| 409 | /* setup BD */ |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 410 | writel((ulong)priv->tx_bd, ®s->txqbase); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 411 | |
| 412 | /* Setup Tx BD */ |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 413 | memset(priv->tx_bd, 0, sizeof(struct emac_bd)); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 414 | |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 415 | priv->tx_bd->addr = (ulong)ptr; |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 416 | priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) | |
Michal Simek | e65d33c | 2015-05-26 12:01:12 +0200 | [diff] [blame] | 417 | ZYNQ_GEM_TXBUF_LAST_MASK | |
| 418 | ZYNQ_GEM_TXBUF_WRAP_MASK; |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 419 | |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 420 | addr = (ulong) ptr; |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 421 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 422 | size = roundup(len, ARCH_DMA_MINALIGN); |
| 423 | flush_dcache_range(addr, addr + size); |
Siva Durga Prasad Paladugu | 96f4f14 | 2014-12-06 12:57:53 +0530 | [diff] [blame] | 424 | |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 425 | addr = (ulong)priv->rxbuffers; |
Siva Durga Prasad Paladugu | 96f4f14 | 2014-12-06 12:57:53 +0530 | [diff] [blame] | 426 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
| 427 | size = roundup((RX_BUF * PKTSIZE_ALIGN), ARCH_DMA_MINALIGN); |
| 428 | flush_dcache_range(addr, addr + size); |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 429 | barrier(); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 430 | |
| 431 | /* Start transmit */ |
| 432 | setbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK); |
| 433 | |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 434 | /* Read TX BD status */ |
| 435 | if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_UNDERRUN) |
| 436 | printf("TX underrun\n"); |
| 437 | if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED) |
| 438 | printf("TX buffers exhausted in mid frame\n"); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 439 | |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | /* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */ |
| 444 | static int zynq_gem_recv(struct eth_device *dev) |
| 445 | { |
| 446 | int frame_len; |
| 447 | struct zynq_gem_priv *priv = dev->priv; |
| 448 | struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current]; |
| 449 | struct emac_bd *first_bd; |
| 450 | |
| 451 | if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK)) |
| 452 | return 0; |
| 453 | |
| 454 | if (!(current_bd->status & |
| 455 | (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) { |
| 456 | printf("GEM: SOF or EOF not set for last buffer received!\n"); |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK; |
| 461 | if (frame_len) { |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 462 | u32 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK; |
| 463 | addr &= ~(ARCH_DMA_MINALIGN - 1); |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 464 | |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 465 | net_process_received_packet((u8 *)(ulong)addr, frame_len); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 466 | |
| 467 | if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK) |
| 468 | priv->rx_first_buf = priv->rxbd_current; |
| 469 | else { |
| 470 | current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK; |
| 471 | current_bd->status = 0xF0000000; /* FIXME */ |
| 472 | } |
| 473 | |
| 474 | if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) { |
| 475 | first_bd = &priv->rx_bd[priv->rx_first_buf]; |
| 476 | first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK; |
| 477 | first_bd->status = 0xF0000000; |
| 478 | } |
| 479 | |
| 480 | if ((++priv->rxbd_current) >= RX_BUF) |
| 481 | priv->rxbd_current = 0; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Michal Simek | 3b90d0a | 2013-01-25 08:24:18 +0100 | [diff] [blame] | 484 | return frame_len; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static void zynq_gem_halt(struct eth_device *dev) |
| 488 | { |
| 489 | struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase; |
| 490 | |
Michal Simek | 8024352 | 2012-10-15 14:01:23 +0200 | [diff] [blame] | 491 | clrsetbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK | |
| 492 | ZYNQ_GEM_NWCTRL_TXEN_MASK, 0); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | static int zynq_gem_miiphyread(const char *devname, uchar addr, |
| 496 | uchar reg, ushort *val) |
| 497 | { |
| 498 | struct eth_device *dev = eth_get_dev(); |
| 499 | int ret; |
| 500 | |
| 501 | ret = phyread(dev, addr, reg, val); |
| 502 | debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, *val); |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | static int zynq_gem_miiphy_write(const char *devname, uchar addr, |
| 507 | uchar reg, ushort val) |
| 508 | { |
| 509 | struct eth_device *dev = eth_get_dev(); |
| 510 | |
| 511 | debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val); |
| 512 | return phywrite(dev, addr, reg, val); |
| 513 | } |
| 514 | |
Michal Simek | 5840537 | 2015-01-14 15:44:21 +0100 | [diff] [blame] | 515 | int zynq_gem_initialize(bd_t *bis, phys_addr_t base_addr, |
| 516 | int phy_addr, u32 emio) |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 517 | { |
| 518 | struct eth_device *dev; |
| 519 | struct zynq_gem_priv *priv; |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 520 | void *bd_space; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 521 | |
| 522 | dev = calloc(1, sizeof(*dev)); |
| 523 | if (dev == NULL) |
| 524 | return -1; |
| 525 | |
| 526 | dev->priv = calloc(1, sizeof(struct zynq_gem_priv)); |
| 527 | if (dev->priv == NULL) { |
| 528 | free(dev); |
| 529 | return -1; |
| 530 | } |
| 531 | priv = dev->priv; |
| 532 | |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 533 | /* Align rxbuffers to ARCH_DMA_MINALIGN */ |
| 534 | priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN); |
| 535 | memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN); |
| 536 | |
Siva Durga Prasad Paladugu | 96f4f14 | 2014-12-06 12:57:53 +0530 | [diff] [blame] | 537 | /* Align bd_space to MMU_SECTION_SHIFT */ |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 538 | bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE); |
Michal Simek | 9ce1edc | 2015-04-15 13:31:28 +0200 | [diff] [blame] | 539 | mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, |
| 540 | BD_SPACE, DCACHE_OFF); |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 541 | |
| 542 | /* Initialize the bd spaces for tx and rx bd's */ |
| 543 | priv->tx_bd = (struct emac_bd *)bd_space; |
Prabhakar Kushwaha | 5b47d40 | 2015-10-25 13:18:54 +0530 | [diff] [blame] | 544 | priv->rx_bd = (struct emac_bd *)((ulong)bd_space + BD_SEPRN_SPACE); |
Srikanth Thokala | a514423 | 2013-11-08 22:55:48 +0530 | [diff] [blame] | 545 | |
David Andrey | 117cd4c | 2013-04-04 19:13:07 +0200 | [diff] [blame] | 546 | priv->phyaddr = phy_addr; |
David Andrey | 01fbf31 | 2013-04-05 17:24:24 +0200 | [diff] [blame] | 547 | priv->emio = emio; |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 548 | |
Michal Simek | 16ce6de | 2015-10-07 16:42:56 +0200 | [diff] [blame] | 549 | #ifndef CONFIG_ZYNQ_GEM_INTERFACE |
| 550 | priv->interface = PHY_INTERFACE_MODE_MII; |
| 551 | #else |
| 552 | priv->interface = CONFIG_ZYNQ_GEM_INTERFACE; |
| 553 | #endif |
| 554 | |
Michal Simek | 5840537 | 2015-01-14 15:44:21 +0100 | [diff] [blame] | 555 | sprintf(dev->name, "Gem.%lx", base_addr); |
Michal Simek | 185f7d9 | 2012-09-13 20:23:34 +0000 | [diff] [blame] | 556 | |
| 557 | dev->iobase = base_addr; |
| 558 | |
| 559 | dev->init = zynq_gem_init; |
| 560 | dev->halt = zynq_gem_halt; |
| 561 | dev->send = zynq_gem_send; |
| 562 | dev->recv = zynq_gem_recv; |
| 563 | dev->write_hwaddr = zynq_gem_setup_mac; |
| 564 | |
| 565 | eth_register(dev); |
| 566 | |
| 567 | miiphy_register(dev->name, zynq_gem_miiphyread, zynq_gem_miiphy_write); |
| 568 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 569 | |
| 570 | return 1; |
| 571 | } |
Michal Simek | f88a686 | 2014-02-24 11:16:30 +0100 | [diff] [blame] | 572 | |
Masahiro Yamada | 0f92582 | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 573 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Michal Simek | f88a686 | 2014-02-24 11:16:30 +0100 | [diff] [blame] | 574 | int zynq_gem_of_init(const void *blob) |
| 575 | { |
| 576 | int offset = 0; |
| 577 | u32 ret = 0; |
| 578 | u32 reg, phy_reg; |
| 579 | |
| 580 | debug("ZYNQ GEM: Initialization\n"); |
| 581 | |
| 582 | do { |
| 583 | offset = fdt_node_offset_by_compatible(blob, offset, |
| 584 | "xlnx,ps7-ethernet-1.00.a"); |
| 585 | if (offset != -1) { |
| 586 | reg = fdtdec_get_addr(blob, offset, "reg"); |
| 587 | if (reg != FDT_ADDR_T_NONE) { |
| 588 | offset = fdtdec_lookup_phandle(blob, offset, |
| 589 | "phy-handle"); |
| 590 | if (offset != -1) |
| 591 | phy_reg = fdtdec_get_addr(blob, offset, |
| 592 | "reg"); |
| 593 | else |
| 594 | phy_reg = 0; |
| 595 | |
| 596 | debug("ZYNQ GEM: addr %x, phyaddr %x\n", |
| 597 | reg, phy_reg); |
| 598 | |
| 599 | ret |= zynq_gem_initialize(NULL, reg, |
| 600 | phy_reg, 0); |
| 601 | |
| 602 | } else { |
| 603 | debug("ZYNQ GEM: Can't get base address\n"); |
| 604 | return -1; |
| 605 | } |
| 606 | } |
| 607 | } while (offset != -1); |
| 608 | |
| 609 | return ret; |
| 610 | } |
| 611 | #endif |