2 * DANUBE internal switch ethernet driver.
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) \
30 && defined(CONFIG_DANUBE_SWITCH)
34 #include <asm/danube.h>
35 #include <asm/addrspace.h>
36 #include <asm/pinstrap.h>
39 #define REV_MII_MODE 2
44 #define NUM_RX_DESC PKTBUFSRX
46 #define MAX_PACKET_SIZE 1536
48 #define PHY0_ADDR 1 /*fixme: set the correct value here*/
50 #define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value
51 #define DMA_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
53 #define SW_WRITE_REG(reg, value) *((volatile u32*)reg) = (u32)value
54 #define SW_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
66 volatile u32 reserved :3;
67 volatile u32 Byteoffset :2;
68 volatile u32 reserve :7;
69 volatile u32 DataLen :16;
76 } danube_rx_descriptor_t;
88 volatile u32 Byteoffset :5;
89 volatile u32 reserved :7;
90 volatile u32 DataLen :16;
97 } danube_tx_descriptor_t;
102 static danube_rx_descriptor_t rx_des_ring[NUM_RX_DESC] __attribute__ ((aligned(8)));
103 static danube_tx_descriptor_t tx_des_ring[NUM_TX_DESC] __attribute__ ((aligned(8)));
104 static int tx_num, rx_num;
106 int danube_switch_init(struct eth_device *dev, bd_t * bis);
107 int danube_switch_send(struct eth_device *dev, volatile void *packet,int length);
108 int danube_switch_recv(struct eth_device *dev);
109 void danube_switch_halt(struct eth_device *dev);
110 static void danube_init_switch_chip(int mode);
111 static void danube_dma_init(void);
115 int danube_switch_initialize(bd_t * bis)
117 struct eth_device *dev;
120 printf("Entered danube_switch_initialize()\n");
123 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
125 printf("Failed to allocate memory\n");
128 memset(dev, 0, sizeof(*dev));
131 danube_init_switch_chip(REV_MII_MODE);
133 #ifdef CLK_OUT2_25MHZ
134 *DANUBE_GPIO_P0_DIR=0x0000ae78;
135 *DANUBE_GPIO_P0_ALTSEL0=0x00008078;
136 //joelin for Mii-1 *DANUBE_GPIO_P0_ALTSEL1=0x80000080;
137 *DANUBE_GPIO_P0_ALTSEL1=0x80000000; //joelin for Mii-1
138 *DANUBE_CGU_IFCCR=0x00400010;
139 *DANUBE_GPIO_P0_OD=0x0000ae78;
144 *DANUBE_RCU_RST_REQ |=1;
146 *DANUBE_RCU_RST_REQ &=(unsigned long)~1;
148 /*while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
149 *DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
151 /*while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
152 *DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
155 sprintf(dev->name, "danube Switch");
156 dev->init = danube_switch_init;
157 dev->halt = danube_switch_halt;
158 dev->send = danube_switch_send;
159 dev->recv = danube_switch_recv;
164 printf("Leaving danube_switch_initialize()\n");
166 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
167 *DANUBE_PPE_ETOP_MDIO_ACC =0x8001840F;
168 while((*DANUBE_PPE_ETOP_MDIO_ACC)&0x80000000);
169 *DANUBE_PPE_ETOP_MDIO_ACC =0x8003840F;
170 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
171 *DANUBE_PPE_ETOP_MDIO_ACC =0x8005840F;
172 //while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
173 //*DANUBE_PPE_ETOP_MDIO_ACC =0x8006840F;
174 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
175 *DANUBE_PPE_ETOP_MDIO_ACC =0x8007840F;
176 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
177 *DANUBE_PPE_ETOP_MDIO_ACC =0x8008840F;
178 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
179 *DANUBE_PPE_ETOP_MDIO_ACC =0x8001840F;
180 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
181 *DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
182 #ifdef CLK_OUT2_25MHZ
183 while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
184 *DANUBE_PPE_ETOP_MDIO_ACC =0x80334000;
190 int danube_switch_init(struct eth_device *dev, bd_t * bis)
199 // serial_puts("i \n\0");
201 *DANUBE_DMA_CS=RX_CHAN_NO;
202 *DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
203 *DANUBE_DMA_CPOLL= 0x80000040;
204 /*set descriptor base*/
205 *DANUBE_DMA_CDBA=(u32)rx_des_ring;
206 *DANUBE_DMA_CDLEN=NUM_RX_DESC;
208 *DANUBE_DMA_CCTRL=0x30000;
210 *DANUBE_DMA_CS=TX_CHAN_NO;
211 *DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
212 *DANUBE_DMA_CPOLL= 0x80000040;
213 *DANUBE_DMA_CDBA=(u32)tx_des_ring;
214 *DANUBE_DMA_CDLEN=NUM_TX_DESC;
216 *DANUBE_DMA_CCTRL=0x30100;
218 for(i=0;i < NUM_RX_DESC; i++)
220 danube_rx_descriptor_t * rx_desc = KSEG1ADDR(&rx_des_ring[i]);
221 rx_desc->status.word=0;
222 rx_desc->status.field.OWN=1;
223 rx_desc->status.field.DataLen=PKTSIZE_ALIGN; /* 1536 */
224 rx_desc->DataPtr=(u32)KSEG1ADDR(NetRxPackets[i]);
227 for(i=0;i < NUM_TX_DESC; i++)
229 danube_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_des_ring[i]);
230 memset(tx_desc, 0, sizeof(tx_des_ring[0]));
232 /* turn on DMA rx & tx channel
234 *DANUBE_DMA_CS=RX_CHAN_NO;
235 *DANUBE_DMA_CCTRL|=1;/*reset and turn on the channel*/
240 void danube_switch_halt(struct eth_device *dev)
246 *DANUBE_DMA_CCTRL&=~1;/*stop the dma channel*/
251 int danube_switch_send(struct eth_device *dev, volatile void *packet,int length)
257 danube_tx_descriptor_t * tx_desc= KSEG1ADDR(&tx_des_ring[tx_num]);
261 printf ("%s: bad packet size: %d\n", dev->name, length);
265 for(i=0; tx_desc->status.field.OWN==1; i++)
269 printf("NO Tx Descriptor...");
276 tx_desc->status.field.Sop=1;
277 tx_desc->status.field.Eop=1;
278 tx_desc->status.field.C=0;
279 tx_desc->DataPtr = (u32)KSEG1ADDR(packet);
281 tx_desc->status.field.DataLen = 60;
283 tx_desc->status.field.DataLen = (u32)length;
286 tx_desc->status.field.OWN=1;
290 if(tx_num==NUM_TX_DESC) tx_num=0;
291 *DANUBE_DMA_CS=TX_CHAN_NO;
293 if(!(*DANUBE_DMA_CCTRL & 1))
294 *DANUBE_DMA_CCTRL|=1;
300 int danube_switch_recv(struct eth_device *dev)
305 danube_rx_descriptor_t * rx_desc;
310 rx_desc = KSEG1ADDR(&rx_des_ring[rx_num]);
312 if ((rx_desc->status.field.C == 0) || (rx_desc->status.field.OWN == 1))
318 length = rx_desc->status.field.DataLen;
321 NetReceive((void*)KSEG1ADDR(NetRxPackets[rx_num]), length - 4);
326 printf("Zero length!!!\n");
329 rx_desc->status.field.Sop=0;
330 rx_desc->status.field.Eop=0;
331 rx_desc->status.field.C=0;
332 rx_desc->status.field.DataLen=PKTSIZE_ALIGN;
333 rx_desc->status.field.OWN=1;
335 if(rx_num==NUM_RX_DESC) rx_num=0;
343 static void danube_init_switch_chip(int mode)
346 /*get and set mac address for MAC*/
347 static unsigned char addr[6];
349 tmp = getenv ("ethaddr");
351 printf("Can't get environment ethaddr!!!\n");
354 printf("ethaddr=%s\n", tmp);
356 *DANUBE_PMU_PWDCR = *DANUBE_PMU_PWDCR & 0xFFFFEFDF;
357 *DANUBE_PPE32_ETOP_MDIO_CFG &= ~0x6;
358 *DANUBE_PPE32_ENET_MAC_CFG = 0x187;
360 // turn on port0, set to rmii and turn off port1.
361 if(mode==REV_MII_MODE)
363 *DANUBE_PPE32_ETOP_CFG = (*DANUBE_PPE32_ETOP_CFG & 0xfffffffc) | 0x0000000a;
365 else if (mode == MII_MODE)
367 *DANUBE_PPE32_ETOP_CFG = (*DANUBE_PPE32_ETOP_CFG & 0xfffffffc) | 0x00000008;
370 *DANUBE_PPE32_ETOP_IG_PLEN_CTRL = 0x4005ee; // set packetlen.
371 *ENET_MAC_CFG|=1<<11;/*enable the crc*/
376 static void danube_dma_init(void)
379 // serial_puts("d \n\0");
381 *DANUBE_PMU_PWDCR &=~(1<<DANUBE_PMU_DMA_SHIFT);/*enable DMA from PMU*/
385 *DANUBE_DMA_IRNEN=0;/*disable all the interrupts first*/
387 /* Clear Interrupt Status Register
389 *DANUBE_DMA_IRNCR=0xfffff;
390 /*disable all the dma interrupts*/
392 /*disable channel 0 and channel 1 interrupts*/
394 *DANUBE_DMA_CS=RX_CHAN_NO;
395 *DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
396 *DANUBE_DMA_CPOLL= 0x80000040;
397 /*set descriptor base*/
398 *DANUBE_DMA_CDBA=(u32)rx_des_ring;
399 *DANUBE_DMA_CDLEN=NUM_RX_DESC;
401 *DANUBE_DMA_CCTRL=0x30000;
403 *DANUBE_DMA_CS=TX_CHAN_NO;
404 *DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
405 *DANUBE_DMA_CPOLL= 0x80000040;
406 *DANUBE_DMA_CDBA=(u32)tx_des_ring;
407 *DANUBE_DMA_CDLEN=NUM_TX_DESC;
409 *DANUBE_DMA_CCTRL=0x30100;
410 /*enable the poll function and set the poll counter*/
411 //*DANUBE_DMA_CPOLL=DANUBE_DMA_POLL_EN | (DANUBE_DMA_POLL_COUNT<<4);
412 /*set port properties, enable endian conversion for switch*/
414 *DANUBE_DMA_PCTRL|=0xf<<8;/*enable 32 bit endian conversion*/