]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/net/bfin_mac.c
Merge branch 'master' of git://git.denx.de/u-boot-blackfin
[karo-tx-uboot.git] / drivers / net / bfin_mac.c
1 /*
2  * Driver for Blackfin On-Chip MAC device
3  *
4  * Copyright (c) 2005-2008 Analog Device, Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <common.h>
10 #include <config.h>
11 #include <net.h>
12 #include <netdev.h>
13 #include <command.h>
14 #include <malloc.h>
15
16 #include <asm/blackfin.h>
17 #include <asm/mach-common/bits/dma.h>
18 #include <asm/mach-common/bits/emac.h>
19 #include <asm/mach-common/bits/pll.h>
20
21 #include "bfin_mac.h"
22
23 #ifdef CONFIG_POST
24 #include <post.h>
25 #endif
26
27 #undef DEBUG_ETHERNET
28
29 #ifdef DEBUG_ETHERNET
30 #define DEBUGF(fmt, args...) printf(fmt, ##args)
31 #else
32 #define DEBUGF(fmt, args...)
33 #endif
34
35 #define RXBUF_BASE_ADDR         0xFF900000
36 #define TXBUF_BASE_ADDR         0xFF800000
37 #define TX_BUF_CNT              1
38
39 #define TOUT_LOOP               1000000
40
41 ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
42 ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
43 static u16 txIdx;               /* index of the current RX buffer */
44 static u16 rxIdx;               /* index of the current TX buffer */
45
46 u16 PHYregs[NO_PHY_REGS];       /* u16 PHYADDR; */
47
48 /* DMAx_CONFIG values at DMA Restart */
49 const ADI_DMA_CONFIG_REG rxdmacfg = {
50         .b_DMA_EN  = 1, /* enabled */
51         .b_WNR     = 1, /* write to memory */
52         .b_WDSIZE  = 2, /* wordsize is 32 bits */
53         .b_DMA2D   = 0,
54         .b_RESTART = 0,
55         .b_DI_SEL  = 0,
56         .b_DI_EN   = 0, /* no interrupt */
57         .b_NDSIZE  = 5, /* 5 half words is desc size */
58         .b_FLOW    = 7  /* large desc flow */
59 };
60
61 const ADI_DMA_CONFIG_REG txdmacfg = {
62         .b_DMA_EN  = 1, /* enabled */
63         .b_WNR     = 0, /* read from memory */
64         .b_WDSIZE  = 2, /* wordsize is 32 bits */
65         .b_DMA2D   = 0,
66         .b_RESTART = 0,
67         .b_DI_SEL  = 0,
68         .b_DI_EN   = 0, /* no interrupt */
69         .b_NDSIZE  = 5, /* 5 half words is desc size */
70         .b_FLOW    = 7  /* large desc flow */
71 };
72
73 int bfin_EMAC_initialize(bd_t *bis)
74 {
75         struct eth_device *dev;
76         dev = (struct eth_device *)malloc(sizeof(*dev));
77         if (dev == NULL)
78                 hang();
79
80         memset(dev, 0, sizeof(*dev));
81         sprintf(dev->name, "Blackfin EMAC");
82
83         dev->iobase = 0;
84         dev->priv = 0;
85         dev->init = bfin_EMAC_init;
86         dev->halt = bfin_EMAC_halt;
87         dev->send = bfin_EMAC_send;
88         dev->recv = bfin_EMAC_recv;
89
90         eth_register(dev);
91
92         return 0;
93 }
94
95 static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
96                           int length)
97 {
98         int i;
99         int result = 0;
100         unsigned int *buf;
101         buf = (unsigned int *)packet;
102
103         if (length <= 0) {
104                 printf("Ethernet: bad packet size: %d\n", length);
105                 goto out;
106         }
107
108         if ((*pDMA2_IRQ_STATUS & DMA_ERR) != 0) {
109                 printf("Ethernet: tx DMA error\n");
110                 goto out;
111         }
112
113         for (i = 0; (*pDMA2_IRQ_STATUS & DMA_RUN) != 0; i++) {
114                 if (i > TOUT_LOOP) {
115                         puts("Ethernet: tx time out\n");
116                         goto out;
117                 }
118         }
119         txbuf[txIdx]->FrmData->NoBytes = length;
120         memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
121         txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
122         *pDMA2_NEXT_DESC_PTR = &txbuf[txIdx]->Dma[0];
123         *pDMA2_CONFIG = *(u16 *) (void *)(&txdmacfg);
124         *pEMAC_OPMODE |= TE;
125
126         for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
127                 if (i > TOUT_LOOP) {
128                         puts("Ethernet: tx error\n");
129                         goto out;
130                 }
131         }
132         result = txbuf[txIdx]->StatusWord;
133         txbuf[txIdx]->StatusWord = 0;
134         if ((txIdx + 1) >= TX_BUF_CNT)
135                 txIdx = 0;
136         else
137                 txIdx++;
138  out:
139         DEBUGF("BFIN EMAC send: length = %d\n", length);
140         return result;
141 }
142
143 static int bfin_EMAC_recv(struct eth_device *dev)
144 {
145         int length = 0;
146
147         for (;;) {
148                 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
149                         length = -1;
150                         break;
151                 }
152                 if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
153                         printf("Ethernet: rx dma overrun\n");
154                         break;
155                 }
156                 if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
157                         printf("Ethernet: rx error\n");
158                         break;
159                 }
160                 length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
161                 if (length <= 4) {
162                         printf("Ethernet: bad frame\n");
163                         break;
164                 }
165                 NetRxPackets[rxIdx] =
166                     (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
167                 NetReceive(NetRxPackets[rxIdx], length - 4);
168                 *pDMA1_IRQ_STATUS |= DMA_DONE | DMA_ERR;
169                 rxbuf[rxIdx]->StatusWord = 0x00000000;
170                 if ((rxIdx + 1) >= PKTBUFSRX)
171                         rxIdx = 0;
172                 else
173                         rxIdx++;
174         }
175
176         return length;
177 }
178
179 /**************************************************************
180  *
181  * Ethernet Initialization Routine
182  *
183  *************************************************************/
184
185 static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
186 {
187         u32 opmode;
188         int dat;
189         int i;
190         DEBUGF("Eth_init: ......\n");
191
192         txIdx = 0;
193         rxIdx = 0;
194
195 /* Initialize System Register */
196         if (SetupSystemRegs(&dat) < 0)
197                 return -1;
198
199 /* Initialize EMAC address */
200         bfin_EMAC_setup_addr(bd);
201
202 /* Initialize TX and RX buffer */
203         for (i = 0; i < PKTBUFSRX; i++) {
204                 rxbuf[i] = SetupRxBuffer(i);
205                 if (i > 0) {
206                         rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR =
207                             &(rxbuf[i]->Dma[0]);
208                         if (i == (PKTBUFSRX - 1))
209                                 rxbuf[i]->Dma[1].NEXT_DESC_PTR =
210                                     &(rxbuf[0]->Dma[0]);
211                 }
212         }
213         for (i = 0; i < TX_BUF_CNT; i++) {
214                 txbuf[i] = SetupTxBuffer(i);
215                 if (i > 0) {
216                         txbuf[i - 1]->Dma[1].NEXT_DESC_PTR =
217                             &(txbuf[i]->Dma[0]);
218                         if (i == (TX_BUF_CNT - 1))
219                                 txbuf[i]->Dma[1].NEXT_DESC_PTR =
220                                     &(txbuf[0]->Dma[0]);
221                 }
222         }
223
224         /* Set RX DMA */
225         *pDMA1_NEXT_DESC_PTR = &rxbuf[0]->Dma[0];
226         *pDMA1_CONFIG = *((u16 *) (void *)&rxbuf[0]->Dma[0].CONFIG);
227
228         /* Wait MII done */
229         PollMdcDone();
230
231         /* We enable only RX here */
232         /* ASTP   : Enable Automatic Pad Stripping
233            PR     : Promiscuous Mode for test
234            PSF    : Receive frames with total length less than 64 bytes.
235            FDMODE : Full Duplex Mode
236            LB     : Internal Loopback for test
237            RE     : Receiver Enable */
238         if (dat == FDMODE)
239                 opmode = ASTP | FDMODE | PSF;
240         else
241                 opmode = ASTP | PSF;
242         opmode |= RE;
243 #ifdef CONFIG_BFIN_MAC_RMII
244         opmode |= TE | RMII;
245 #endif
246         /* Turn on the EMAC */
247         *pEMAC_OPMODE = opmode;
248         return 0;
249 }
250
251 static void bfin_EMAC_halt(struct eth_device *dev)
252 {
253         DEBUGF("Eth_halt: ......\n");
254         /* Turn off the EMAC */
255         *pEMAC_OPMODE = 0x00000000;
256         /* Turn off the EMAC RX DMA */
257         *pDMA1_CONFIG = 0x0000;
258         *pDMA2_CONFIG = 0x0000;
259
260 }
261
262 void bfin_EMAC_setup_addr(bd_t *bd)
263 {
264         *pEMAC_ADDRLO =
265                 bd->bi_enetaddr[0] |
266                 bd->bi_enetaddr[1] << 8 |
267                 bd->bi_enetaddr[2] << 16 |
268                 bd->bi_enetaddr[3] << 24;
269         *pEMAC_ADDRHI =
270                 bd->bi_enetaddr[4] |
271                 bd->bi_enetaddr[5] << 8;
272 }
273
274 static void PollMdcDone(void)
275 {
276         /* poll the STABUSY bit */
277         while (*pEMAC_STAADD & STABUSY) ;
278 }
279
280 static void WrPHYReg(u16 PHYAddr, u16 RegAddr, u16 Data)
281 {
282         PollMdcDone();
283
284         *pEMAC_STADAT = Data;
285
286         *pEMAC_STAADD = SET_PHYAD(PHYAddr) | SET_REGAD(RegAddr) |
287             STAOP | STAIE | STABUSY;
288 }
289
290 /*********************************************************************************
291  *              Read an off-chip register in a PHY through the MDC/MDIO port     *
292  *********************************************************************************/
293 static u16 RdPHYReg(u16 PHYAddr, u16 RegAddr)
294 {
295         u16 Data;
296
297         PollMdcDone();
298
299         *pEMAC_STAADD = SET_PHYAD(PHYAddr) | SET_REGAD(RegAddr) |
300             STAIE | STABUSY;
301
302         PollMdcDone();
303
304         Data = (u16) * pEMAC_STADAT;
305
306         PHYregs[RegAddr] = Data;        /* save shadow copy */
307
308         return Data;
309 }
310
311 #if 0 /* dead code ? */
312 static void SoftResetPHY(void)
313 {
314         u16 phydat;
315         /* set the reset bit */
316         WrPHYReg(PHYADDR, PHY_MODECTL, PHY_RESET);
317         /* and clear it again */
318         WrPHYReg(PHYADDR, PHY_MODECTL, 0x0000);
319         do {
320                 /* poll until reset is complete */
321                 phydat = RdPHYReg(PHYADDR, PHY_MODECTL);
322         } while ((phydat & PHY_RESET) != 0);
323 }
324 #endif
325
326 static int SetupSystemRegs(int *opmode)
327 {
328         u16 sysctl, phydat;
329         int count = 0;
330         /* Enable PHY output */
331         *pVR_CTL |= CLKBUFOE;
332         /* Set all the pins to peripheral mode */
333
334 #ifdef CONFIG_BFIN_MAC_RMII
335         /* grab RMII pins */
336 # if defined(__ADSPBF51x__)
337         *pPORTF_MUX = (*pPORTF_MUX & \
338                 ~(PORT_x_MUX_3_MASK | PORT_x_MUX_4_MASK | PORT_x_MUX_5_MASK)) | \
339                 PORT_x_MUX_3_FUNC_1 | PORT_x_MUX_4_FUNC_1 | PORT_x_MUX_5_FUNC_1;
340         *pPORTF_FER |= PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
341         *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
342         *pPORTG_FER |= PG0 | PG1 | PG2;
343 # elif defined(__ADSPBF52x__)
344         *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
345         *pPORTG_FER |= PG14 | PG15;
346         *pPORTH_MUX = (*pPORTH_MUX & ~(PORT_x_MUX_0_MASK | PORT_x_MUX_1_MASK)) | \
347                 PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2;
348         *pPORTH_FER |= PH0 | PH1 | PH2 | PH3 | PH4 | PH5 | PH6 | PH7 | PH8;
349 # else
350         *pPORTH_FER |= PH0 | PH1 | PH4 | PH5 | PH6 | PH8 | PH9 | PH14 | PH15;
351 # endif
352 #else
353         /* grab MII & RMII pins */
354 # if defined(__ADSPBF51x__)
355         *pPORTF_MUX = (*pPORTF_MUX & \
356                 ~(PORT_x_MUX_0_MASK | PORT_x_MUX_1_MASK | PORT_x_MUX_3_MASK | PORT_x_MUX_4_MASK | PORT_x_MUX_5_MASK)) | \
357                 PORT_x_MUX_0_FUNC_1 | PORT_x_MUX_1_FUNC_1 | PORT_x_MUX_3_FUNC_1 | PORT_x_MUX_4_FUNC_1 | PORT_x_MUX_5_FUNC_1;
358         *pPORTF_FER |= PF0 | PF1 | PF2 | PF3 | PF4 | PF5 | PF6 | PF8 | PF9 | PF10 | PF11 | PF12 | PF13 | PF14 | PF15;
359         *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_0_MASK) | PORT_x_MUX_0_FUNC_1;
360         *pPORTG_FER |= PG0 | PG1 | PG2;
361 # elif defined(__ADSPBF52x__)
362         *pPORTG_MUX = (*pPORTG_MUX & ~PORT_x_MUX_6_MASK) | PORT_x_MUX_6_FUNC_2;
363         *pPORTG_FER |= PG14 | PG15;
364         *pPORTH_MUX = PORT_x_MUX_0_FUNC_2 | PORT_x_MUX_1_FUNC_2 | PORT_x_MUX_2_FUNC_2;
365         *pPORTH_FER = -1; /* all pins */
366 # else
367         *pPORTH_FER = -1; /* all pins */
368 # endif
369 #endif
370
371         /* MDC  = 2.5 MHz */
372         sysctl = SET_MDCDIV(24);
373         /* Odd word alignment for Receive Frame DMA word */
374         /* Configure checksum support and rcve frame word alignment */
375         sysctl |= RXDWA | RXCKS;
376         *pEMAC_SYSCTL = sysctl;
377         /* auto negotiation on  */
378         /* full duplex */
379         /* 100 Mbps */
380         phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET;
381         WrPHYReg(PHYADDR, PHY_MODECTL, phydat);
382         do {
383                 udelay(1000);
384                 phydat = RdPHYReg(PHYADDR, PHY_MODESTAT);
385                 if (count > 3000) {
386                         printf
387                             ("Link is down, please check your network connection\n");
388                         return -1;
389                 }
390                 count++;
391         } while (!(phydat & 0x0004));
392
393         phydat = RdPHYReg(PHYADDR, PHY_ANLPAR);
394
395         if ((phydat & 0x0100) || (phydat & 0x0040))
396                 *opmode = FDMODE;
397         else
398                 *opmode = 0;
399
400         *pEMAC_MMC_CTL = RSTC | CROLL;
401
402         /* Initialize the TX DMA channel registers */
403         *pDMA2_X_COUNT = 0;
404         *pDMA2_X_MODIFY = 4;
405         *pDMA2_Y_COUNT = 0;
406         *pDMA2_Y_MODIFY = 0;
407
408         /* Initialize the RX DMA channel registers */
409         *pDMA1_X_COUNT = 0;
410         *pDMA1_X_MODIFY = 4;
411         *pDMA1_Y_COUNT = 0;
412         *pDMA1_Y_MODIFY = 0;
413         return 0;
414 }
415
416 ADI_ETHER_BUFFER *SetupRxBuffer(int no)
417 {
418         ADI_ETHER_FRAME_BUFFER *frmbuf;
419         ADI_ETHER_BUFFER *buf;
420         int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2;   /* ensure a multi. of 4 */
421         int total_size = nobytes_buffer + RECV_BUFSIZE;
422
423         buf = (ADI_ETHER_BUFFER *) (RXBUF_BASE_ADDR + no * total_size);
424         frmbuf =
425             (ADI_ETHER_FRAME_BUFFER *) (RXBUF_BASE_ADDR + no * total_size +
426                                         nobytes_buffer);
427
428         memset(buf, 0x00, nobytes_buffer);
429         buf->FrmData = frmbuf;
430         memset(frmbuf, 0xfe, RECV_BUFSIZE);
431
432         /* set up first desc to point to receive frame buffer */
433         buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
434         buf->Dma[0].START_ADDR = (u32) buf->FrmData;
435         buf->Dma[0].CONFIG.b_DMA_EN = 1;        /* enabled */
436         buf->Dma[0].CONFIG.b_WNR = 1;   /* Write to memory */
437         buf->Dma[0].CONFIG.b_WDSIZE = 2;        /* wordsize is 32 bits */
438         buf->Dma[0].CONFIG.b_NDSIZE = 5;        /* 5 half words is desc size. */
439         buf->Dma[0].CONFIG.b_FLOW = 7;  /* large desc flow */
440
441         /* set up second desc to point to status word */
442         buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
443         buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
444         buf->Dma[1].CONFIG.b_DMA_EN = 1;        /* enabled */
445         buf->Dma[1].CONFIG.b_WNR = 1;   /* Write to memory */
446         buf->Dma[1].CONFIG.b_WDSIZE = 2;        /* wordsize is 32 bits */
447         buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
448         buf->Dma[1].CONFIG.b_NDSIZE = 5;        /* must be 0 when FLOW is 0 */
449         buf->Dma[1].CONFIG.b_FLOW = 7;  /* stop */
450
451         return buf;
452 }
453
454 ADI_ETHER_BUFFER *SetupTxBuffer(int no)
455 {
456         ADI_ETHER_FRAME_BUFFER *frmbuf;
457         ADI_ETHER_BUFFER *buf;
458         int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2;   /* ensure a multi. of 4 */
459         int total_size = nobytes_buffer + RECV_BUFSIZE;
460
461         buf = (ADI_ETHER_BUFFER *) (TXBUF_BASE_ADDR + no * total_size);
462         frmbuf =
463             (ADI_ETHER_FRAME_BUFFER *) (TXBUF_BASE_ADDR + no * total_size +
464                                         nobytes_buffer);
465
466         memset(buf, 0x00, nobytes_buffer);
467         buf->FrmData = frmbuf;
468         memset(frmbuf, 0x00, RECV_BUFSIZE);
469
470         /* set up first desc to point to receive frame buffer */
471         buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
472         buf->Dma[0].START_ADDR = (u32) buf->FrmData;
473         buf->Dma[0].CONFIG.b_DMA_EN = 1;        /* enabled */
474         buf->Dma[0].CONFIG.b_WNR = 0;   /* Read to memory */
475         buf->Dma[0].CONFIG.b_WDSIZE = 2;        /* wordsize is 32 bits */
476         buf->Dma[0].CONFIG.b_NDSIZE = 5;        /* 5 half words is desc size. */
477         buf->Dma[0].CONFIG.b_FLOW = 7;  /* large desc flow */
478
479         /* set up second desc to point to status word */
480         buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
481         buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
482         buf->Dma[1].CONFIG.b_DMA_EN = 1;        /* enabled */
483         buf->Dma[1].CONFIG.b_WNR = 1;   /* Write to memory */
484         buf->Dma[1].CONFIG.b_WDSIZE = 2;        /* wordsize is 32 bits */
485         buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
486         buf->Dma[1].CONFIG.b_NDSIZE = 0;        /* must be 0 when FLOW is 0 */
487         buf->Dma[1].CONFIG.b_FLOW = 0;  /* stop */
488
489         return buf;
490 }
491
492 #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
493 int ether_post_test(int flags)
494 {
495         uchar buf[64];
496         int i, value = 0;
497         int length;
498
499         printf("\n--------");
500         bfin_EMAC_init(NULL, NULL);
501         /* construct the package */
502         buf[0] = buf[6] = (unsigned char)(*pEMAC_ADDRLO & 0xFF);
503         buf[1] = buf[7] = (unsigned char)((*pEMAC_ADDRLO & 0xFF00) >> 8);
504         buf[2] = buf[8] = (unsigned char)((*pEMAC_ADDRLO & 0xFF0000) >> 16);
505         buf[3] = buf[9] = (unsigned char)((*pEMAC_ADDRLO & 0xFF000000) >> 24);
506         buf[4] = buf[10] = (unsigned char)(*pEMAC_ADDRHI & 0xFF);
507         buf[5] = buf[11] = (unsigned char)((*pEMAC_ADDRHI & 0xFF00) >> 8);
508         buf[12] = 0x08;         /* Type: ARP */
509         buf[13] = 0x06;
510         buf[14] = 0x00;         /* Hardware type: Ethernet */
511         buf[15] = 0x01;
512         buf[16] = 0x08;         /* Protocal type: IP */
513         buf[17] = 0x00;
514         buf[18] = 0x06;         /* Hardware size    */
515         buf[19] = 0x04;         /* Protocol size    */
516         buf[20] = 0x00;         /* Opcode: request  */
517         buf[21] = 0x01;
518
519         for (i = 0; i < 42; i++)
520                 buf[i + 22] = i;
521         printf("--------Send 64 bytes......\n");
522         bfin_EMAC_send(NULL, (volatile void *)buf, 64);
523         for (i = 0; i < 100; i++) {
524                 udelay(10000);
525                 if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
526                         value = 1;
527                         break;
528                 }
529         }
530         if (value == 0) {
531                 printf("--------EMAC can't receive any data\n");
532                 eth_halt();
533                 return -1;
534         }
535         length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
536         for (i = 0; i < length; i++) {
537                 if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
538                         printf("--------EMAC receive error data!\n");
539                         eth_halt();
540                         return -1;
541                 }
542         }
543         printf("--------receive %d bytes, matched\n", length);
544         bfin_EMAC_halt(NULL);
545         return 0;
546 }
547 #endif