blob: 0ee909c97fdfa91499eb60648e4d59a775c5faa0 [file] [log] [blame]
Michal Simek185f7d92012-09-13 20:23:34 +00001/*
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 Denk3765b3e2013-10-07 13:07:26 +02009 * SPDX-License-Identifier: GPL-2.0+
Michal Simek185f7d92012-09-13 20:23:34 +000010 */
11
12#include <common.h>
Michal Simek6889ca72015-11-30 14:14:56 +010013#include <dm.h>
Michal Simek185f7d92012-09-13 20:23:34 +000014#include <net.h>
Michal Simek2fd24892014-04-25 14:17:38 +020015#include <netdev.h>
Michal Simek185f7d92012-09-13 20:23:34 +000016#include <config.h>
17#include <malloc.h>
18#include <asm/io.h>
19#include <phy.h>
20#include <miiphy.h>
21#include <watchdog.h>
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +053022#include <asm/system.h>
David Andrey01fbf312013-04-05 17:24:24 +020023#include <asm/arch/hardware.h>
Michal Simek80243522012-10-15 14:01:23 +020024#include <asm/arch/sys_proto.h>
Michal Simeke4d23182015-08-17 09:57:46 +020025#include <asm-generic/errno.h>
Michal Simek185f7d92012-09-13 20:23:34 +000026
Michal Simek6889ca72015-11-30 14:14:56 +010027DECLARE_GLOBAL_DATA_PTR;
28
Michal Simek185f7d92012-09-13 20:23:34 +000029#if !defined(CONFIG_PHYLIB)
30# error XILINX_GEM_ETHERNET requires PHYLIB
31#endif
32
33/* Bit/mask specification */
34#define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */
35#define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */
36#define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */
37#define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */
38#define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */
39
40#define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */
41#define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */
42#define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */
43
44#define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */
45#define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */
46#define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */
47
48/* Wrap bit, last descriptor */
49#define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000
50#define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */
Michal Simek23a598f2015-08-17 09:58:54 +020051#define ZYNQ_GEM_TXBUF_USED_MASK 0x80000000 /* Used by Hw */
Michal Simek185f7d92012-09-13 20:23:34 +000052
Michal Simek185f7d92012-09-13 20:23:34 +000053#define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */
54#define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */
55#define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */
56#define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */
57
Michal Simek80243522012-10-15 14:01:23 +020058#define ZYNQ_GEM_NWCFG_SPEED100 0x000000001 /* 100 Mbps operation */
59#define ZYNQ_GEM_NWCFG_SPEED1000 0x000000400 /* 1Gbps operation */
60#define ZYNQ_GEM_NWCFG_FDEN 0x000000002 /* Full Duplex mode */
61#define ZYNQ_GEM_NWCFG_FSREM 0x000020000 /* FCS removal */
Michal Simek6777f382015-09-08 17:07:01 +020062#define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x0000c0000 /* Div pclk by 48, max 120MHz */
Michal Simek185f7d92012-09-13 20:23:34 +000063
Siva Durga Prasad Paladugu8a584c82014-07-08 15:31:03 +053064#ifdef CONFIG_ARM64
65# define ZYNQ_GEM_DBUS_WIDTH (1 << 21) /* 64 bit bus */
66#else
67# define ZYNQ_GEM_DBUS_WIDTH (0 << 21) /* 32 bit bus */
68#endif
69
70#define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_DBUS_WIDTH | \
71 ZYNQ_GEM_NWCFG_FDEN | \
Michal Simek185f7d92012-09-13 20:23:34 +000072 ZYNQ_GEM_NWCFG_FSREM | \
73 ZYNQ_GEM_NWCFG_MDCCLKDIV)
74
75#define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */
76
77#define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */
78/* Use full configured addressable space (8 Kb) */
79#define ZYNQ_GEM_DMACR_RXSIZE 0x00000300
80/* Use full configured addressable space (4 Kb) */
81#define ZYNQ_GEM_DMACR_TXSIZE 0x00000400
82/* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
83#define ZYNQ_GEM_DMACR_RXBUF 0x00180000
84
85#define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \
86 ZYNQ_GEM_DMACR_RXSIZE | \
87 ZYNQ_GEM_DMACR_TXSIZE | \
88 ZYNQ_GEM_DMACR_RXBUF)
89
Michal Simeke4d23182015-08-17 09:57:46 +020090#define ZYNQ_GEM_TSR_DONE 0x00000020 /* Tx done mask */
91
Michal Simekf97d7e82013-04-22 14:41:09 +020092/* Use MII register 1 (MII status register) to detect PHY */
93#define PHY_DETECT_REG 1
94
95/* Mask used to verify certain PHY features (or register contents)
96 * in the register above:
97 * 0x1000: 10Mbps full duplex support
98 * 0x0800: 10Mbps half duplex support
99 * 0x0008: Auto-negotiation support
100 */
101#define PHY_DETECT_MASK 0x1808
102
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530103/* TX BD status masks */
104#define ZYNQ_GEM_TXBUF_FRMLEN_MASK 0x000007ff
105#define ZYNQ_GEM_TXBUF_EXHAUSTED 0x08000000
106#define ZYNQ_GEM_TXBUF_UNDERRUN 0x10000000
107
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800108/* Clock frequencies for different speeds */
109#define ZYNQ_GEM_FREQUENCY_10 2500000UL
110#define ZYNQ_GEM_FREQUENCY_100 25000000UL
111#define ZYNQ_GEM_FREQUENCY_1000 125000000UL
112
Michal Simek185f7d92012-09-13 20:23:34 +0000113/* Device registers */
114struct zynq_gem_regs {
Michal Simek97a51a02015-10-05 11:49:43 +0200115 u32 nwctrl; /* 0x0 - Network Control reg */
116 u32 nwcfg; /* 0x4 - Network Config reg */
117 u32 nwsr; /* 0x8 - Network Status reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000118 u32 reserved1;
Michal Simek97a51a02015-10-05 11:49:43 +0200119 u32 dmacr; /* 0x10 - DMA Control reg */
120 u32 txsr; /* 0x14 - TX Status reg */
121 u32 rxqbase; /* 0x18 - RX Q Base address reg */
122 u32 txqbase; /* 0x1c - TX Q Base address reg */
123 u32 rxsr; /* 0x20 - RX Status reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000124 u32 reserved2[2];
Michal Simek97a51a02015-10-05 11:49:43 +0200125 u32 idr; /* 0x2c - Interrupt Disable reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000126 u32 reserved3;
Michal Simek97a51a02015-10-05 11:49:43 +0200127 u32 phymntnc; /* 0x34 - Phy Maintaince reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000128 u32 reserved4[18];
Michal Simek97a51a02015-10-05 11:49:43 +0200129 u32 hashl; /* 0x80 - Hash Low address reg */
130 u32 hashh; /* 0x84 - Hash High address reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000131#define LADDR_LOW 0
132#define LADDR_HIGH 1
Michal Simek97a51a02015-10-05 11:49:43 +0200133 u32 laddr[4][LADDR_HIGH + 1]; /* 0x8c - Specific1 addr low/high reg */
134 u32 match[4]; /* 0xa8 - Type ID1 Match reg */
Michal Simek185f7d92012-09-13 20:23:34 +0000135 u32 reserved6[18];
Michal Simek0ebf4042015-10-05 12:49:48 +0200136#define STAT_SIZE 44
137 u32 stat[STAT_SIZE]; /* 0x100 - Octects transmitted Low reg */
Edgar E. Iglesias603ff002015-09-25 23:50:07 -0700138 u32 reserved7[164];
139 u32 transmit_q1_ptr; /* 0x440 - Transmit priority queue 1 */
140 u32 reserved8[15];
141 u32 receive_q1_ptr; /* 0x480 - Receive priority queue 1 */
Michal Simek185f7d92012-09-13 20:23:34 +0000142};
143
144/* BD descriptors */
145struct emac_bd {
146 u32 addr; /* Next descriptor pointer */
147 u32 status;
148};
149
Siva Durga Prasad Paladugueda9d302015-04-15 12:15:01 +0530150#define RX_BUF 32
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530151/* Page table entries are set to 1MB, or multiples of 1MB
152 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
153 */
154#define BD_SPACE 0x100000
155/* BD separation space */
Michal Simekff475872015-08-17 09:45:53 +0200156#define BD_SEPRN_SPACE (RX_BUF * sizeof(struct emac_bd))
Michal Simek185f7d92012-09-13 20:23:34 +0000157
Edgar E. Iglesias603ff002015-09-25 23:50:07 -0700158/* Setup the first free TX descriptor */
159#define TX_FREE_DESC 2
160
Michal Simek185f7d92012-09-13 20:23:34 +0000161/* Initialized, rxbd_current, rx_first_buf must be 0 after init */
162struct zynq_gem_priv {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530163 struct emac_bd *tx_bd;
164 struct emac_bd *rx_bd;
165 char *rxbuffers;
Michal Simek185f7d92012-09-13 20:23:34 +0000166 u32 rxbd_current;
167 u32 rx_first_buf;
168 int phyaddr;
David Andrey01fbf312013-04-05 17:24:24 +0200169 u32 emio;
Michal Simek05868752013-01-24 13:04:12 +0100170 int init;
Michal Simekf2fc2762015-11-30 10:24:15 +0100171 struct zynq_gem_regs *iobase;
Michal Simek16ce6de2015-10-07 16:42:56 +0200172 phy_interface_t interface;
Michal Simek185f7d92012-09-13 20:23:34 +0000173 struct phy_device *phydev;
174 struct mii_dev *bus;
175};
176
Michal Simek3fac2722015-11-30 10:09:43 +0100177static inline int mdio_wait(struct zynq_gem_regs *regs)
Michal Simek185f7d92012-09-13 20:23:34 +0000178{
Michal Simek4c8b7bf2012-10-16 17:37:11 +0200179 u32 timeout = 20000;
Michal Simek185f7d92012-09-13 20:23:34 +0000180
181 /* Wait till MDIO interface is ready to accept a new transaction. */
182 while (--timeout) {
183 if (readl(&regs->nwsr) & ZYNQ_GEM_NWSR_MDIOIDLE_MASK)
184 break;
185 WATCHDOG_RESET();
186 }
187
188 if (!timeout) {
189 printf("%s: Timeout\n", __func__);
190 return 1;
191 }
192
193 return 0;
194}
195
Michal Simekf2fc2762015-11-30 10:24:15 +0100196static u32 phy_setup_op(struct zynq_gem_priv *priv, u32 phy_addr, u32 regnum,
197 u32 op, u16 *data)
Michal Simek185f7d92012-09-13 20:23:34 +0000198{
199 u32 mgtcr;
Michal Simekf2fc2762015-11-30 10:24:15 +0100200 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek185f7d92012-09-13 20:23:34 +0000201
Michal Simek3fac2722015-11-30 10:09:43 +0100202 if (mdio_wait(regs))
Michal Simek185f7d92012-09-13 20:23:34 +0000203 return 1;
204
205 /* Construct mgtcr mask for the operation */
206 mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
207 (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
208 (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
209
210 /* Write mgtcr and wait for completion */
211 writel(mgtcr, &regs->phymntnc);
212
Michal Simek3fac2722015-11-30 10:09:43 +0100213 if (mdio_wait(regs))
Michal Simek185f7d92012-09-13 20:23:34 +0000214 return 1;
215
216 if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
217 *data = readl(&regs->phymntnc);
218
219 return 0;
220}
221
Michal Simekf2fc2762015-11-30 10:24:15 +0100222static u32 phyread(struct zynq_gem_priv *priv, u32 phy_addr,
223 u32 regnum, u16 *val)
Michal Simek185f7d92012-09-13 20:23:34 +0000224{
Michal Simek198e9a42015-10-07 16:34:51 +0200225 u32 ret;
226
Michal Simekf2fc2762015-11-30 10:24:15 +0100227 ret = phy_setup_op(priv, phy_addr, regnum,
228 ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
Michal Simek198e9a42015-10-07 16:34:51 +0200229
230 if (!ret)
231 debug("%s: phy_addr %d, regnum 0x%x, val 0x%x\n", __func__,
232 phy_addr, regnum, *val);
233
234 return ret;
Michal Simek185f7d92012-09-13 20:23:34 +0000235}
236
Michal Simekf2fc2762015-11-30 10:24:15 +0100237static u32 phywrite(struct zynq_gem_priv *priv, u32 phy_addr,
238 u32 regnum, u16 data)
Michal Simek185f7d92012-09-13 20:23:34 +0000239{
Michal Simek198e9a42015-10-07 16:34:51 +0200240 debug("%s: phy_addr %d, regnum 0x%x, data 0x%x\n", __func__, phy_addr,
241 regnum, data);
242
Michal Simekf2fc2762015-11-30 10:24:15 +0100243 return phy_setup_op(priv, phy_addr, regnum,
244 ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
Michal Simek185f7d92012-09-13 20:23:34 +0000245}
246
Michal Simek6889ca72015-11-30 14:14:56 +0100247static int phy_detection(struct udevice *dev)
Michal Simekf97d7e82013-04-22 14:41:09 +0200248{
249 int i;
250 u16 phyreg;
251 struct zynq_gem_priv *priv = dev->priv;
252
253 if (priv->phyaddr != -1) {
Michal Simekf2fc2762015-11-30 10:24:15 +0100254 phyread(priv, priv->phyaddr, PHY_DETECT_REG, &phyreg);
Michal Simekf97d7e82013-04-22 14:41:09 +0200255 if ((phyreg != 0xFFFF) &&
256 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
257 /* Found a valid PHY address */
258 debug("Default phy address %d is valid\n",
259 priv->phyaddr);
Michal Simekb9047252015-11-30 13:38:32 +0100260 return 0;
Michal Simekf97d7e82013-04-22 14:41:09 +0200261 } else {
262 debug("PHY address is not setup correctly %d\n",
263 priv->phyaddr);
264 priv->phyaddr = -1;
265 }
266 }
267
268 debug("detecting phy address\n");
269 if (priv->phyaddr == -1) {
270 /* detect the PHY address */
271 for (i = 31; i >= 0; i--) {
Michal Simekf2fc2762015-11-30 10:24:15 +0100272 phyread(priv, i, PHY_DETECT_REG, &phyreg);
Michal Simekf97d7e82013-04-22 14:41:09 +0200273 if ((phyreg != 0xFFFF) &&
274 ((phyreg & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
275 /* Found a valid PHY address */
276 priv->phyaddr = i;
277 debug("Found valid phy address, %d\n", i);
Michal Simekb9047252015-11-30 13:38:32 +0100278 return 0;
Michal Simekf97d7e82013-04-22 14:41:09 +0200279 }
280 }
281 }
282 printf("PHY is not detected\n");
Michal Simekb9047252015-11-30 13:38:32 +0100283 return -1;
Michal Simekf97d7e82013-04-22 14:41:09 +0200284}
285
Michal Simek6889ca72015-11-30 14:14:56 +0100286static int zynq_gem_setup_mac(struct udevice *dev)
Michal Simek185f7d92012-09-13 20:23:34 +0000287{
288 u32 i, macaddrlow, macaddrhigh;
Michal Simek6889ca72015-11-30 14:14:56 +0100289 struct eth_pdata *pdata = dev_get_platdata(dev);
290 struct zynq_gem_priv *priv = dev_get_priv(dev);
291 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek185f7d92012-09-13 20:23:34 +0000292
293 /* Set the MAC bits [31:0] in BOT */
Michal Simek6889ca72015-11-30 14:14:56 +0100294 macaddrlow = pdata->enetaddr[0];
295 macaddrlow |= pdata->enetaddr[1] << 8;
296 macaddrlow |= pdata->enetaddr[2] << 16;
297 macaddrlow |= pdata->enetaddr[3] << 24;
Michal Simek185f7d92012-09-13 20:23:34 +0000298
299 /* Set MAC bits [47:32] in TOP */
Michal Simek6889ca72015-11-30 14:14:56 +0100300 macaddrhigh = pdata->enetaddr[4];
301 macaddrhigh |= pdata->enetaddr[5] << 8;
Michal Simek185f7d92012-09-13 20:23:34 +0000302
303 for (i = 0; i < 4; i++) {
304 writel(0, &regs->laddr[i][LADDR_LOW]);
305 writel(0, &regs->laddr[i][LADDR_HIGH]);
306 /* Do not use MATCHx register */
307 writel(0, &regs->match[i]);
308 }
309
310 writel(macaddrlow, &regs->laddr[0][LADDR_LOW]);
311 writel(macaddrhigh, &regs->laddr[0][LADDR_HIGH]);
312
313 return 0;
314}
315
Michal Simek6889ca72015-11-30 14:14:56 +0100316static int zynq_phy_init(struct udevice *dev)
Michal Simek68cc3bd2015-11-30 13:54:43 +0100317{
318 int ret;
Michal Simek6889ca72015-11-30 14:14:56 +0100319 struct zynq_gem_priv *priv = dev_get_priv(dev);
320 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek68cc3bd2015-11-30 13:54:43 +0100321 const u32 supported = SUPPORTED_10baseT_Half |
322 SUPPORTED_10baseT_Full |
323 SUPPORTED_100baseT_Half |
324 SUPPORTED_100baseT_Full |
325 SUPPORTED_1000baseT_Half |
326 SUPPORTED_1000baseT_Full;
327
Michal Simekc8e29272015-11-30 13:58:36 +0100328 /* Enable only MDIO bus */
329 writel(ZYNQ_GEM_NWCTRL_MDEN_MASK, &regs->nwctrl);
330
Michal Simek68cc3bd2015-11-30 13:54:43 +0100331 ret = phy_detection(dev);
332 if (ret) {
333 printf("GEM PHY init failed\n");
334 return ret;
335 }
336
337 priv->phydev = phy_connect(priv->bus, priv->phyaddr, dev,
338 priv->interface);
Michal Simek90c6f2e2015-11-30 14:03:37 +0100339 if (!priv->phydev)
340 return -ENODEV;
Michal Simek68cc3bd2015-11-30 13:54:43 +0100341
342 priv->phydev->supported = supported | ADVERTISED_Pause |
343 ADVERTISED_Asym_Pause;
344 priv->phydev->advertising = priv->phydev->supported;
345 phy_config(priv->phydev);
346
347 return 0;
348}
349
Michal Simek6889ca72015-11-30 14:14:56 +0100350static int zynq_gem_init(struct udevice *dev)
Michal Simek185f7d92012-09-13 20:23:34 +0000351{
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800352 u32 i;
353 unsigned long clk_rate = 0;
Michal Simek6889ca72015-11-30 14:14:56 +0100354 struct zynq_gem_priv *priv = dev_get_priv(dev);
355 struct zynq_gem_regs *regs = priv->iobase;
Edgar E. Iglesias603ff002015-09-25 23:50:07 -0700356 struct emac_bd *dummy_tx_bd = &priv->tx_bd[TX_FREE_DESC];
357 struct emac_bd *dummy_rx_bd = &priv->tx_bd[TX_FREE_DESC + 2];
Michal Simek185f7d92012-09-13 20:23:34 +0000358
Michal Simek05868752013-01-24 13:04:12 +0100359 if (!priv->init) {
360 /* Disable all interrupts */
361 writel(0xFFFFFFFF, &regs->idr);
Michal Simek185f7d92012-09-13 20:23:34 +0000362
Michal Simek05868752013-01-24 13:04:12 +0100363 /* Disable the receiver & transmitter */
364 writel(0, &regs->nwctrl);
365 writel(0, &regs->txsr);
366 writel(0, &regs->rxsr);
367 writel(0, &regs->phymntnc);
Michal Simek185f7d92012-09-13 20:23:34 +0000368
Michal Simek05868752013-01-24 13:04:12 +0100369 /* Clear the Hash registers for the mac address
370 * pointed by AddressPtr
371 */
372 writel(0x0, &regs->hashl);
373 /* Write bits [63:32] in TOP */
374 writel(0x0, &regs->hashh);
Michal Simek185f7d92012-09-13 20:23:34 +0000375
Michal Simek05868752013-01-24 13:04:12 +0100376 /* Clear all counters */
Michal Simek0ebf4042015-10-05 12:49:48 +0200377 for (i = 0; i < STAT_SIZE; i++)
Michal Simek05868752013-01-24 13:04:12 +0100378 readl(&regs->stat[i]);
Michal Simek185f7d92012-09-13 20:23:34 +0000379
Michal Simek05868752013-01-24 13:04:12 +0100380 /* Setup RxBD space */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530381 memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000382
Michal Simek05868752013-01-24 13:04:12 +0100383 for (i = 0; i < RX_BUF; i++) {
384 priv->rx_bd[i].status = 0xF0000000;
385 priv->rx_bd[i].addr =
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530386 ((ulong)(priv->rxbuffers) +
Michal Simek185f7d92012-09-13 20:23:34 +0000387 (i * PKTSIZE_ALIGN));
Michal Simek05868752013-01-24 13:04:12 +0100388 }
389 /* WRAP bit to last BD */
390 priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
391 /* Write RxBDs to IP */
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530392 writel((ulong)priv->rx_bd, &regs->rxqbase);
Michal Simek185f7d92012-09-13 20:23:34 +0000393
Michal Simek05868752013-01-24 13:04:12 +0100394 /* Setup for DMA Configuration register */
395 writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
Michal Simek185f7d92012-09-13 20:23:34 +0000396
Michal Simek05868752013-01-24 13:04:12 +0100397 /* Setup for Network Control register, MDIO, Rx and Tx enable */
Michal Simek80243522012-10-15 14:01:23 +0200398 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK);
Michal Simek185f7d92012-09-13 20:23:34 +0000399
Edgar E. Iglesias603ff002015-09-25 23:50:07 -0700400 /* Disable the second priority queue */
401 dummy_tx_bd->addr = 0;
402 dummy_tx_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
403 ZYNQ_GEM_TXBUF_LAST_MASK|
404 ZYNQ_GEM_TXBUF_USED_MASK;
405
406 dummy_rx_bd->addr = ZYNQ_GEM_RXBUF_WRAP_MASK |
407 ZYNQ_GEM_RXBUF_NEW_MASK;
408 dummy_rx_bd->status = 0;
409 flush_dcache_range((ulong)&dummy_tx_bd, (ulong)&dummy_tx_bd +
410 sizeof(dummy_tx_bd));
411 flush_dcache_range((ulong)&dummy_rx_bd, (ulong)&dummy_rx_bd +
412 sizeof(dummy_rx_bd));
413
414 writel((ulong)dummy_tx_bd, &regs->transmit_q1_ptr);
415 writel((ulong)dummy_rx_bd, &regs->receive_q1_ptr);
416
Michal Simek05868752013-01-24 13:04:12 +0100417 priv->init++;
418 }
419
Michal Simek64a7ead2015-11-30 13:44:49 +0100420 phy_startup(priv->phydev);
Michal Simek185f7d92012-09-13 20:23:34 +0000421
Michal Simek64a7ead2015-11-30 13:44:49 +0100422 if (!priv->phydev->link) {
423 printf("%s: No link.\n", priv->phydev->dev->name);
Michal Simek4ed4aa22013-11-12 14:25:29 +0100424 return -1;
425 }
426
Michal Simek64a7ead2015-11-30 13:44:49 +0100427 switch (priv->phydev->speed) {
Michal Simek80243522012-10-15 14:01:23 +0200428 case SPEED_1000:
429 writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED1000,
430 &regs->nwcfg);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800431 clk_rate = ZYNQ_GEM_FREQUENCY_1000;
Michal Simek80243522012-10-15 14:01:23 +0200432 break;
433 case SPEED_100:
Michal Simek242b1542015-09-08 16:55:42 +0200434 writel(ZYNQ_GEM_NWCFG_INIT | ZYNQ_GEM_NWCFG_SPEED100,
435 &regs->nwcfg);
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800436 clk_rate = ZYNQ_GEM_FREQUENCY_100;
Michal Simek80243522012-10-15 14:01:23 +0200437 break;
438 case SPEED_10:
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800439 clk_rate = ZYNQ_GEM_FREQUENCY_10;
Michal Simek80243522012-10-15 14:01:23 +0200440 break;
441 }
David Andrey01fbf312013-04-05 17:24:24 +0200442
443 /* Change the rclk and clk only not using EMIO interface */
444 if (!priv->emio)
Michal Simek6889ca72015-11-30 14:14:56 +0100445 zynq_slcr_gem_clk_setup((ulong)priv->iobase !=
Soren Brinkmann97598fc2013-11-21 13:39:01 -0800446 ZYNQ_GEM_BASEADDR0, clk_rate);
Michal Simek80243522012-10-15 14:01:23 +0200447
448 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
449 ZYNQ_GEM_NWCTRL_TXEN_MASK);
450
Michal Simek185f7d92012-09-13 20:23:34 +0000451 return 0;
452}
453
Michal Simeke4d23182015-08-17 09:57:46 +0200454static int wait_for_bit(const char *func, u32 *reg, const u32 mask,
455 bool set, unsigned int timeout)
456{
457 u32 val;
458 unsigned long start = get_timer(0);
459
460 while (1) {
461 val = readl(reg);
462
463 if (!set)
464 val = ~val;
465
466 if ((val & mask) == mask)
467 return 0;
468
469 if (get_timer(start) > timeout)
470 break;
471
472 udelay(1);
473 }
474
475 debug("%s: Timeout (reg=%p mask=%08x wait_set=%i)\n",
476 func, reg, mask, set);
477
478 return -ETIMEDOUT;
479}
480
Michal Simek6889ca72015-11-30 14:14:56 +0100481static int zynq_gem_send(struct udevice *dev, void *ptr, int len)
Michal Simek185f7d92012-09-13 20:23:34 +0000482{
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530483 u32 addr, size;
Michal Simek6889ca72015-11-30 14:14:56 +0100484 struct zynq_gem_priv *priv = dev_get_priv(dev);
485 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek23a598f2015-08-17 09:58:54 +0200486 struct emac_bd *current_bd = &priv->tx_bd[1];
Michal Simek185f7d92012-09-13 20:23:34 +0000487
Michal Simek185f7d92012-09-13 20:23:34 +0000488 /* Setup Tx BD */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530489 memset(priv->tx_bd, 0, sizeof(struct emac_bd));
Michal Simek185f7d92012-09-13 20:23:34 +0000490
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530491 priv->tx_bd->addr = (ulong)ptr;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530492 priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
Michal Simek23a598f2015-08-17 09:58:54 +0200493 ZYNQ_GEM_TXBUF_LAST_MASK;
494 /* Dummy descriptor to mark it as the last in descriptor chain */
495 current_bd->addr = 0x0;
496 current_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
497 ZYNQ_GEM_TXBUF_LAST_MASK|
498 ZYNQ_GEM_TXBUF_USED_MASK;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530499
Michal Simek45c07742015-08-17 09:50:09 +0200500 /* setup BD */
501 writel((ulong)priv->tx_bd, &regs->txqbase);
502
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530503 addr = (ulong) ptr;
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530504 addr &= ~(ARCH_DMA_MINALIGN - 1);
505 size = roundup(len, ARCH_DMA_MINALIGN);
506 flush_dcache_range(addr, addr + size);
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530507
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530508 addr = (ulong)priv->rxbuffers;
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530509 addr &= ~(ARCH_DMA_MINALIGN - 1);
510 size = roundup((RX_BUF * PKTSIZE_ALIGN), ARCH_DMA_MINALIGN);
511 flush_dcache_range(addr, addr + size);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530512 barrier();
Michal Simek185f7d92012-09-13 20:23:34 +0000513
514 /* Start transmit */
515 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
516
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530517 /* Read TX BD status */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530518 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
519 printf("TX buffers exhausted in mid frame\n");
Michal Simek185f7d92012-09-13 20:23:34 +0000520
Michal Simeke4d23182015-08-17 09:57:46 +0200521 return wait_for_bit(__func__, &regs->txsr, ZYNQ_GEM_TSR_DONE,
522 true, 20000);
Michal Simek185f7d92012-09-13 20:23:34 +0000523}
524
525/* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
Michal Simek6889ca72015-11-30 14:14:56 +0100526static int zynq_gem_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek185f7d92012-09-13 20:23:34 +0000527{
528 int frame_len;
Michal Simek6889ca72015-11-30 14:14:56 +0100529 struct zynq_gem_priv *priv = dev_get_priv(dev);
Michal Simek185f7d92012-09-13 20:23:34 +0000530 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
531 struct emac_bd *first_bd;
532
533 if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
534 return 0;
535
536 if (!(current_bd->status &
537 (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
538 printf("GEM: SOF or EOF not set for last buffer received!\n");
539 return 0;
540 }
541
542 frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
543 if (frame_len) {
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530544 u32 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
545 addr &= ~(ARCH_DMA_MINALIGN - 1);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530546
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530547 net_process_received_packet((u8 *)(ulong)addr, frame_len);
Michal Simek185f7d92012-09-13 20:23:34 +0000548
549 if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK)
550 priv->rx_first_buf = priv->rxbd_current;
551 else {
552 current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
553 current_bd->status = 0xF0000000; /* FIXME */
554 }
555
556 if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
557 first_bd = &priv->rx_bd[priv->rx_first_buf];
558 first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
559 first_bd->status = 0xF0000000;
560 }
561
562 if ((++priv->rxbd_current) >= RX_BUF)
563 priv->rxbd_current = 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000564 }
565
Michal Simek3b90d0a2013-01-25 08:24:18 +0100566 return frame_len;
Michal Simek185f7d92012-09-13 20:23:34 +0000567}
568
Michal Simek6889ca72015-11-30 14:14:56 +0100569static void zynq_gem_halt(struct udevice *dev)
Michal Simek185f7d92012-09-13 20:23:34 +0000570{
Michal Simek6889ca72015-11-30 14:14:56 +0100571 struct zynq_gem_priv *priv = dev_get_priv(dev);
572 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek185f7d92012-09-13 20:23:34 +0000573
Michal Simek80243522012-10-15 14:01:23 +0200574 clrsetbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
575 ZYNQ_GEM_NWCTRL_TXEN_MASK, 0);
Michal Simek185f7d92012-09-13 20:23:34 +0000576}
577
Michal Simek6889ca72015-11-30 14:14:56 +0100578static int zynq_gem_miiphy_read(struct mii_dev *bus, int addr,
579 int devad, int reg)
Michal Simek185f7d92012-09-13 20:23:34 +0000580{
Michal Simek6889ca72015-11-30 14:14:56 +0100581 struct zynq_gem_priv *priv = bus->priv;
Michal Simek185f7d92012-09-13 20:23:34 +0000582 int ret;
Michal Simek6889ca72015-11-30 14:14:56 +0100583 u16 val;
Michal Simek185f7d92012-09-13 20:23:34 +0000584
Michal Simek6889ca72015-11-30 14:14:56 +0100585 ret = phyread(priv, addr, reg, &val);
586 debug("%s 0x%x, 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val, ret);
587 return val;
Michal Simek185f7d92012-09-13 20:23:34 +0000588}
589
Michal Simek6889ca72015-11-30 14:14:56 +0100590static int zynq_gem_miiphy_write(struct mii_dev *bus, int addr, int devad,
591 int reg, u16 value)
Michal Simek185f7d92012-09-13 20:23:34 +0000592{
Michal Simek6889ca72015-11-30 14:14:56 +0100593 struct zynq_gem_priv *priv = bus->priv;
Michal Simek185f7d92012-09-13 20:23:34 +0000594
Michal Simek6889ca72015-11-30 14:14:56 +0100595 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, value);
596 return phywrite(priv, addr, reg, value);
Michal Simek185f7d92012-09-13 20:23:34 +0000597}
598
Michal Simek6889ca72015-11-30 14:14:56 +0100599static int zynq_gem_probe(struct udevice *dev)
Michal Simek185f7d92012-09-13 20:23:34 +0000600{
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530601 void *bd_space;
Michal Simek6889ca72015-11-30 14:14:56 +0100602 struct zynq_gem_priv *priv = dev_get_priv(dev);
603 int ret;
Michal Simek185f7d92012-09-13 20:23:34 +0000604
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530605 /* Align rxbuffers to ARCH_DMA_MINALIGN */
606 priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
607 memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
608
Siva Durga Prasad Paladugu96f4f142014-12-06 12:57:53 +0530609 /* Align bd_space to MMU_SECTION_SHIFT */
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530610 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Michal Simek9ce1edc2015-04-15 13:31:28 +0200611 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space,
612 BD_SPACE, DCACHE_OFF);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530613
614 /* Initialize the bd spaces for tx and rx bd's */
615 priv->tx_bd = (struct emac_bd *)bd_space;
Prabhakar Kushwaha5b47d402015-10-25 13:18:54 +0530616 priv->rx_bd = (struct emac_bd *)((ulong)bd_space + BD_SEPRN_SPACE);
Srikanth Thokalaa5144232013-11-08 22:55:48 +0530617
Michal Simek6889ca72015-11-30 14:14:56 +0100618 priv->bus = mdio_alloc();
619 priv->bus->read = zynq_gem_miiphy_read;
620 priv->bus->write = zynq_gem_miiphy_write;
621 priv->bus->priv = priv;
622 strcpy(priv->bus->name, "gem");
Michal Simek185f7d92012-09-13 20:23:34 +0000623
Michal Simek6889ca72015-11-30 14:14:56 +0100624 ret = mdio_register(priv->bus);
Michal Simekc8e29272015-11-30 13:58:36 +0100625 if (ret)
626 return ret;
627
Michal Simek6889ca72015-11-30 14:14:56 +0100628 zynq_phy_init(dev);
629
630 return 0;
Michal Simek185f7d92012-09-13 20:23:34 +0000631}
Michal Simek6889ca72015-11-30 14:14:56 +0100632
633static int zynq_gem_remove(struct udevice *dev)
634{
635 struct zynq_gem_priv *priv = dev_get_priv(dev);
636
637 free(priv->phydev);
638 mdio_unregister(priv->bus);
639 mdio_free(priv->bus);
640
641 return 0;
642}
643
644static const struct eth_ops zynq_gem_ops = {
645 .start = zynq_gem_init,
646 .send = zynq_gem_send,
647 .recv = zynq_gem_recv,
648 .stop = zynq_gem_halt,
649 .write_hwaddr = zynq_gem_setup_mac,
650};
651
652static int zynq_gem_ofdata_to_platdata(struct udevice *dev)
653{
654 struct eth_pdata *pdata = dev_get_platdata(dev);
655 struct zynq_gem_priv *priv = dev_get_priv(dev);
656 int offset = 0;
Michal Simek3cdb1452015-11-30 14:17:50 +0100657 const char *phy_mode;
Michal Simek6889ca72015-11-30 14:14:56 +0100658
659 pdata->iobase = (phys_addr_t)dev_get_addr(dev);
660 priv->iobase = (struct zynq_gem_regs *)pdata->iobase;
661 /* Hardcode for now */
662 priv->emio = 0;
663
664 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev->of_offset,
665 "phy-handle");
666 if (offset > 0)
667 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", 0);
668
Michal Simek3cdb1452015-11-30 14:17:50 +0100669 phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
670 if (phy_mode)
671 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
672 if (pdata->phy_interface == -1) {
673 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
674 return -EINVAL;
675 }
676 priv->interface = pdata->phy_interface;
677
678 printf("ZYNQ GEM: %lx, phyaddr %d, interface %s\n", (ulong)priv->iobase,
679 priv->phyaddr, phy_string_for_interface(priv->interface));
Michal Simek6889ca72015-11-30 14:14:56 +0100680
681 return 0;
682}
683
684static const struct udevice_id zynq_gem_ids[] = {
685 { .compatible = "cdns,zynqmp-gem" },
686 { .compatible = "cdns,zynq-gem" },
687 { .compatible = "cdns,gem" },
688 { }
689};
690
691U_BOOT_DRIVER(zynq_gem) = {
692 .name = "zynq_gem",
693 .id = UCLASS_ETH,
694 .of_match = zynq_gem_ids,
695 .ofdata_to_platdata = zynq_gem_ofdata_to_platdata,
696 .probe = zynq_gem_probe,
697 .remove = zynq_gem_remove,
698 .ops = &zynq_gem_ops,
699 .priv_auto_alloc_size = sizeof(struct zynq_gem_priv),
700 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
701};