blob: cc49788012f30bd6cc90da98fae0fe6d5a9b7607 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek19dfc472012-09-13 20:23:34 +00002/*
3 * (C) Copyright 2011 Michal Simek
4 *
5 * Michal SIMEK <monstr@monstr.eu>
6 *
7 * Based on Xilinx gmac driver:
8 * (C) Copyright 2011 Xilinx
Michal Simek19dfc472012-09-13 20:23:34 +00009 */
10
Siva Durga Prasad Paladugubaa20352016-11-15 16:15:42 +053011#include <clk.h>
Michal Simek19dfc472012-09-13 20:23:34 +000012#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Michal Simek250e05e2015-11-30 14:14:56 +010014#include <dm.h>
Michal Simekc8142d42021-12-15 11:00:01 +010015#include <generic-phy.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Michal Simek19dfc472012-09-13 20:23:34 +000017#include <net.h>
Michal Simekb055f672014-04-25 14:17:38 +020018#include <netdev.h>
Michal Simek19dfc472012-09-13 20:23:34 +000019#include <config.h>
Michal Simekd9cfa972015-09-24 20:13:45 +020020#include <console.h>
Michal Simek19dfc472012-09-13 20:23:34 +000021#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Michal Simek19dfc472012-09-13 20:23:34 +000023#include <asm/io.h>
24#include <phy.h>
Michal Simekc86e7fc2021-12-06 16:25:20 +010025#include <reset.h>
Michal Simek19dfc472012-09-13 20:23:34 +000026#include <miiphy.h>
Mateusz Kulikowski93597d72016-01-23 11:54:33 +010027#include <wait_bit.h>
Michal Simek19dfc472012-09-13 20:23:34 +000028#include <watchdog.h>
Siva Durga Prasad Paladugu2b0690e2014-12-06 12:57:53 +053029#include <asm/system.h>
David Andrey73875dc2013-04-05 17:24:24 +020030#include <asm/arch/hardware.h>
Michal Simekd9f2c112012-10-15 14:01:23 +020031#include <asm/arch/sys_proto.h>
Simon Glass9bc15642020-02-03 07:36:16 -070032#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060033#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070034#include <linux/err.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090035#include <linux/errno.h>
Michal Simekb0017982022-03-30 11:07:53 +020036#include <eth_phy.h>
T Karthik Reddy4a0e6b52022-03-30 11:07:58 +020037#include <zynqmp_firmware.h>
Michal Simek19dfc472012-09-13 20:23:34 +000038
Michal Simek19dfc472012-09-13 20:23:34 +000039/* Bit/mask specification */
40#define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */
41#define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */
42#define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */
43#define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */
44#define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */
45
46#define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */
47#define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */
48#define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */
49
50#define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */
51#define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */
52#define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */
53
54/* Wrap bit, last descriptor */
55#define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000
56#define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */
Michal Simek1dc446e2015-08-17 09:58:54 +020057#define ZYNQ_GEM_TXBUF_USED_MASK 0x80000000 /* Used by Hw */
Michal Simek19dfc472012-09-13 20:23:34 +000058
Michal Simek19dfc472012-09-13 20:23:34 +000059#define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */
60#define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */
61#define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */
62#define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */
63
Siva Durga Prasad Paladugu7e7fcc32016-05-16 15:31:37 +053064#define ZYNQ_GEM_NWCFG_SPEED100 0x00000001 /* 100 Mbps operation */
65#define ZYNQ_GEM_NWCFG_SPEED1000 0x00000400 /* 1Gbps operation */
66#define ZYNQ_GEM_NWCFG_FDEN 0x00000002 /* Full Duplex mode */
67#define ZYNQ_GEM_NWCFG_FSREM 0x00020000 /* FCS removal */
Siva Durga Prasad Paladuguf6c2d202016-05-16 15:31:38 +053068#define ZYNQ_GEM_NWCFG_SGMII_ENBL 0x08000000 /* SGMII Enable */
Siva Durga Prasad Paladugu7e7fcc32016-05-16 15:31:37 +053069#define ZYNQ_GEM_NWCFG_PCS_SEL 0x00000800 /* PCS select */
Michal Simek780c5352015-09-08 17:20:01 +020070#ifdef CONFIG_ARM64
Siva Durga Prasad Paladugu7e7fcc32016-05-16 15:31:37 +053071#define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x00100000 /* Div pclk by 64, max 160MHz */
Michal Simek780c5352015-09-08 17:20:01 +020072#else
Siva Durga Prasad Paladugu7e7fcc32016-05-16 15:31:37 +053073#define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x000c0000 /* Div pclk by 48, max 120MHz */
Michal Simek780c5352015-09-08 17:20:01 +020074#endif
Michal Simek19dfc472012-09-13 20:23:34 +000075
Siva Durga Prasad Paladugu71245a42014-07-08 15:31:03 +053076#ifdef CONFIG_ARM64
77# define ZYNQ_GEM_DBUS_WIDTH (1 << 21) /* 64 bit bus */
78#else
79# define ZYNQ_GEM_DBUS_WIDTH (0 << 21) /* 32 bit bus */
80#endif
81
82#define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_DBUS_WIDTH | \
83 ZYNQ_GEM_NWCFG_FDEN | \
Michal Simek19dfc472012-09-13 20:23:34 +000084 ZYNQ_GEM_NWCFG_FSREM | \
85 ZYNQ_GEM_NWCFG_MDCCLKDIV)
86
87#define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */
88
89#define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */
90/* Use full configured addressable space (8 Kb) */
91#define ZYNQ_GEM_DMACR_RXSIZE 0x00000300
92/* Use full configured addressable space (4 Kb) */
93#define ZYNQ_GEM_DMACR_TXSIZE 0x00000400
94/* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
95#define ZYNQ_GEM_DMACR_RXBUF 0x00180000
96
Vipul Kumarcbc2ed62018-11-26 16:27:38 +053097#if defined(CONFIG_PHYS_64BIT)
98# define ZYNQ_GEM_DMA_BUS_WIDTH BIT(30) /* 64 bit bus */
99#else
100# define ZYNQ_GEM_DMA_BUS_WIDTH (0 << 30) /* 32 bit bus */
101#endif
102
Michal Simek19dfc472012-09-13 20:23:34 +0000103#define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \
104 ZYNQ_GEM_DMACR_RXSIZE | \
105 ZYNQ_GEM_DMACR_TXSIZE | \
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530106 ZYNQ_GEM_DMACR_RXBUF | \
107 ZYNQ_GEM_DMA_BUS_WIDTH)
Michal Simek19dfc472012-09-13 20:23:34 +0000108
Michal Simek975ae352015-08-17 09:57:46 +0200109#define ZYNQ_GEM_TSR_DONE 0x00000020 /* Tx done mask */
110
Siva Durga Prasad Paladugu45467002016-03-25 12:53:44 +0530111#define ZYNQ_GEM_PCS_CTL_ANEG_ENBL 0x1000
112
Siva Durga Prasad Paladugub7b36372018-11-26 16:27:39 +0530113#define ZYNQ_GEM_DCFG_DBG6_DMA_64B BIT(23)
114
Ashok Reddy Soma06250462021-11-18 13:05:24 +0100115#define MDIO_IDLE_TIMEOUT_MS 100
116
Michal Simekab72cb42013-04-22 14:41:09 +0200117/* Use MII register 1 (MII status register) to detect PHY */
118#define PHY_DETECT_REG 1
119
120/* Mask used to verify certain PHY features (or register contents)
121 * in the register above:
122 * 0x1000: 10Mbps full duplex support
123 * 0x0800: 10Mbps half duplex support
124 * 0x0008: Auto-negotiation support
125 */
126#define PHY_DETECT_MASK 0x1808
127
Stefan Roese7acfc262023-01-25 08:09:08 +0100128/* PCS (SGMII) Link Status */
129#define ZYNQ_GEM_PCSSTATUS_LINK BIT(2)
130#define ZYNQ_GEM_PCSSTATUS_ANEG_COMPL BIT(5)
131
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530132/* TX BD status masks */
133#define ZYNQ_GEM_TXBUF_FRMLEN_MASK 0x000007ff
134#define ZYNQ_GEM_TXBUF_EXHAUSTED 0x08000000
135#define ZYNQ_GEM_TXBUF_UNDERRUN 0x10000000
136
Soren Brinkmann4dded982013-11-21 13:39:01 -0800137/* Clock frequencies for different speeds */
138#define ZYNQ_GEM_FREQUENCY_10 2500000UL
139#define ZYNQ_GEM_FREQUENCY_100 25000000UL
140#define ZYNQ_GEM_FREQUENCY_1000 125000000UL
141
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700142#define RXCLK_EN BIT(0)
143
Michal Simek19dfc472012-09-13 20:23:34 +0000144/* Device registers */
145struct zynq_gem_regs {
Michal Simek74a86e82015-10-05 11:49:43 +0200146 u32 nwctrl; /* 0x0 - Network Control reg */
147 u32 nwcfg; /* 0x4 - Network Config reg */
148 u32 nwsr; /* 0x8 - Network Status reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000149 u32 reserved1;
Michal Simek74a86e82015-10-05 11:49:43 +0200150 u32 dmacr; /* 0x10 - DMA Control reg */
151 u32 txsr; /* 0x14 - TX Status reg */
152 u32 rxqbase; /* 0x18 - RX Q Base address reg */
153 u32 txqbase; /* 0x1c - TX Q Base address reg */
154 u32 rxsr; /* 0x20 - RX Status reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000155 u32 reserved2[2];
Michal Simek74a86e82015-10-05 11:49:43 +0200156 u32 idr; /* 0x2c - Interrupt Disable reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000157 u32 reserved3;
Michal Simek74a86e82015-10-05 11:49:43 +0200158 u32 phymntnc; /* 0x34 - Phy Maintaince reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000159 u32 reserved4[18];
Michal Simek74a86e82015-10-05 11:49:43 +0200160 u32 hashl; /* 0x80 - Hash Low address reg */
161 u32 hashh; /* 0x84 - Hash High address reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000162#define LADDR_LOW 0
163#define LADDR_HIGH 1
Michal Simek74a86e82015-10-05 11:49:43 +0200164 u32 laddr[4][LADDR_HIGH + 1]; /* 0x8c - Specific1 addr low/high reg */
165 u32 match[4]; /* 0xa8 - Type ID1 Match reg */
Michal Simek19dfc472012-09-13 20:23:34 +0000166 u32 reserved6[18];
Michal Simekff5dbef2015-10-05 12:49:48 +0200167#define STAT_SIZE 44
168 u32 stat[STAT_SIZE]; /* 0x100 - Octects transmitted Low reg */
Siva Durga Prasad Paladugu45467002016-03-25 12:53:44 +0530169 u32 reserved9[20];
170 u32 pcscntrl;
Stefan Roese7acfc262023-01-25 08:09:08 +0100171 u32 pcsstatus;
172 u32 rserved12[35];
Siva Durga Prasad Paladugub7b36372018-11-26 16:27:39 +0530173 u32 dcfg6; /* 0x294 Design config reg6 */
174 u32 reserved7[106];
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700175 u32 transmit_q1_ptr; /* 0x440 - Transmit priority queue 1 */
176 u32 reserved8[15];
177 u32 receive_q1_ptr; /* 0x480 - Receive priority queue 1 */
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530178 u32 reserved10[17];
179 u32 upper_txqbase; /* 0x4C8 - Upper tx_q base addr */
180 u32 reserved11[2];
181 u32 upper_rxqbase; /* 0x4D4 - Upper rx_q base addr */
Michal Simek19dfc472012-09-13 20:23:34 +0000182};
183
184/* BD descriptors */
185struct emac_bd {
186 u32 addr; /* Next descriptor pointer */
187 u32 status;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530188#if defined(CONFIG_PHYS_64BIT)
189 u32 addr_hi;
190 u32 reserved;
191#endif
Michal Simek19dfc472012-09-13 20:23:34 +0000192};
193
Michal Simekc40c93e2019-05-22 14:12:20 +0200194/* Reduce amount of BUFs if you have limited amount of memory */
Siva Durga Prasad Paladugu55931cf2015-04-15 12:15:01 +0530195#define RX_BUF 32
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530196/* Page table entries are set to 1MB, or multiples of 1MB
197 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
198 */
199#define BD_SPACE 0x100000
200/* BD separation space */
Michal Simekc6eb0bc2015-08-17 09:45:53 +0200201#define BD_SEPRN_SPACE (RX_BUF * sizeof(struct emac_bd))
Michal Simek19dfc472012-09-13 20:23:34 +0000202
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700203/* Setup the first free TX descriptor */
204#define TX_FREE_DESC 2
205
Michal Simek19dfc472012-09-13 20:23:34 +0000206/* Initialized, rxbd_current, rx_first_buf must be 0 after init */
207struct zynq_gem_priv {
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530208 struct emac_bd *tx_bd;
209 struct emac_bd *rx_bd;
210 char *rxbuffers;
Michal Simek19dfc472012-09-13 20:23:34 +0000211 u32 rxbd_current;
212 u32 rx_first_buf;
213 int phyaddr;
Michal Simeka94f84d2013-01-24 13:04:12 +0100214 int init;
Michal Simek1a63ee22015-11-30 10:24:15 +0100215 struct zynq_gem_regs *iobase;
Michal Simek55ee1862016-05-30 10:43:11 +0200216 struct zynq_gem_regs *mdiobase;
Michal Simek492de0f2015-10-07 16:42:56 +0200217 phy_interface_t interface;
Michal Simek19dfc472012-09-13 20:23:34 +0000218 struct phy_device *phydev;
Siva Durga Prasad Paladugu34a48e52018-07-16 18:25:45 +0530219 ofnode phy_of_node;
Michal Simek19dfc472012-09-13 20:23:34 +0000220 struct mii_dev *bus;
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700221 struct clk rx_clk;
222 struct clk tx_clk;
Siva Durga Prasad Paladugu0703cc52018-04-12 12:22:17 +0200223 u32 max_speed;
Siva Durga Prasad Paladugu134cfa62017-11-23 12:56:55 +0530224 bool int_pcs;
Siva Durga Prasad Paladugub7b36372018-11-26 16:27:39 +0530225 bool dma_64bit;
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700226 u32 clk_en_info;
Michal Simekc86e7fc2021-12-06 16:25:20 +0100227 struct reset_ctl_bulk resets;
Michal Simek19dfc472012-09-13 20:23:34 +0000228};
229
Michal Simek70551ca2018-06-13 10:00:30 +0200230static int phy_setup_op(struct zynq_gem_priv *priv, u32 phy_addr, u32 regnum,
Michal Simek1a63ee22015-11-30 10:24:15 +0100231 u32 op, u16 *data)
Michal Simek19dfc472012-09-13 20:23:34 +0000232{
233 u32 mgtcr;
Michal Simek55ee1862016-05-30 10:43:11 +0200234 struct zynq_gem_regs *regs = priv->mdiobase;
Michal Simeke6709652016-12-12 09:47:26 +0100235 int err;
Michal Simek19dfc472012-09-13 20:23:34 +0000236
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100237 err = wait_for_bit_le32(&regs->nwsr, ZYNQ_GEM_NWSR_MDIOIDLE_MASK,
Ashok Reddy Soma06250462021-11-18 13:05:24 +0100238 true, MDIO_IDLE_TIMEOUT_MS, false);
Michal Simeke6709652016-12-12 09:47:26 +0100239 if (err)
240 return err;
Michal Simek19dfc472012-09-13 20:23:34 +0000241
242 /* Construct mgtcr mask for the operation */
243 mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
244 (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
245 (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
246
247 /* Write mgtcr and wait for completion */
248 writel(mgtcr, &regs->phymntnc);
249
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100250 err = wait_for_bit_le32(&regs->nwsr, ZYNQ_GEM_NWSR_MDIOIDLE_MASK,
Ashok Reddy Soma06250462021-11-18 13:05:24 +0100251 true, MDIO_IDLE_TIMEOUT_MS, false);
Michal Simeke6709652016-12-12 09:47:26 +0100252 if (err)
253 return err;
Michal Simek19dfc472012-09-13 20:23:34 +0000254
255 if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
256 *data = readl(&regs->phymntnc);
257
258 return 0;
259}
260
Michal Simek70551ca2018-06-13 10:00:30 +0200261static int phyread(struct zynq_gem_priv *priv, u32 phy_addr,
Michal Simek1a63ee22015-11-30 10:24:15 +0100262 u32 regnum, u16 *val)
Michal Simek19dfc472012-09-13 20:23:34 +0000263{
Michal Simek70551ca2018-06-13 10:00:30 +0200264 int ret;
Michal Simekc919c2c2015-10-07 16:34:51 +0200265
Michal Simek1a63ee22015-11-30 10:24:15 +0100266 ret = phy_setup_op(priv, phy_addr, regnum,
267 ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
Michal Simekc919c2c2015-10-07 16:34:51 +0200268
269 if (!ret)
270 debug("%s: phy_addr %d, regnum 0x%x, val 0x%x\n", __func__,
271 phy_addr, regnum, *val);
272
273 return ret;
Michal Simek19dfc472012-09-13 20:23:34 +0000274}
275
Michal Simek70551ca2018-06-13 10:00:30 +0200276static int phywrite(struct zynq_gem_priv *priv, u32 phy_addr,
Michal Simek1a63ee22015-11-30 10:24:15 +0100277 u32 regnum, u16 data)
Michal Simek19dfc472012-09-13 20:23:34 +0000278{
Michal Simekc919c2c2015-10-07 16:34:51 +0200279 debug("%s: phy_addr %d, regnum 0x%x, data 0x%x\n", __func__, phy_addr,
280 regnum, data);
281
Michal Simek1a63ee22015-11-30 10:24:15 +0100282 return phy_setup_op(priv, phy_addr, regnum,
283 ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
Michal Simek19dfc472012-09-13 20:23:34 +0000284}
285
Michal Simek250e05e2015-11-30 14:14:56 +0100286static int zynq_gem_setup_mac(struct udevice *dev)
Michal Simek19dfc472012-09-13 20:23:34 +0000287{
288 u32 i, macaddrlow, macaddrhigh;
Simon Glassfa20e932020-12-03 16:55:20 -0700289 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simek250e05e2015-11-30 14:14:56 +0100290 struct zynq_gem_priv *priv = dev_get_priv(dev);
291 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek19dfc472012-09-13 20:23:34 +0000292
293 /* Set the MAC bits [31:0] in BOT */
Michal Simek250e05e2015-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 Simek19dfc472012-09-13 20:23:34 +0000298
299 /* Set MAC bits [47:32] in TOP */
Michal Simek250e05e2015-11-30 14:14:56 +0100300 macaddrhigh = pdata->enetaddr[4];
301 macaddrhigh |= pdata->enetaddr[5] << 8;
Michal Simek19dfc472012-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 Simek250e05e2015-11-30 14:14:56 +0100316static int zynq_phy_init(struct udevice *dev)
Michal Simek19dfc472012-09-13 20:23:34 +0000317{
Michal Simek75fbb692015-11-30 13:38:32 +0100318 int ret;
Michal Simek250e05e2015-11-30 14:14:56 +0100319 struct zynq_gem_priv *priv = dev_get_priv(dev);
Michal Simek55ee1862016-05-30 10:43:11 +0200320 struct zynq_gem_regs *regs_mdio = priv->mdiobase;
Michal Simek19dfc472012-09-13 20:23:34 +0000321 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 Simeke9ecc1c2015-11-30 13:58:36 +0100328 /* Enable only MDIO bus */
Michal Simek55ee1862016-05-30 10:43:11 +0200329 writel(ZYNQ_GEM_NWCTRL_MDEN_MASK, &regs_mdio->nwctrl);
Michal Simeke9ecc1c2015-11-30 13:58:36 +0100330
Michal Simekb0017982022-03-30 11:07:53 +0200331 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
332 priv->phyaddr = eth_phy_get_addr(dev);
333
Michal Simek7cd7ea62015-11-30 13:54:43 +0100334 priv->phydev = phy_connect(priv->bus, priv->phyaddr, dev,
335 priv->interface);
Venkatesh Yadav Abbarapu1bbe4502022-09-29 10:26:05 +0530336 if (IS_ERR_OR_NULL(priv->phydev))
Michal Simek2c68e082015-11-30 14:03:37 +0100337 return -ENODEV;
Michal Simek7cd7ea62015-11-30 13:54:43 +0100338
Siva Durga Prasad Paladugu0703cc52018-04-12 12:22:17 +0200339 if (priv->max_speed) {
340 ret = phy_set_supported(priv->phydev, priv->max_speed);
341 if (ret)
342 return ret;
343 }
344
Siva Durga Prasad Paladugu12203502019-03-27 17:39:59 +0530345 priv->phydev->supported &= supported | ADVERTISED_Pause |
346 ADVERTISED_Asym_Pause;
347
Michal Simek7cd7ea62015-11-30 13:54:43 +0100348 priv->phydev->advertising = priv->phydev->supported;
Ashok Reddy Somabea12f42022-01-14 13:08:07 +0100349 if (!ofnode_valid(priv->phydev->node))
350 priv->phydev->node = priv->phy_of_node;
Dan Murphya5828712016-05-02 15:45:57 -0500351
Michal Simek24ce2322016-05-18 14:37:23 +0200352 return phy_config(priv->phydev);
Michal Simek7cd7ea62015-11-30 13:54:43 +0100353}
354
Michal Simek250e05e2015-11-30 14:14:56 +0100355static int zynq_gem_init(struct udevice *dev)
Michal Simek7cd7ea62015-11-30 13:54:43 +0100356{
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530357 u32 i, nwconfig;
Michal Simekdbc0cfc2016-05-18 12:37:22 +0200358 int ret;
Michal Simek7cd7ea62015-11-30 13:54:43 +0100359 unsigned long clk_rate = 0;
Michal Simek250e05e2015-11-30 14:14:56 +0100360 struct zynq_gem_priv *priv = dev_get_priv(dev);
361 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek55ee1862016-05-30 10:43:11 +0200362 struct zynq_gem_regs *regs_mdio = priv->mdiobase;
Michal Simek7cd7ea62015-11-30 13:54:43 +0100363 struct emac_bd *dummy_tx_bd = &priv->tx_bd[TX_FREE_DESC];
364 struct emac_bd *dummy_rx_bd = &priv->tx_bd[TX_FREE_DESC + 2];
365
Siva Durga Prasad Paladugub7b36372018-11-26 16:27:39 +0530366 if (readl(&regs->dcfg6) & ZYNQ_GEM_DCFG_DBG6_DMA_64B)
367 priv->dma_64bit = true;
368 else
369 priv->dma_64bit = false;
370
371#if defined(CONFIG_PHYS_64BIT)
372 if (!priv->dma_64bit) {
373 printf("ERR: %s: Using 64-bit DMA but HW doesn't support it\n",
374 __func__);
375 return -EINVAL;
376 }
377#else
378 if (priv->dma_64bit)
379 debug("WARN: %s: Not using 64-bit dma even HW supports it\n",
380 __func__);
381#endif
382
Michal Simeka94f84d2013-01-24 13:04:12 +0100383 if (!priv->init) {
384 /* Disable all interrupts */
385 writel(0xFFFFFFFF, &regs->idr);
Michal Simek19dfc472012-09-13 20:23:34 +0000386
Michal Simeka94f84d2013-01-24 13:04:12 +0100387 /* Disable the receiver & transmitter */
388 writel(0, &regs->nwctrl);
389 writel(0, &regs->txsr);
390 writel(0, &regs->rxsr);
391 writel(0, &regs->phymntnc);
Michal Simek19dfc472012-09-13 20:23:34 +0000392
Michal Simeka94f84d2013-01-24 13:04:12 +0100393 /* Clear the Hash registers for the mac address
394 * pointed by AddressPtr
395 */
396 writel(0x0, &regs->hashl);
397 /* Write bits [63:32] in TOP */
398 writel(0x0, &regs->hashh);
Michal Simek19dfc472012-09-13 20:23:34 +0000399
Michal Simeka94f84d2013-01-24 13:04:12 +0100400 /* Clear all counters */
Michal Simekff5dbef2015-10-05 12:49:48 +0200401 for (i = 0; i < STAT_SIZE; i++)
Michal Simeka94f84d2013-01-24 13:04:12 +0100402 readl(&regs->stat[i]);
Michal Simek19dfc472012-09-13 20:23:34 +0000403
Michal Simeka94f84d2013-01-24 13:04:12 +0100404 /* Setup RxBD space */
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530405 memset(priv->rx_bd, 0, RX_BUF * sizeof(struct emac_bd));
Michal Simek19dfc472012-09-13 20:23:34 +0000406
Michal Simeka94f84d2013-01-24 13:04:12 +0100407 for (i = 0; i < RX_BUF; i++) {
408 priv->rx_bd[i].status = 0xF0000000;
409 priv->rx_bd[i].addr =
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530410 (lower_32_bits((ulong)(priv->rxbuffers)
411 + (i * PKTSIZE_ALIGN)));
412#if defined(CONFIG_PHYS_64BIT)
413 priv->rx_bd[i].addr_hi =
414 (upper_32_bits((ulong)(priv->rxbuffers)
415 + (i * PKTSIZE_ALIGN)));
416#endif
417 }
Michal Simeka94f84d2013-01-24 13:04:12 +0100418 /* WRAP bit to last BD */
419 priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
420 /* Write RxBDs to IP */
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530421 writel(lower_32_bits((ulong)priv->rx_bd), &regs->rxqbase);
422#if defined(CONFIG_PHYS_64BIT)
423 writel(upper_32_bits((ulong)priv->rx_bd), &regs->upper_rxqbase);
424#endif
Michal Simek19dfc472012-09-13 20:23:34 +0000425
Michal Simeka94f84d2013-01-24 13:04:12 +0100426 /* Setup for DMA Configuration register */
427 writel(ZYNQ_GEM_DMACR_INIT, &regs->dmacr);
Michal Simek19dfc472012-09-13 20:23:34 +0000428
Michal Simeka94f84d2013-01-24 13:04:12 +0100429 /* Setup for Network Control register, MDIO, Rx and Tx enable */
Michal Simek55ee1862016-05-30 10:43:11 +0200430 setbits_le32(&regs_mdio->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK);
Michal Simek19dfc472012-09-13 20:23:34 +0000431
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700432 /* Disable the second priority queue */
433 dummy_tx_bd->addr = 0;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530434#if defined(CONFIG_PHYS_64BIT)
435 dummy_tx_bd->addr_hi = 0;
436#endif
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700437 dummy_tx_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
438 ZYNQ_GEM_TXBUF_LAST_MASK|
439 ZYNQ_GEM_TXBUF_USED_MASK;
440
441 dummy_rx_bd->addr = ZYNQ_GEM_RXBUF_WRAP_MASK |
442 ZYNQ_GEM_RXBUF_NEW_MASK;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530443#if defined(CONFIG_PHYS_64BIT)
444 dummy_rx_bd->addr_hi = 0;
445#endif
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700446 dummy_rx_bd->status = 0;
Edgar E. Iglesias23045112015-09-25 23:50:07 -0700447
448 writel((ulong)dummy_tx_bd, &regs->transmit_q1_ptr);
449 writel((ulong)dummy_rx_bd, &regs->receive_q1_ptr);
450
Michal Simeka94f84d2013-01-24 13:04:12 +0100451 priv->init++;
452 }
453
Michal Simekdbc0cfc2016-05-18 12:37:22 +0200454 ret = phy_startup(priv->phydev);
455 if (ret)
456 return ret;
Michal Simek19dfc472012-09-13 20:23:34 +0000457
Michal Simek43b38322015-11-30 13:44:49 +0100458 if (!priv->phydev->link) {
459 printf("%s: No link.\n", priv->phydev->dev->name);
Michal Simek216b96d2013-11-12 14:25:29 +0100460 return -1;
461 }
462
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530463 nwconfig = ZYNQ_GEM_NWCFG_INIT;
464
Siva Durga Prasad Paladugu134cfa62017-11-23 12:56:55 +0530465 /*
466 * Set SGMII enable PCS selection only if internal PCS/PMA
467 * core is used and interface is SGMII.
468 */
469 if (priv->interface == PHY_INTERFACE_MODE_SGMII &&
470 priv->int_pcs) {
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530471 nwconfig |= ZYNQ_GEM_NWCFG_SGMII_ENBL |
472 ZYNQ_GEM_NWCFG_PCS_SEL;
Siva Durga Prasad Paladugu45467002016-03-25 12:53:44 +0530473 }
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530474
Michal Simek43b38322015-11-30 13:44:49 +0100475 switch (priv->phydev->speed) {
Michal Simekd9f2c112012-10-15 14:01:23 +0200476 case SPEED_1000:
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530477 writel(nwconfig | ZYNQ_GEM_NWCFG_SPEED1000,
Michal Simekd9f2c112012-10-15 14:01:23 +0200478 &regs->nwcfg);
Soren Brinkmann4dded982013-11-21 13:39:01 -0800479 clk_rate = ZYNQ_GEM_FREQUENCY_1000;
Michal Simekd9f2c112012-10-15 14:01:23 +0200480 break;
481 case SPEED_100:
Siva Durga Prasad Paladugu65d3f3a2016-02-05 13:22:11 +0530482 writel(nwconfig | ZYNQ_GEM_NWCFG_SPEED100,
Michal Simek64295952015-09-08 16:55:42 +0200483 &regs->nwcfg);
Soren Brinkmann4dded982013-11-21 13:39:01 -0800484 clk_rate = ZYNQ_GEM_FREQUENCY_100;
Michal Simekd9f2c112012-10-15 14:01:23 +0200485 break;
486 case SPEED_10:
Soren Brinkmann4dded982013-11-21 13:39:01 -0800487 clk_rate = ZYNQ_GEM_FREQUENCY_10;
Michal Simekd9f2c112012-10-15 14:01:23 +0200488 break;
489 }
Robert Hancock3d6a9e02021-03-11 16:55:50 -0600490
491#ifdef CONFIG_ARM64
492 if (priv->interface == PHY_INTERFACE_MODE_SGMII &&
493 priv->int_pcs) {
494 /*
495 * Disable AN for fixed link configuration, enable otherwise.
496 * Must be written after PCS_SEL is set in nwconfig,
497 * otherwise writes will not take effect.
498 */
Stefan Roese7acfc262023-01-25 08:09:08 +0100499 if (priv->phydev->phy_id != PHY_FIXED_ID) {
Robert Hancock3d6a9e02021-03-11 16:55:50 -0600500 writel(readl(&regs->pcscntrl) | ZYNQ_GEM_PCS_CTL_ANEG_ENBL,
501 &regs->pcscntrl);
Stefan Roese7acfc262023-01-25 08:09:08 +0100502 /*
503 * When the PHY link is already up, the PCS link needs
504 * to get re-checked
505 */
506 if (priv->phydev->link) {
507 u32 pcsstatus;
508
509 pcsstatus = ZYNQ_GEM_PCSSTATUS_LINK |
510 ZYNQ_GEM_PCSSTATUS_ANEG_COMPL;
511 ret = wait_for_bit_le32(&regs->pcsstatus,
512 pcsstatus,
513 true, 5000, true);
514 if (ret) {
515 dev_warn(dev,
516 "no PCS (SGMII) link\n");
517 } else {
518 /*
519 * Some additional minimal delay seems
520 * to be needed so that the first
521 * packet will be sent correctly
522 */
523 mdelay(1);
524 }
525 }
526 } else {
Robert Hancock3d6a9e02021-03-11 16:55:50 -0600527 writel(readl(&regs->pcscntrl) & ~ZYNQ_GEM_PCS_CTL_ANEG_ENBL,
528 &regs->pcscntrl);
Stefan Roese7acfc262023-01-25 08:09:08 +0100529 }
Robert Hancock3d6a9e02021-03-11 16:55:50 -0600530 }
531#endif
David Andrey73875dc2013-04-05 17:24:24 +0200532
Michal Simeka170e432022-08-26 10:30:47 +0200533 ret = clk_get_rate(&priv->tx_clk);
534 if (ret != clk_rate) {
535 ret = clk_set_rate(&priv->tx_clk, clk_rate);
536 if (IS_ERR_VALUE(ret)) {
537 dev_err(dev, "failed to set tx clock rate %ld\n", clk_rate);
538 return ret;
539 }
Stefan Herbrechtsmeierbb433972017-01-17 16:27:25 +0100540 }
541
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700542 ret = clk_enable(&priv->tx_clk);
Michal Simek41710952021-02-09 15:28:15 +0100543 if (ret) {
Stefan Herbrechtsmeierbb433972017-01-17 16:27:25 +0100544 dev_err(dev, "failed to enable tx clock\n");
545 return ret;
546 }
Michal Simekd9f2c112012-10-15 14:01:23 +0200547
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700548 if (priv->clk_en_info & RXCLK_EN) {
549 ret = clk_enable(&priv->rx_clk);
550 if (ret) {
551 dev_err(dev, "failed to enable rx clock\n");
552 return ret;
553 }
554 }
Michal Simekd9f2c112012-10-15 14:01:23 +0200555 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
556 ZYNQ_GEM_NWCTRL_TXEN_MASK);
557
Michal Simek19dfc472012-09-13 20:23:34 +0000558 return 0;
559}
560
Michal Simek250e05e2015-11-30 14:14:56 +0100561static int zynq_gem_send(struct udevice *dev, void *ptr, int len)
Michal Simek19dfc472012-09-13 20:23:34 +0000562{
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530563 dma_addr_t addr;
564 u32 size;
Michal Simek250e05e2015-11-30 14:14:56 +0100565 struct zynq_gem_priv *priv = dev_get_priv(dev);
566 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek1dc446e2015-08-17 09:58:54 +0200567 struct emac_bd *current_bd = &priv->tx_bd[1];
Michal Simek19dfc472012-09-13 20:23:34 +0000568
Michal Simek19dfc472012-09-13 20:23:34 +0000569 /* Setup Tx BD */
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530570 memset(priv->tx_bd, 0, sizeof(struct emac_bd));
Michal Simek19dfc472012-09-13 20:23:34 +0000571
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530572 priv->tx_bd->addr = lower_32_bits((ulong)ptr);
573#if defined(CONFIG_PHYS_64BIT)
574 priv->tx_bd->addr_hi = upper_32_bits((ulong)ptr);
575#endif
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530576 priv->tx_bd->status = (len & ZYNQ_GEM_TXBUF_FRMLEN_MASK) |
Michal Simek1dc446e2015-08-17 09:58:54 +0200577 ZYNQ_GEM_TXBUF_LAST_MASK;
578 /* Dummy descriptor to mark it as the last in descriptor chain */
579 current_bd->addr = 0x0;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530580#if defined(CONFIG_PHYS_64BIT)
581 current_bd->addr_hi = 0x0;
582#endif
Michal Simek1dc446e2015-08-17 09:58:54 +0200583 current_bd->status = ZYNQ_GEM_TXBUF_WRAP_MASK |
584 ZYNQ_GEM_TXBUF_LAST_MASK|
585 ZYNQ_GEM_TXBUF_USED_MASK;
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530586
Michal Simekb6fe7ad2015-08-17 09:50:09 +0200587 /* setup BD */
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530588 writel(lower_32_bits((ulong)priv->tx_bd), &regs->txqbase);
589#if defined(CONFIG_PHYS_64BIT)
590 writel(upper_32_bits((ulong)priv->tx_bd), &regs->upper_txqbase);
591#endif
Michal Simekb6fe7ad2015-08-17 09:50:09 +0200592
Prabhakar Kushwaha1e9e6192015-10-25 13:18:54 +0530593 addr = (ulong) ptr;
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530594 addr &= ~(ARCH_DMA_MINALIGN - 1);
595 size = roundup(len, ARCH_DMA_MINALIGN);
596 flush_dcache_range(addr, addr + size);
597 barrier();
Michal Simek19dfc472012-09-13 20:23:34 +0000598
599 /* Start transmit */
600 setbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
601
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530602 /* Read TX BD status */
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530603 if (priv->tx_bd->status & ZYNQ_GEM_TXBUF_EXHAUSTED)
604 printf("TX buffers exhausted in mid frame\n");
Michal Simek19dfc472012-09-13 20:23:34 +0000605
Álvaro Fernåndez Rojas918de032018-01-23 17:14:55 +0100606 return wait_for_bit_le32(&regs->txsr, ZYNQ_GEM_TSR_DONE,
607 true, 20000, true);
Michal Simek19dfc472012-09-13 20:23:34 +0000608}
609
610/* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
Michal Simek250e05e2015-11-30 14:14:56 +0100611static int zynq_gem_recv(struct udevice *dev, int flags, uchar **packetp)
Michal Simek19dfc472012-09-13 20:23:34 +0000612{
613 int frame_len;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530614 dma_addr_t addr;
Michal Simek250e05e2015-11-30 14:14:56 +0100615 struct zynq_gem_priv *priv = dev_get_priv(dev);
Michal Simek19dfc472012-09-13 20:23:34 +0000616 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
Michal Simek19dfc472012-09-13 20:23:34 +0000617
618 if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
Michal Simek57b02692015-12-09 14:26:48 +0100619 return -1;
Michal Simek19dfc472012-09-13 20:23:34 +0000620
621 if (!(current_bd->status &
622 (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
623 printf("GEM: SOF or EOF not set for last buffer received!\n");
Michal Simek57b02692015-12-09 14:26:48 +0100624 return -1;
Michal Simek19dfc472012-09-13 20:23:34 +0000625 }
626
627 frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
Michal Simek57b02692015-12-09 14:26:48 +0100628 if (!frame_len) {
629 printf("%s: Zero size packet?\n", __func__);
630 return -1;
631 }
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530632
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530633#if defined(CONFIG_PHYS_64BIT)
634 addr = (dma_addr_t)((current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK)
635 | ((dma_addr_t)current_bd->addr_hi << 32));
636#else
Michal Simek57b02692015-12-09 14:26:48 +0100637 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530638#endif
Michal Simek57b02692015-12-09 14:26:48 +0100639 addr &= ~(ARCH_DMA_MINALIGN - 1);
Vipul Kumarcbc2ed62018-11-26 16:27:38 +0530640
Michal Simek57b02692015-12-09 14:26:48 +0100641 *packetp = (uchar *)(uintptr_t)addr;
Michal Simek19dfc472012-09-13 20:23:34 +0000642
Stefan Theil0f407c92018-12-17 09:12:30 +0100643 invalidate_dcache_range(addr, addr + roundup(PKTSIZE_ALIGN, ARCH_DMA_MINALIGN));
644 barrier();
645
Michal Simek57b02692015-12-09 14:26:48 +0100646 return frame_len;
647}
648
649static int zynq_gem_free_pkt(struct udevice *dev, uchar *packet, int length)
650{
651 struct zynq_gem_priv *priv = dev_get_priv(dev);
652 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
653 struct emac_bd *first_bd;
Ashok Reddy Soma47572532020-02-23 08:01:29 -0700654 dma_addr_t addr;
Michal Simek19dfc472012-09-13 20:23:34 +0000655
Michal Simek57b02692015-12-09 14:26:48 +0100656 if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK) {
657 priv->rx_first_buf = priv->rxbd_current;
658 } else {
659 current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
660 current_bd->status = 0xF0000000; /* FIXME */
661 }
Michal Simek19dfc472012-09-13 20:23:34 +0000662
Michal Simek57b02692015-12-09 14:26:48 +0100663 if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
664 first_bd = &priv->rx_bd[priv->rx_first_buf];
665 first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
666 first_bd->status = 0xF0000000;
Michal Simek19dfc472012-09-13 20:23:34 +0000667 }
668
Ashok Reddy Soma47572532020-02-23 08:01:29 -0700669 /* Flush the cache for the packet as well */
670#if defined(CONFIG_PHYS_64BIT)
671 addr = (dma_addr_t)((current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK)
672 | ((dma_addr_t)current_bd->addr_hi << 32));
673#else
674 addr = current_bd->addr & ZYNQ_GEM_RXBUF_ADD_MASK;
675#endif
676 flush_dcache_range(addr, addr + roundup(PKTSIZE_ALIGN,
677 ARCH_DMA_MINALIGN));
678 barrier();
679
Michal Simek57b02692015-12-09 14:26:48 +0100680 if ((++priv->rxbd_current) >= RX_BUF)
681 priv->rxbd_current = 0;
682
Michal Simek139f4102015-12-09 14:16:32 +0100683 return 0;
Michal Simek19dfc472012-09-13 20:23:34 +0000684}
685
Michal Simek250e05e2015-11-30 14:14:56 +0100686static void zynq_gem_halt(struct udevice *dev)
Michal Simek19dfc472012-09-13 20:23:34 +0000687{
Michal Simek250e05e2015-11-30 14:14:56 +0100688 struct zynq_gem_priv *priv = dev_get_priv(dev);
689 struct zynq_gem_regs *regs = priv->iobase;
Michal Simek19dfc472012-09-13 20:23:34 +0000690
Michal Simekd9f2c112012-10-15 14:01:23 +0200691 clrsetbits_le32(&regs->nwctrl, ZYNQ_GEM_NWCTRL_RXEN_MASK |
692 ZYNQ_GEM_NWCTRL_TXEN_MASK, 0);
Michal Simek19dfc472012-09-13 20:23:34 +0000693}
694
Michal Simek250e05e2015-11-30 14:14:56 +0100695static int zynq_gem_miiphy_read(struct mii_dev *bus, int addr,
696 int devad, int reg)
Michal Simek19dfc472012-09-13 20:23:34 +0000697{
Michal Simek250e05e2015-11-30 14:14:56 +0100698 struct zynq_gem_priv *priv = bus->priv;
Michal Simek19dfc472012-09-13 20:23:34 +0000699 int ret;
Michal Simekd061bfd2018-06-14 09:08:44 +0200700 u16 val = 0;
Michal Simek19dfc472012-09-13 20:23:34 +0000701
Michal Simek250e05e2015-11-30 14:14:56 +0100702 ret = phyread(priv, addr, reg, &val);
703 debug("%s 0x%x, 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val, ret);
704 return val;
Michal Simek19dfc472012-09-13 20:23:34 +0000705}
706
Michal Simek250e05e2015-11-30 14:14:56 +0100707static int zynq_gem_miiphy_write(struct mii_dev *bus, int addr, int devad,
708 int reg, u16 value)
Michal Simek19dfc472012-09-13 20:23:34 +0000709{
Michal Simek250e05e2015-11-30 14:14:56 +0100710 struct zynq_gem_priv *priv = bus->priv;
Michal Simek19dfc472012-09-13 20:23:34 +0000711
Michal Simek250e05e2015-11-30 14:14:56 +0100712 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, value);
713 return phywrite(priv, addr, reg, value);
Michal Simek19dfc472012-09-13 20:23:34 +0000714}
715
Michal Simekc86e7fc2021-12-06 16:25:20 +0100716static int zynq_gem_reset_init(struct udevice *dev)
717{
718 struct zynq_gem_priv *priv = dev_get_priv(dev);
719 int ret;
720
721 ret = reset_get_bulk(dev, &priv->resets);
722 if (ret == -ENOTSUPP || ret == -ENOENT)
723 return 0;
724 else if (ret)
725 return ret;
726
727 ret = reset_deassert_bulk(&priv->resets);
728 if (ret) {
729 reset_release_bulk(&priv->resets);
730 return ret;
731 }
732
733 return 0;
734}
735
T Karthik Reddy4a0e6b52022-03-30 11:07:58 +0200736static int gem_zynqmp_set_dynamic_config(struct udevice *dev)
737{
738 u32 pm_info[2];
739 int ret;
740
741 if (IS_ENABLED(CONFIG_ARCH_ZYNQMP)) {
742 if (!zynqmp_pm_is_function_supported(PM_IOCTL,
743 IOCTL_SET_GEM_CONFIG)) {
744 ret = ofnode_read_u32_array(dev_ofnode(dev),
745 "power-domains",
746 pm_info,
747 ARRAY_SIZE(pm_info));
748 if (ret) {
749 dev_err(dev,
750 "Failed to read power-domains info\n");
751 return ret;
752 }
753
754 ret = zynqmp_pm_set_gem_config(pm_info[1],
755 GEM_CONFIG_FIXED, 0);
756 if (ret)
757 return ret;
758
759 ret = zynqmp_pm_set_gem_config(pm_info[1],
760 GEM_CONFIG_SGMII_MODE,
761 1);
762 if (ret)
763 return ret;
764 }
765 }
766
767 return 0;
768}
769
Michal Simek250e05e2015-11-30 14:14:56 +0100770static int zynq_gem_probe(struct udevice *dev)
Michal Simek19dfc472012-09-13 20:23:34 +0000771{
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530772 void *bd_space;
Michal Simek250e05e2015-11-30 14:14:56 +0100773 struct zynq_gem_priv *priv = dev_get_priv(dev);
774 int ret;
Michal Simekc8142d42021-12-15 11:00:01 +0100775 struct phy phy;
776
777 if (priv->interface == PHY_INTERFACE_MODE_SGMII) {
778 ret = generic_phy_get_by_index(dev, 0, &phy);
779 if (!ret) {
780 ret = generic_phy_init(&phy);
781 if (ret)
782 return ret;
783 } else if (ret != -ENOENT) {
784 debug("could not get phy (err %d)\n", ret);
785 return ret;
786 }
787 }
Michal Simek19dfc472012-09-13 20:23:34 +0000788
Michal Simekc86e7fc2021-12-06 16:25:20 +0100789 ret = zynq_gem_reset_init(dev);
790 if (ret)
791 return ret;
792
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530793 /* Align rxbuffers to ARCH_DMA_MINALIGN */
794 priv->rxbuffers = memalign(ARCH_DMA_MINALIGN, RX_BUF * PKTSIZE_ALIGN);
Michal Simekc8959f42018-06-13 15:20:35 +0200795 if (!priv->rxbuffers)
796 return -ENOMEM;
797
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530798 memset(priv->rxbuffers, 0, RX_BUF * PKTSIZE_ALIGN);
T Karthik Reddy60bf2162020-01-15 02:15:13 -0700799 ulong addr = (ulong)priv->rxbuffers;
Stefan Theil0f407c92018-12-17 09:12:30 +0100800 flush_dcache_range(addr, addr + roundup(RX_BUF * PKTSIZE_ALIGN, ARCH_DMA_MINALIGN));
801 barrier();
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530802
Siva Durga Prasad Paladugu2b0690e2014-12-06 12:57:53 +0530803 /* Align bd_space to MMU_SECTION_SHIFT */
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530804 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Michal Simek049c65b2020-02-06 14:36:46 +0100805 if (!bd_space) {
806 ret = -ENOMEM;
807 goto err1;
808 }
Michal Simekc8959f42018-06-13 15:20:35 +0200809
Michal Simek0afb6b22015-04-15 13:31:28 +0200810 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space,
811 BD_SPACE, DCACHE_OFF);
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530812
813 /* Initialize the bd spaces for tx and rx bd's */
814 priv->tx_bd = (struct emac_bd *)bd_space;
Prabhakar Kushwaha1e9e6192015-10-25 13:18:54 +0530815 priv->rx_bd = (struct emac_bd *)((ulong)bd_space + BD_SEPRN_SPACE);
Srikanth Thokalacbf20b22013-11-08 22:55:48 +0530816
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700817 ret = clk_get_by_name(dev, "tx_clk", &priv->tx_clk);
Siva Durga Prasad Paladugubaa20352016-11-15 16:15:42 +0530818 if (ret < 0) {
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700819 dev_err(dev, "failed to get tx_clock\n");
Michal Simek179f7d72021-02-11 19:03:30 +0100820 goto err2;
Siva Durga Prasad Paladugubaa20352016-11-15 16:15:42 +0530821 }
Siva Durga Prasad Paladugubaa20352016-11-15 16:15:42 +0530822
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700823 if (priv->clk_en_info & RXCLK_EN) {
824 ret = clk_get_by_name(dev, "rx_clk", &priv->rx_clk);
825 if (ret < 0) {
826 dev_err(dev, "failed to get rx_clock\n");
Michal Simek179f7d72021-02-11 19:03:30 +0100827 goto err2;
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700828 }
829 }
830
Michal Simekb0017982022-03-30 11:07:53 +0200831 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
832 priv->bus = eth_phy_get_mdio_bus(dev);
Michal Simek19dfc472012-09-13 20:23:34 +0000833
Michal Simekb0017982022-03-30 11:07:53 +0200834 if (!priv->bus) {
835 priv->bus = mdio_alloc();
836 priv->bus->read = zynq_gem_miiphy_read;
837 priv->bus->write = zynq_gem_miiphy_write;
838 priv->bus->priv = priv;
839
840 ret = mdio_register_seq(priv->bus, dev_seq(dev));
841 if (ret)
842 goto err2;
843 }
844
845 if (IS_ENABLED(CONFIG_DM_ETH_PHY))
846 eth_phy_set_mdio_bus(dev, priv->bus);
Michal Simek049c65b2020-02-06 14:36:46 +0100847
848 ret = zynq_phy_init(dev);
849 if (ret)
Michael Walle465437c2021-02-10 22:41:57 +0100850 goto err3;
Michal Simek049c65b2020-02-06 14:36:46 +0100851
Michal Simekc8142d42021-12-15 11:00:01 +0100852 if (priv->interface == PHY_INTERFACE_MODE_SGMII && phy.dev) {
T Karthik Reddy4a0e6b52022-03-30 11:07:58 +0200853 if (IS_ENABLED(CONFIG_DM_ETH_PHY)) {
Michal Simek347d8252022-12-09 16:19:29 +0100854 if (device_is_compatible(dev, "cdns,zynqmp-gem") ||
855 device_is_compatible(dev, "xlnx,zynqmp-gem")) {
T Karthik Reddy4a0e6b52022-03-30 11:07:58 +0200856 ret = gem_zynqmp_set_dynamic_config(dev);
857 if (ret) {
858 dev_err
859 (dev,
860 "Failed to set gem dynamic config\n");
861 return ret;
862 }
863 }
864 }
Michal Simekc8142d42021-12-15 11:00:01 +0100865 ret = generic_phy_power_on(&phy);
866 if (ret)
867 return ret;
868 }
869
T Karthik Reddy297521e2022-03-30 11:07:55 +0200870 printf("\nZYNQ GEM: %lx, mdio bus %lx, phyaddr %d, interface %s\n",
871 (ulong)priv->iobase, (ulong)priv->mdiobase, priv->phydev->addr,
872 phy_string_for_interface(priv->interface));
873
Michal Simek049c65b2020-02-06 14:36:46 +0100874 return ret;
Michal Simek19dfc472012-09-13 20:23:34 +0000875
Michael Walle465437c2021-02-10 22:41:57 +0100876err3:
877 mdio_unregister(priv->bus);
Michal Simek049c65b2020-02-06 14:36:46 +0100878err2:
Michal Simek049c65b2020-02-06 14:36:46 +0100879 free(priv->tx_bd);
Michal Simek179f7d72021-02-11 19:03:30 +0100880err1:
881 free(priv->rxbuffers);
Michal Simek049c65b2020-02-06 14:36:46 +0100882 return ret;
Michal Simek250e05e2015-11-30 14:14:56 +0100883}
Michal Simek19dfc472012-09-13 20:23:34 +0000884
Michal Simek250e05e2015-11-30 14:14:56 +0100885static int zynq_gem_remove(struct udevice *dev)
886{
887 struct zynq_gem_priv *priv = dev_get_priv(dev);
Michal Simek19dfc472012-09-13 20:23:34 +0000888
Michal Simek250e05e2015-11-30 14:14:56 +0100889 free(priv->phydev);
890 mdio_unregister(priv->bus);
891 mdio_free(priv->bus);
Michal Simek19dfc472012-09-13 20:23:34 +0000892
Michal Simek250e05e2015-11-30 14:14:56 +0100893 return 0;
894}
895
896static const struct eth_ops zynq_gem_ops = {
897 .start = zynq_gem_init,
898 .send = zynq_gem_send,
899 .recv = zynq_gem_recv,
Michal Simek57b02692015-12-09 14:26:48 +0100900 .free_pkt = zynq_gem_free_pkt,
Michal Simek250e05e2015-11-30 14:14:56 +0100901 .stop = zynq_gem_halt,
902 .write_hwaddr = zynq_gem_setup_mac,
903};
Michal Simeke9ecc1c2015-11-30 13:58:36 +0100904
Simon Glassaad29ae2020-12-03 16:55:21 -0700905static int zynq_gem_of_to_plat(struct udevice *dev)
Michal Simek250e05e2015-11-30 14:14:56 +0100906{
Simon Glassfa20e932020-12-03 16:55:20 -0700907 struct eth_pdata *pdata = dev_get_plat(dev);
Michal Simek250e05e2015-11-30 14:14:56 +0100908 struct zynq_gem_priv *priv = dev_get_priv(dev);
Siva Durga Prasad Paladugu34a48e52018-07-16 18:25:45 +0530909 struct ofnode_phandle_args phandle_args;
Michal Simek250e05e2015-11-30 14:14:56 +0100910
Siva Durga Prasad Paladugu34a48e52018-07-16 18:25:45 +0530911 pdata->iobase = (phys_addr_t)dev_read_addr(dev);
Michal Simek250e05e2015-11-30 14:14:56 +0100912 priv->iobase = (struct zynq_gem_regs *)pdata->iobase;
Michal Simek55ee1862016-05-30 10:43:11 +0200913 priv->mdiobase = priv->iobase;
Michal Simek250e05e2015-11-30 14:14:56 +0100914 /* Hardcode for now */
Michal Simekc6aa4132015-12-09 09:29:12 +0100915 priv->phyaddr = -1;
Michal Simek250e05e2015-11-30 14:14:56 +0100916
Michal Simek81145382018-09-20 09:42:27 +0200917 if (!dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
918 &phandle_args)) {
Michal Simek8ec90662016-05-30 10:43:11 +0200919 fdt_addr_t addr;
920 ofnode parent;
921
Michal Simek81145382018-09-20 09:42:27 +0200922 debug("phy-handle does exist %s\n", dev->name);
Michal Simekb0017982022-03-30 11:07:53 +0200923 if (!(IS_ENABLED(CONFIG_DM_ETH_PHY)))
924 priv->phyaddr = ofnode_read_u32_default
925 (phandle_args.node, "reg", -1);
926
Michal Simek81145382018-09-20 09:42:27 +0200927 priv->phy_of_node = phandle_args.node;
928 priv->max_speed = ofnode_read_u32_default(phandle_args.node,
929 "max-speed",
930 SPEED_1000);
Michal Simek8ec90662016-05-30 10:43:11 +0200931
932 parent = ofnode_get_parent(phandle_args.node);
Michal Simekeac3c672021-12-06 14:53:17 +0100933 if (ofnode_name_eq(parent, "mdio"))
934 parent = ofnode_get_parent(parent);
935
Michal Simek8ec90662016-05-30 10:43:11 +0200936 addr = ofnode_get_addr(parent);
937 if (addr != FDT_ADDR_T_NONE) {
938 debug("MDIO bus not found %s\n", dev->name);
939 priv->mdiobase = (struct zynq_gem_regs *)addr;
940 }
Siva Durga Prasad Paladugu34a48e52018-07-16 18:25:45 +0530941 }
Michal Simek250e05e2015-11-30 14:14:56 +0100942
Marek BehĂșnbc194772022-04-07 00:33:01 +0200943 pdata->phy_interface = dev_read_phy_mode(dev);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200944 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Michal Simek3c4ce3c2015-11-30 14:17:50 +0100945 return -EINVAL;
Michal Simek3c4ce3c2015-11-30 14:17:50 +0100946 priv->interface = pdata->phy_interface;
947
Siva Durga Prasad Paladugu34a48e52018-07-16 18:25:45 +0530948 priv->int_pcs = dev_read_bool(dev, "is-internal-pcspma");
Siva Durga Prasad Paladugu134cfa62017-11-23 12:56:55 +0530949
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700950 priv->clk_en_info = dev_get_driver_data(dev);
951
Michal Simek250e05e2015-11-30 14:14:56 +0100952 return 0;
Michal Simek19dfc472012-09-13 20:23:34 +0000953}
Michal Simek250e05e2015-11-30 14:14:56 +0100954
955static const struct udevice_id zynq_gem_ids[] = {
Michal Simek347d8252022-12-09 16:19:29 +0100956 { .compatible = "xlnx,versal-gem", .data = RXCLK_EN },
T Karthik Reddy68cd67d2021-02-03 03:10:48 -0700957 { .compatible = "cdns,versal-gem", .data = RXCLK_EN },
Michal Simek347d8252022-12-09 16:19:29 +0100958 { .compatible = "xlnx,zynqmp-gem" },
Michal Simek250e05e2015-11-30 14:14:56 +0100959 { .compatible = "cdns,zynqmp-gem" },
Michal Simek347d8252022-12-09 16:19:29 +0100960 { .compatible = "xlnx,zynq-gem" },
Michal Simek250e05e2015-11-30 14:14:56 +0100961 { .compatible = "cdns,zynq-gem" },
962 { .compatible = "cdns,gem" },
963 { }
964};
965
966U_BOOT_DRIVER(zynq_gem) = {
967 .name = "zynq_gem",
968 .id = UCLASS_ETH,
969 .of_match = zynq_gem_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700970 .of_to_plat = zynq_gem_of_to_plat,
Michal Simek250e05e2015-11-30 14:14:56 +0100971 .probe = zynq_gem_probe,
972 .remove = zynq_gem_remove,
973 .ops = &zynq_gem_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700974 .priv_auto = sizeof(struct zynq_gem_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700975 .plat_auto = sizeof(struct eth_pdata),
Michal Simek250e05e2015-11-30 14:14:56 +0100976};