35c2ed9fe06c0e628fc15eaec4cd9d9443aeee5e
[oweals/u-boot.git] / drivers / net / dnet.c
1 /*
2  * Dave Ethernet Controller driver
3  *
4  * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
5  *
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.
9  */
10
11 #include <common.h>
12 #include <log.h>
13
14 #ifndef CONFIG_DNET_AUTONEG_TIMEOUT
15 #define CONFIG_DNET_AUTONEG_TIMEOUT     5000000 /* default value */
16 #endif
17
18 #include <net.h>
19 #include <malloc.h>
20 #include <linux/mii.h>
21
22 #include <miiphy.h>
23 #include <asm/io.h>
24 #include <asm/unaligned.h>
25
26 #include "dnet.h"
27
28 struct dnet_device {
29         struct dnet_registers   *regs;
30         const struct device     *dev;
31         struct eth_device       netdev;
32         unsigned short          phy_addr;
33 };
34
35 /* get struct dnet_device from given struct netdev */
36 #define to_dnet(_nd) container_of(_nd, struct dnet_device, netdev)
37
38 /* function for reading internal MAC register */
39 u16 dnet_readw_mac(struct dnet_device *dnet, u16 reg)
40 {
41         u16 data_read;
42
43         /* issue a read */
44         writel(reg, &dnet->regs->MACREG_ADDR);
45
46         /* since a read/write op to the MAC is very slow,
47          * we must wait before reading the data */
48         udelay(1);
49
50         /* read data read from the MAC register */
51         data_read = readl(&dnet->regs->MACREG_DATA);
52
53         /* all done */
54         return data_read;
55 }
56
57 /* function for writing internal MAC register */
58 void dnet_writew_mac(struct dnet_device *dnet, u16 reg, u16 val)
59 {
60         /* load data to write */
61         writel(val, &dnet->regs->MACREG_DATA);
62
63         /* issue a write */
64         writel(reg | DNET_INTERNAL_WRITE, &dnet->regs->MACREG_ADDR);
65
66         /* since a read/write op to the MAC is very slow,
67          * we must wait before exiting */
68         udelay(1);
69 }
70
71 static void dnet_mdio_write(struct dnet_device *dnet, u8 reg, u16 value)
72 {
73         u16 tmp;
74
75         debug(DRIVERNAME "dnet_mdio_write %02x:%02x <- %04x\n",
76                         dnet->phy_addr, reg, value);
77
78         while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
79                                 DNET_INTERNAL_GMII_MNG_CMD_FIN))
80                 ;
81
82         /* prepare for a write operation */
83         tmp = (1 << 13);
84
85         /* only 5 bits allowed for register offset */
86         reg &= 0x1f;
87
88         /* prepare reg_value for a write */
89         tmp |= (dnet->phy_addr << 8);
90         tmp |= reg;
91
92         /* write data to write first */
93         dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
94
95         /* write control word */
96         dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
97
98         while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
99                                 DNET_INTERNAL_GMII_MNG_CMD_FIN))
100                 ;
101 }
102
103 static u16 dnet_mdio_read(struct dnet_device *dnet, u8 reg)
104 {
105         u16 value;
106
107         while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
108                                 DNET_INTERNAL_GMII_MNG_CMD_FIN))
109                 ;
110
111         /* only 5 bits allowed for register offset*/
112         reg &= 0x1f;
113
114         /* prepare reg_value for a read */
115         value = (dnet->phy_addr << 8);
116         value |= reg;
117
118         /* write control word */
119         dnet_writew_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
120
121         /* wait for end of transfer */
122         while (!(dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_CTL_REG) &
123                                 DNET_INTERNAL_GMII_MNG_CMD_FIN))
124                 ;
125
126         value = dnet_readw_mac(dnet, DNET_INTERNAL_GMII_MNG_DAT_REG);
127
128         debug(DRIVERNAME "dnet_mdio_read %02x:%02x <- %04x\n",
129                 dnet->phy_addr, reg, value);
130
131         return value;
132 }
133
134 static int dnet_send(struct eth_device *netdev, void *packet, int length)
135 {
136         struct dnet_device *dnet = to_dnet(netdev);
137         int i, wrsz;
138         unsigned int *bufp;
139         unsigned int tx_cmd;
140
141         debug(DRIVERNAME "[%s] Sending %u bytes\n", __func__, length);
142
143         bufp = (unsigned int *) (((u32)packet) & 0xFFFFFFFC);
144         wrsz = (u32)length + 3;
145         wrsz += ((u32)packet) & 0x3;
146         wrsz >>= 2;
147         tx_cmd = ((((unsigned int)(packet)) & 0x03) << 16) | (u32)length;
148
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);
153                 /*
154                  * inform MAC that a packet's written and ready
155                  * to be shipped out
156                  */
157                 writel(tx_cmd, &dnet->regs->TX_LEN_FIFO);
158         } else {
159                 printf(DRIVERNAME "No free space (actual %d, required %d "
160                                 "(words))\n", DNET_FIFO_SIZE -
161                                 readl(&dnet->regs->TX_FIFO_WCNT), wrsz);
162         }
163
164         /* No one cares anyway */
165         return 0;
166 }
167
168
169 static int dnet_recv(struct eth_device *netdev)
170 {
171         struct dnet_device *dnet = to_dnet(netdev);
172         unsigned int *data_ptr;
173         int pkt_len, poll, i;
174         u32 cmd_word;
175
176         debug("Waiting for pkt (polling)\n");
177         poll = 50;
178         while ((readl(&dnet->regs->RX_FIFO_WCNT) >> 16) == 0) {
179                 udelay(10);  /* wait 10 usec */
180                 if (--poll == 0)
181                         return 0;       /* no pkt available */
182         }
183
184         cmd_word = readl(&dnet->regs->RX_LEN_FIFO);
185         pkt_len = cmd_word & 0xFFFF;
186
187         debug("Got pkt with size %d bytes\n", pkt_len);
188
189         if (cmd_word & 0xDF180000)
190                 printf("%s packet receive error %x\n", __func__, cmd_word);
191
192         data_ptr = (unsigned int *)net_rx_packets[0];
193
194         for (i = 0; i < (pkt_len + 3) >> 2; i++)
195                 *data_ptr++ = readl(&dnet->regs->RX_DATA_FIFO);
196
197         /* ok + 5 ?? */
198         net_process_received_packet(net_rx_packets[0], pkt_len + 5);
199
200         return 0;
201 }
202
203 static void dnet_set_hwaddr(struct eth_device *netdev)
204 {
205         struct dnet_device *dnet = to_dnet(netdev);
206         u16 tmp;
207
208         tmp = get_unaligned_be16(netdev->enetaddr);
209         dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
210         tmp = get_unaligned_be16(&netdev->enetaddr[2]);
211         dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
212         tmp = get_unaligned_be16(&netdev->enetaddr[4]);
213         dnet_writew_mac(dnet, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
214 }
215
216 static void dnet_phy_reset(struct dnet_device *dnet)
217 {
218         struct eth_device *netdev = &dnet->netdev;
219         int i;
220         u16 status, adv;
221
222         adv = ADVERTISE_CSMA | ADVERTISE_ALL;
223         dnet_mdio_write(dnet, MII_ADVERTISE, adv);
224         printf("%s: Starting autonegotiation...\n", netdev->name);
225         dnet_mdio_write(dnet, MII_BMCR, (BMCR_ANENABLE
226                                          | BMCR_ANRESTART));
227
228         for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
229                 status = dnet_mdio_read(dnet, MII_BMSR);
230                 if (status & BMSR_ANEGCOMPLETE)
231                         break;
232                 udelay(100);
233         }
234
235         if (status & BMSR_ANEGCOMPLETE)
236                 printf("%s: Autonegotiation complete\n", netdev->name);
237         else
238                 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
239                        netdev->name, status);
240 }
241
242 static int dnet_phy_init(struct dnet_device *dnet)
243 {
244         struct eth_device *netdev = &dnet->netdev;
245         u16 phy_id, status, adv, lpa;
246         int media, speed, duplex;
247         int i;
248         u32 ctl_reg;
249
250         /* Find a PHY */
251         for (i = 0; i < 32; i++) {
252                 dnet->phy_addr = i;
253                 phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
254                 if (phy_id != 0xffff) {
255                         /* ok we found it */
256                         printf("Found PHY at address %d PHYID (%04x:%04x)\n",
257                                         i, phy_id,
258                                         dnet_mdio_read(dnet, MII_PHYSID2));
259                         break;
260                 }
261         }
262
263         /* Check if the PHY is up to snuff... */
264         phy_id = dnet_mdio_read(dnet, MII_PHYSID1);
265         if (phy_id == 0xffff) {
266                 printf("%s: No PHY present\n", netdev->name);
267                 return -1;
268         }
269
270         status = dnet_mdio_read(dnet, MII_BMSR);
271         if (!(status & BMSR_LSTATUS)) {
272                 /* Try to re-negotiate if we don't have link already. */
273                 dnet_phy_reset(dnet);
274
275                 for (i = 0; i < CONFIG_DNET_AUTONEG_TIMEOUT / 100; i++) {
276                         status = dnet_mdio_read(dnet, MII_BMSR);
277                         if (status & BMSR_LSTATUS)
278                                 break;
279                         udelay(100);
280                 }
281         }
282
283         if (!(status & BMSR_LSTATUS)) {
284                 printf("%s: link down (status: 0x%04x)\n",
285                        netdev->name, status);
286                 return -1;
287         } else {
288                 adv = dnet_mdio_read(dnet, MII_ADVERTISE);
289                 lpa = dnet_mdio_read(dnet, MII_LPA);
290                 media = mii_nway_result(lpa & adv);
291                 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
292                          ? 1 : 0);
293                 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
294                 /* 1000BaseT ethernet is not supported */
295                 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
296                        netdev->name,
297                        speed ? "100" : "10",
298                        duplex ? "full" : "half",
299                        lpa);
300
301                 ctl_reg = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
302
303                 if (duplex)
304                         ctl_reg &= ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
305                 else
306                         ctl_reg |= DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
307
308                 dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
309
310                 return 0;
311         }
312 }
313
314 static int dnet_init(struct eth_device *netdev, bd_t *bd)
315 {
316         struct dnet_device *dnet = to_dnet(netdev);
317         u32 config;
318
319         /*
320          * dnet_halt should have been called at some point before now,
321          * so we'll assume the controller is idle.
322          */
323
324         /* set hardware address */
325         dnet_set_hwaddr(netdev);
326
327         if (dnet_phy_init(dnet) < 0)
328                 return -1;
329
330         /* flush rx/tx fifos */
331         writel(DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
332                         &dnet->regs->SYS_CTL);
333         udelay(1000);
334         writel(0, &dnet->regs->SYS_CTL);
335
336         config = dnet_readw_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG);
337
338         config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
339                         DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
340                         DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
341                         DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
342
343         dnet_writew_mac(dnet, DNET_INTERNAL_RXTX_CONTROL_REG, config);
344
345         /* Enable TX and RX */
346         dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG,
347                         DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
348
349         return 0;
350 }
351
352 static void dnet_halt(struct eth_device *netdev)
353 {
354         struct dnet_device *dnet = to_dnet(netdev);
355
356         /* Disable TX and RX */
357         dnet_writew_mac(dnet, DNET_INTERNAL_MODE_REG, 0);
358 }
359
360 int dnet_eth_initialize(int id, void *regs, unsigned int phy_addr)
361 {
362         struct dnet_device *dnet;
363         struct eth_device *netdev;
364         unsigned int dev_capa;
365
366         dnet = malloc(sizeof(struct dnet_device));
367         if (!dnet) {
368                 printf("Error: Failed to allocate memory for DNET%d\n", id);
369                 return -1;
370         }
371         memset(dnet, 0, sizeof(struct dnet_device));
372
373         netdev = &dnet->netdev;
374
375         dnet->regs = (struct dnet_registers *)regs;
376         dnet->phy_addr = phy_addr;
377
378         sprintf(netdev->name, "dnet%d", id);
379         netdev->init = dnet_init;
380         netdev->halt = dnet_halt;
381         netdev->send = dnet_send;
382         netdev->recv = dnet_recv;
383
384         dev_capa = readl(&dnet->regs->VERCAPS) & 0xFFFF;
385         debug("%s: has %smdio, %sirq, %sgigabit, %sdma \n", netdev->name,
386                 (dev_capa & DNET_HAS_MDIO) ? "" : "no ",
387                 (dev_capa & DNET_HAS_IRQ) ? "" : "no ",
388                 (dev_capa & DNET_HAS_GIGABIT) ? "" : "no ",
389                 (dev_capa & DNET_HAS_DMA) ? "" : "no ");
390
391         eth_register(netdev);
392
393         return 0;
394 }