2 * Dave Ethernet Controller driver
4 * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
13 #ifndef CONFIG_DNET_AUTONEG_TIMEOUT
14 #define CONFIG_DNET_AUTONEG_TIMEOUT 5000000 /* default value */
19 #include <linux/mii.h>
23 #include <asm/unaligned.h>
28 struct dnet_registers *regs;
29 const struct device *dev;
30 struct eth_device netdev;
31 unsigned short phy_addr;
34 /* get struct dnet_device from given struct netdev */
35 #define to_dnet(_nd) container_of(_nd, struct dnet_device, netdev)
37 /* function for reading internal MAC register */
38 u16 dnet_readw_mac(struct dnet_device *dnet, u16 reg)
43 writel(reg, &dnet->regs->MACREG_ADDR);
45 /* since a read/write op to the MAC is very slow,
46 * we must wait before reading the data */
49 /* read data read from the MAC register */
50 data_read = readl(&dnet->regs->MACREG_DATA);
56 /* function for writing internal MAC register */
57 void dnet_writew_mac(struct dnet_device *dnet, u16 reg, u16 val)
59 /* load data to write */
60 writel(val, &dnet->regs->MACREG_DATA);
63 writel(reg | DNET_INTERNAL_WRITE, &dnet->regs->MACREG_ADDR);
65 /* since a read/write op to the MAC is very slow,
66 * we must wait before exiting */
70 static void dnet_mdio_write(struct dnet_device *dnet, u8 reg, u16 value)
74 debug(DRIVERNAME "dnet_mdio_write %02x:%02x <- %04x\n",
75 dnet->phy_addr, reg, value);
77 while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
78 DNET_INTERNAL_GMII_MNG_CMD_FIN))
81 /* prepare for a write operation */
84 /* only 5 bits allowed for register offset */
87 /* prepare reg_value for a write */
88 tmp |= (dnet->phy_addr << 8);
91 /* write data to write first */
92 dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
94 /* write control word */
95 dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
97 while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
98 DNET_INTERNAL_GMII_MNG_CMD_FIN))
102 static u16 dnet_mdio_read(struct dnet_device *dnet, u8 reg)
106 while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
107 DNET_INTERNAL_GMII_MNG_CMD_FIN))
110 /* only 5 bits allowed for register offset*/
113 /* prepare reg_value for a read */
114 value = (dnet->phy_addr << 8);
117 /* write control word */
118 dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
120 /* wait for end of transfer */
121 while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
122 DNET_INTERNAL_GMII_MNG_CMD_FIN))
125 value = dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG);
127 debug(DRIVERNAME "dnet_mdio_read %02x:%02x <- %04x\n",
128 dnet->phy_addr, reg, value);
133 static int dnet_send(struct eth_device *netdev, volatile void *packet,
136 struct dnet_device *dnet = to_dnet(netdev);
141 debug(DRIVERNAME "[%s] Sending %u bytes\n", __func__, length);
143 bufp = (unsigned int *) (((u32)packet) & 0xFFFFFFFC);
144 wrsz = (u32)length + 3;
145 wrsz += ((u32)packet) & 0x3;
147 tx_cmd = ((((unsigned int)(packet)) & 0x03) << 16) | (u32)length;
149 /* check if there is enough room for the current frame */
150 if (wrsz < (DNET_FIFO_SIZE - readl(&dnet->regs->TX_FIFO_WCNT))) {
151 for (i = 0; i < wrsz; i++)
152 writel(*bufp++, &dnet->regs->TX_DATA_FIFO);
154 * inform MAC that a packet's written and ready
157 writel(tx_cmd, &dnet->regs->TX_LEN_FIFO);
159 printf(DRIVERNAME "No free space (actual %d, required %d "
160 "(words))\n", DNET_FIFO_SIZE -
161 readl(&dnet->regs->TX_FIFO_WCNT), wrsz);
164 /* No one cares anyway */
169 static int dnet_recv(struct eth_device *netdev)
171 struct dnet_device *dnet = to_dnet(netdev);
172 unsigned int *data_ptr;
173 int pkt_len, poll, i;
176 debug("Waiting for pkt (polling)\n");
178 while ((readl(&dnet->regs->RX_FIFO_WCNT) >> 16) == 0) {
179 udelay(10); /* wait 10 usec */
181 return 0; /* no pkt available */
184 cmd_word = readl(&dnet->regs->RX_LEN_FIFO);
185 pkt_len = cmd_word & 0xFFFF;
187 debug("Got pkt with size %d bytes\n", pkt_len);
189 if (cmd_word & 0xDF180000)
190 printf("%s packet receive error %x\n", __func__, cmd_word);
192 data_ptr = (unsigned int *) NetRxPackets[0];
194 for (i = 0; i < (pkt_len + 3) >> 2; i++)
195 *data_ptr++ = readl(&dnet->regs->RX_DATA_FIFO);
197 NetReceive(NetRxPackets[0], pkt_len + 5); /* ok + 5 ?? */
202 static void dnet_set_hwaddr(struct eth_device *netdev)
204 struct dnet_device *dnet = to_dnet(netdev);
207 tmp = get_unaligned_be16(netdev->enetaddr);
208 dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
209 tmp = get_unaligned_be16(&netdev->enetaddr[2]);
210 dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
211 tmp = get_unaligned_be16(&netdev->enetaddr[4]);
212 dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
215 static void dnet_phy_reset(struct dnet_device *dnet)
217 struct eth_device *netdev = &dnet->netdev;
221 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
222 dnet_mdio_write(dnet, MII_ADVERTISE, adv);
223 printf("%s: Starting autonegotiation...\n", netdev->name);
224 dnet_mdio_write(dnet, MII_BMCR, (BMCR_ANENABLE
227 for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
228 status = dnet_mdio_read(dnet, MII_BMSR);
229 if (status & BMSR_ANEGCOMPLETE)
234 if (status & BMSR_ANEGCOMPLETE)
235 printf("%s: Autonegotiation complete\n", netdev->name);
237 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
238 netdev->name, status);
241 static int dnet_phy_init(struct dnet_device *dnet)
243 struct eth_device *netdev = &dnet->netdev;
244 u16 phy_id, status, adv, lpa;
245 int media, speed, duplex;
250 for (i = 0; i < 32; i++) {
252 phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
253 if (phy_id != 0xffff) {
255 printf("Found PHY at address %d PHYID (%04x:%04x)\n",
257 dnet_mdio_read(dnet, MII_PHYSID2));
262 /* Check if the PHY is up to snuff... */
263 phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
264 if (phy_id == 0xffff) {
265 printf("%s: No PHY present\n", netdev->name);
269 status = dnet_mdio_read(dnet, MII_BMSR);
270 if (!(status & BMSR_LSTATUS)) {
271 /* Try to re-negotiate if we don't have link already. */
272 dnet_phy_reset(dnet);
274 for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
275 status = dnet_mdio_read(dnet, MII_BMSR);
276 if (status & BMSR_LSTATUS)
282 if (!(status & BMSR_LSTATUS)) {
283 printf("%s: link down (status: 0x%04x)\n",
284 netdev->name, status);
287 adv = dnet_mdio_read(dnet, MII_ADVERTISE);
288 lpa = dnet_mdio_read(dnet, MII_LPA);
289 media = mii_nway_result(lpa & adv);
290 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
292 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
293 /* 1000BaseT ethernet is not supported */
294 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
296 speed ? "100" : "10",
297 duplex ? "full" : "half",
300 ctl_reg = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
303 ctl_reg &= ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
305 ctl_reg |= DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
307 dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
313 static int dnet_init(struct eth_device *netdev, bd_t *bd)
315 struct dnet_device *dnet = to_dnet(netdev);
319 * dnet_halt should have been called at some point before now,
320 * so we'll assume the controller is idle.
323 /* set hardware address */
324 dnet_set_hwaddr(netdev);
326 if (dnet_phy_init(dnet) < 0)
329 /* flush rx/tx fifos */
330 writel(DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
331 &dnet->regs->SYS_CTL);
333 writel(0, &dnet->regs->SYS_CTL);
335 config = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
337 config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
338 DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
339 DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
340 DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
342 dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, config);
344 /* Enable TX and RX */
345 dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG,
346 DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
351 static void dnet_halt(struct eth_device *netdev)
353 struct dnet_device *dnet = to_dnet(netdev);
355 /* Disable TX and RX */
356 dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG, 0);
359 int dnet_eth_initialize(int id, void *regs, unsigned int phy_addr)
361 struct dnet_device *dnet;
362 struct eth_device *netdev;
363 unsigned int dev_capa;
365 dnet = malloc(sizeof(struct dnet_device));
367 printf("Error: Failed to allocate memory for DNET%d\n", id);
370 memset(dnet, 0, sizeof(struct dnet_device));
372 netdev = &dnet->netdev;
374 dnet->regs = (struct dnet_registers *)regs;
375 dnet->phy_addr = phy_addr;
377 sprintf(netdev->name, "dnet%d", id);
378 netdev->init = dnet_init;
379 netdev->halt = dnet_halt;
380 netdev->send = dnet_send;
381 netdev->recv = dnet_recv;
383 dev_capa = readl(&dnet->regs->VERCAPS) & 0xFFFF;
384 debug("%s: has %smdio, %sirq, %sgigabit, %sdma \n", netdev->name,
385 (dev_capa & DNET_HAS_MDIO) ? "" : "no ",
386 (dev_capa & DNET_HAS_IRQ) ? "" : "no ",
387 (dev_capa & DNET_HAS_GIGABIT) ? "" : "no ",
388 (dev_capa & DNET_HAS_DMA) ? "" : "no ");
390 eth_register(netdev);