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