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