2 * (C) Copyright 2011 Michal Simek
4 * Michal SIMEK <monstr@monstr.eu>
6 * Based on Xilinx gmac driver:
7 * (C) Copyright 2011 Xilinx
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 #if !defined(CONFIG_PHYLIB)
38 # error XILINX_GEM_ETHERNET requires PHYLIB
41 /* Bit/mask specification */
42 #define ZYNQ_GEM_PHYMNTNC_OP_MASK 0x40020000 /* operation mask bits */
43 #define ZYNQ_GEM_PHYMNTNC_OP_R_MASK 0x20000000 /* read operation */
44 #define ZYNQ_GEM_PHYMNTNC_OP_W_MASK 0x10000000 /* write operation */
45 #define ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK 23 /* Shift bits for PHYAD */
46 #define ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK 18 /* Shift bits for PHREG */
48 #define ZYNQ_GEM_RXBUF_EOF_MASK 0x00008000 /* End of frame. */
49 #define ZYNQ_GEM_RXBUF_SOF_MASK 0x00004000 /* Start of frame. */
50 #define ZYNQ_GEM_RXBUF_LEN_MASK 0x00003FFF /* Mask for length field */
52 #define ZYNQ_GEM_RXBUF_WRAP_MASK 0x00000002 /* Wrap bit, last BD */
53 #define ZYNQ_GEM_RXBUF_NEW_MASK 0x00000001 /* Used bit.. */
54 #define ZYNQ_GEM_RXBUF_ADD_MASK 0xFFFFFFFC /* Mask for address */
56 /* Wrap bit, last descriptor */
57 #define ZYNQ_GEM_TXBUF_WRAP_MASK 0x40000000
58 #define ZYNQ_GEM_TXBUF_LAST_MASK 0x00008000 /* Last buffer */
60 #define ZYNQ_GEM_TXSR_HRESPNOK_MASK 0x00000100 /* Transmit hresp not OK */
61 #define ZYNQ_GEM_TXSR_URUN_MASK 0x00000040 /* Transmit underrun */
62 /* Transmit buffs exhausted mid frame */
63 #define ZYNQ_GEM_TXSR_BUFEXH_MASK 0x00000010
65 #define ZYNQ_GEM_NWCTRL_TXEN_MASK 0x00000008 /* Enable transmit */
66 #define ZYNQ_GEM_NWCTRL_RXEN_MASK 0x00000004 /* Enable receive */
67 #define ZYNQ_GEM_NWCTRL_MDEN_MASK 0x00000010 /* Enable MDIO port */
68 #define ZYNQ_GEM_NWCTRL_STARTTX_MASK 0x00000200 /* Start tx (tx_go) */
70 #define ZYNQ_GEM_NWCFG_SPEED 0x00000001 /* 100 Mbps operation */
71 #define ZYNQ_GEM_NWCFG_FDEN 0x00000002 /* Full Duplex mode */
72 #define ZYNQ_GEM_NWCFG_FSREM 0x00020000 /* FCS removal */
73 #define ZYNQ_GEM_NWCFG_MDCCLKDIV 0x000080000 /* Div pclk by 32, 80MHz */
75 #define ZYNQ_GEM_NWCFG_INIT (ZYNQ_GEM_NWCFG_SPEED | \
76 ZYNQ_GEM_NWCFG_FDEN | \
77 ZYNQ_GEM_NWCFG_FSREM | \
78 ZYNQ_GEM_NWCFG_MDCCLKDIV)
80 #define ZYNQ_GEM_NWSR_MDIOIDLE_MASK 0x00000004 /* PHY management idle */
82 #define ZYNQ_GEM_DMACR_BLENGTH 0x00000004 /* INCR4 AHB bursts */
83 /* Use full configured addressable space (8 Kb) */
84 #define ZYNQ_GEM_DMACR_RXSIZE 0x00000300
85 /* Use full configured addressable space (4 Kb) */
86 #define ZYNQ_GEM_DMACR_TXSIZE 0x00000400
87 /* Set with binary 00011000 to use 1536 byte(1*max length frame/buffer) */
88 #define ZYNQ_GEM_DMACR_RXBUF 0x00180000
90 #define ZYNQ_GEM_DMACR_INIT (ZYNQ_GEM_DMACR_BLENGTH | \
91 ZYNQ_GEM_DMACR_RXSIZE | \
92 ZYNQ_GEM_DMACR_TXSIZE | \
95 /* Device registers */
96 struct zynq_gem_regs {
97 u32 nwctrl; /* Network Control reg */
98 u32 nwcfg; /* Network Config reg */
99 u32 nwsr; /* Network Status reg */
101 u32 dmacr; /* DMA Control reg */
102 u32 txsr; /* TX Status reg */
103 u32 rxqbase; /* RX Q Base address reg */
104 u32 txqbase; /* TX Q Base address reg */
105 u32 rxsr; /* RX Status reg */
107 u32 idr; /* Interrupt Disable reg */
109 u32 phymntnc; /* Phy Maintaince reg */
111 u32 hashl; /* Hash Low address reg */
112 u32 hashh; /* Hash High address reg */
115 u32 laddr[4][LADDR_HIGH + 1]; /* Specific1 addr low/high reg */
116 u32 match[4]; /* Type ID1 Match reg */
118 u32 stat[44]; /* Octects transmitted Low reg - stat start */
123 u32 addr; /* Next descriptor pointer */
129 /* Initialized, rxbd_current, rx_first_buf must be 0 after init */
130 struct zynq_gem_priv {
131 struct emac_bd tx_bd;
132 struct emac_bd rx_bd[RX_BUF];
133 char rxbuffers[RX_BUF * PKTSIZE_ALIGN];
137 struct phy_device *phydev;
141 static inline int mdio_wait(struct eth_device *dev)
143 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
146 /* Wait till MDIO interface is ready to accept a new transaction. */
148 if (readl(®s->nwsr) & ZYNQ_GEM_NWSR_MDIOIDLE_MASK)
154 printf("%s: Timeout\n", __func__);
161 static u32 phy_setup_op(struct eth_device *dev, u32 phy_addr, u32 regnum,
165 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
170 /* Construct mgtcr mask for the operation */
171 mgtcr = ZYNQ_GEM_PHYMNTNC_OP_MASK | op |
172 (phy_addr << ZYNQ_GEM_PHYMNTNC_PHYAD_SHIFT_MASK) |
173 (regnum << ZYNQ_GEM_PHYMNTNC_PHREG_SHIFT_MASK) | *data;
175 /* Write mgtcr and wait for completion */
176 writel(mgtcr, ®s->phymntnc);
181 if (op == ZYNQ_GEM_PHYMNTNC_OP_R_MASK)
182 *data = readl(®s->phymntnc);
187 static u32 phyread(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 *val)
189 return phy_setup_op(dev, phy_addr, regnum,
190 ZYNQ_GEM_PHYMNTNC_OP_R_MASK, val);
193 static u32 phywrite(struct eth_device *dev, u32 phy_addr, u32 regnum, u16 data)
195 return phy_setup_op(dev, phy_addr, regnum,
196 ZYNQ_GEM_PHYMNTNC_OP_W_MASK, &data);
199 static int zynq_gem_setup_mac(struct eth_device *dev)
201 u32 i, macaddrlow, macaddrhigh;
202 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
204 /* Set the MAC bits [31:0] in BOT */
205 macaddrlow = dev->enetaddr[0];
206 macaddrlow |= dev->enetaddr[1] << 8;
207 macaddrlow |= dev->enetaddr[2] << 16;
208 macaddrlow |= dev->enetaddr[3] << 24;
210 /* Set MAC bits [47:32] in TOP */
211 macaddrhigh = dev->enetaddr[4];
212 macaddrhigh |= dev->enetaddr[5] << 8;
214 for (i = 0; i < 4; i++) {
215 writel(0, ®s->laddr[i][LADDR_LOW]);
216 writel(0, ®s->laddr[i][LADDR_HIGH]);
217 /* Do not use MATCHx register */
218 writel(0, ®s->match[i]);
221 writel(macaddrlow, ®s->laddr[0][LADDR_LOW]);
222 writel(macaddrhigh, ®s->laddr[0][LADDR_HIGH]);
227 static int zynq_gem_init(struct eth_device *dev, bd_t * bis)
230 struct phy_device *phydev;
231 const u32 stat_size = (sizeof(struct zynq_gem_regs) -
232 offsetof(struct zynq_gem_regs, stat)) / 4;
233 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
234 struct zynq_gem_priv *priv = dev->priv;
235 const u32 supported = SUPPORTED_10baseT_Half |
236 SUPPORTED_10baseT_Full |
237 SUPPORTED_100baseT_Half |
238 SUPPORTED_100baseT_Full |
239 SUPPORTED_1000baseT_Half |
240 SUPPORTED_1000baseT_Full;
242 /* Disable all interrupts */
243 writel(0xFFFFFFFF, ®s->idr);
245 /* Disable the receiver & transmitter */
246 writel(0, ®s->nwctrl);
247 writel(0, ®s->txsr);
248 writel(0, ®s->rxsr);
249 writel(0, ®s->phymntnc);
251 /* Clear the Hash registers for the mac address pointed by AddressPtr */
252 writel(0x0, ®s->hashl);
253 /* Write bits [63:32] in TOP */
254 writel(0x0, ®s->hashh);
256 /* Clear all counters */
257 for (i = 0; i <= stat_size; i++)
258 readl(®s->stat[i]);
260 /* Setup RxBD space */
261 memset(&(priv->rx_bd), 0, sizeof(priv->rx_bd));
262 /* Create the RxBD ring */
263 memset(&(priv->rxbuffers), 0, sizeof(priv->rxbuffers));
265 for (i = 0; i < RX_BUF; i++) {
266 priv->rx_bd[i].status = 0xF0000000;
267 priv->rx_bd[i].addr = (u32)((char *) &(priv->rxbuffers) +
268 (i * PKTSIZE_ALIGN));
270 /* WRAP bit to last BD */
271 priv->rx_bd[--i].addr |= ZYNQ_GEM_RXBUF_WRAP_MASK;
272 /* Write RxBDs to IP */
273 writel((u32) &(priv->rx_bd), ®s->rxqbase);
276 /* Setup Network Configuration register */
277 writel(ZYNQ_GEM_NWCFG_INIT, ®s->nwcfg);
279 /* Setup for DMA Configuration register */
280 writel(ZYNQ_GEM_DMACR_INIT, ®s->dmacr);
282 /* Setup for Network Control register, MDIO, Rx and Tx enable */
283 setbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_MDEN_MASK |
284 ZYNQ_GEM_NWCTRL_RXEN_MASK | ZYNQ_GEM_NWCTRL_TXEN_MASK);
286 /* interface - look at tsec */
287 phydev = phy_connect(priv->bus, priv->phyaddr, dev, 0);
289 phydev->supported &= supported;
290 phydev->advertising = phydev->supported;
291 priv->phydev = phydev;
298 static int zynq_gem_send(struct eth_device *dev, void *ptr, int len)
301 struct zynq_gem_priv *priv = dev->priv;
302 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
303 const u32 mask = ZYNQ_GEM_TXSR_HRESPNOK_MASK | \
304 ZYNQ_GEM_TXSR_URUN_MASK | ZYNQ_GEM_TXSR_BUFEXH_MASK;
307 writel((u32)&(priv->tx_bd), ®s->txqbase);
310 memset((void *) &(priv->tx_bd), 0, sizeof(struct emac_bd));
312 priv->tx_bd.addr = (u32)ptr;
313 priv->tx_bd.status = len | ZYNQ_GEM_TXBUF_LAST_MASK |
314 ZYNQ_GEM_TXBUF_WRAP_MASK;
317 setbits_le32(®s->nwctrl, ZYNQ_GEM_NWCTRL_STARTTX_MASK);
319 /* Read the stat register to know if the packet has been transmitted */
320 status = readl(®s->txsr);
322 printf("Something has gone wrong here!? Status is 0x%x.\n",
325 /* Clear Tx status register before leaving . */
326 writel(status, ®s->txsr);
330 /* Do not check frame_recd flag in rx_status register 0x20 - just poll BD */
331 static int zynq_gem_recv(struct eth_device *dev)
334 struct zynq_gem_priv *priv = dev->priv;
335 struct emac_bd *current_bd = &priv->rx_bd[priv->rxbd_current];
336 struct emac_bd *first_bd;
338 if (!(current_bd->addr & ZYNQ_GEM_RXBUF_NEW_MASK))
341 if (!(current_bd->status &
342 (ZYNQ_GEM_RXBUF_SOF_MASK | ZYNQ_GEM_RXBUF_EOF_MASK))) {
343 printf("GEM: SOF or EOF not set for last buffer received!\n");
347 frame_len = current_bd->status & ZYNQ_GEM_RXBUF_LEN_MASK;
349 NetReceive((u8 *) (current_bd->addr &
350 ZYNQ_GEM_RXBUF_ADD_MASK), frame_len);
352 if (current_bd->status & ZYNQ_GEM_RXBUF_SOF_MASK)
353 priv->rx_first_buf = priv->rxbd_current;
355 current_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
356 current_bd->status = 0xF0000000; /* FIXME */
359 if (current_bd->status & ZYNQ_GEM_RXBUF_EOF_MASK) {
360 first_bd = &priv->rx_bd[priv->rx_first_buf];
361 first_bd->addr &= ~ZYNQ_GEM_RXBUF_NEW_MASK;
362 first_bd->status = 0xF0000000;
365 if ((++priv->rxbd_current) >= RX_BUF)
366 priv->rxbd_current = 0;
374 static void zynq_gem_halt(struct eth_device *dev)
376 struct zynq_gem_regs *regs = (struct zynq_gem_regs *)dev->iobase;
378 /* Disable the receiver & transmitter */
379 writel(0, ®s->nwctrl);
382 static int zynq_gem_miiphyread(const char *devname, uchar addr,
383 uchar reg, ushort *val)
385 struct eth_device *dev = eth_get_dev();
388 ret = phyread(dev, addr, reg, val);
389 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, *val);
393 static int zynq_gem_miiphy_write(const char *devname, uchar addr,
394 uchar reg, ushort val)
396 struct eth_device *dev = eth_get_dev();
398 debug("%s 0x%x, 0x%x, 0x%x\n", __func__, addr, reg, val);
399 return phywrite(dev, addr, reg, val);
402 int zynq_gem_initialize(bd_t *bis, int base_addr)
404 struct eth_device *dev;
405 struct zynq_gem_priv *priv;
407 dev = calloc(1, sizeof(*dev));
411 dev->priv = calloc(1, sizeof(struct zynq_gem_priv));
412 if (dev->priv == NULL) {
418 #ifdef CONFIG_PHY_ADDR
419 priv->phyaddr = CONFIG_PHY_ADDR;
424 sprintf(dev->name, "Gem.%x", base_addr);
426 dev->iobase = base_addr;
428 dev->init = zynq_gem_init;
429 dev->halt = zynq_gem_halt;
430 dev->send = zynq_gem_send;
431 dev->recv = zynq_gem_recv;
432 dev->write_hwaddr = zynq_gem_setup_mac;
436 miiphy_register(dev->name, zynq_gem_miiphyread, zynq_gem_miiphy_write);
437 priv->bus = miiphy_get_dev_by_name(dev->name);