2 * PLB2800 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,
28 #if defined(CONFIG_CMD_NET) \
29 && defined(CONFIG_NET_MULTI) && defined(CONFIG_PLB2800_ETHER)
33 #include <asm/addrspace.h>
36 #define NUM_RX_DESC PKTBUFSRX
37 #define TOUT_LOOP 1000000
39 #define LONG_REF(addr) (*((volatile unsigned long*)addr))
41 #define CMAC_CRX_CTRL LONG_REF(0xb800c870)
42 #define CMAC_CTX_CTRL LONG_REF(0xb800c874)
43 #define SYS_MAC_ADDR_0 LONG_REF(0xb800c878)
44 #define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c)
45 #define MIPS_H_MASK LONG_REF(0xB800C810)
47 #define MA_LEARN LONG_REF(0xb8008004)
48 #define DA_LOOKUP LONG_REF(0xb8008008)
50 #define CMAC_CRX_CTRL_PD 0x00000001
51 #define CMAC_CRX_CTRL_CG 0x00000002
52 #define CMAC_CRX_CTRL_PL_SHIFT 2
54 #define CMAC_NON_CRIT 0x1
55 #define MBOX_STAT_ID_SHF 28
56 #define MBOX_STAT_CP 0x80000000
57 #define MBOX_STAT_MB 0x00000001
58 #define EN_MA_LEARN 0x02000000
59 #define EN_DA_LKUP 0x01000000
60 #define MA_DEST_SHF 11
61 #define DA_DEST_SHF 11
62 #define DA_STATE_SHF 19
63 #define TSTAMP_MS 0x00000000
64 #define SW_H_MBOX4_MASK 0x08000000
65 #define SW_H_MBOX3_MASK 0x04000000
66 #define SW_H_MBOX2_MASK 0x02000000
67 #define SW_H_MBOX1_MASK 0x01000000
69 typedef volatile struct {
76 #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
78 typedef volatile struct {
84 #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
87 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
88 static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
90 static int plb2800_eth_recv(struct eth_device *dev);
91 static void plb2800_eth_halt(struct eth_device *dev);
93 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
94 static unsigned char * plb2800_get_mac_addr(void);
97 static int mac_addr_set = 0;
100 int plb2800_eth_initialize(bd_t * bis)
102 struct eth_device *dev;
106 printf("Entered plb2800_eth_initialize()\n");
109 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
111 printf("Failed to allocate memory\n");
114 memset(dev, 0, sizeof(*dev));
116 sprintf(dev->name, "PLB2800 Switch");
117 dev->init = plb2800_eth_init;
118 dev->halt = plb2800_eth_halt;
119 dev->send = plb2800_eth_send;
120 dev->recv = plb2800_eth_recv;
125 *(ulong *)0xb800e800 = 0x838;
127 /* Set MBOX ownership */
128 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
129 MBOX_REG(0)->stat = temp;
130 MBOX_REG(1)->stat = temp;
132 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
133 MBOX_REG(2)->stat = temp;
134 MBOX_REG(3)->stat = temp;
136 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
138 /* Disable all Mbox interrupt */
140 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
144 printf("Leaving plb2800_eth_initialize()\n");
150 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
153 printf("Entering plb2800_eth_init()\n");
156 plb2800_set_mac_addr(dev, dev->enetaddr);
161 printf("Leaving plb2800_eth_init()\n");
168 static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
174 mailbox_t * mb = MBOX_REG(0);
175 char * mem = MBOX_MEM(0);
178 printf("Entered plb2800_eth_send()\n");
183 printf ("%s: bad packet size: %d\n", dev->name, length);
192 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
195 printf("0 mb->stat = 0x%x\n", mb->stat);
198 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
202 printf("%s: tx buffer not ready\n", dev->name);
203 printf("1 mb->stat = 0x%x\n", mb->stat);
208 /* For some strange reason, memcpy doesn't work, here!
212 int words = (length >> 2) + 1;
213 unsigned int* dst = (unsigned int*)(mem);
214 unsigned int* src = (unsigned int*)(packet);
215 for (i = 0; i < words; i++)
223 CMAC_CRX_CTRL = temp;
224 mb->cmd = MBOX_STAT_CP;
227 printf("2 mb->stat = 0x%x\n", mb->stat);
234 printf("Leaving plb2800_eth_send()\n");
241 static int plb2800_eth_recv(struct eth_device *dev)
244 mailbox_t * mbox = MBOX_REG(3);
245 unsigned char * hdr = MBOX_MEM(3);
249 printf("Entered plb2800_eth_recv()\n");
256 if (!(stat & MBOX_STAT_CP))
261 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
262 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
264 stat &= ~MBOX_STAT_CP;
269 for (i=0;i<length - 4;i++)
271 if (i % 16 == 0) printf("\n%04x: ", i);
272 printf("%02X ", NetRxPackets[rx_new][i]);
281 printf("Received %d bytes\n", length);
283 NetReceive((void*)(NetRxPackets[rx_new]),
289 printf("Zero length!!!\n");
293 rx_new = (rx_new + 1) % NUM_RX_DESC;
297 printf("Leaving plb2800_eth_recv()\n");
304 static void plb2800_eth_halt(struct eth_device *dev)
307 printf("Entered plb2800_eth_halt()\n");
311 printf("Leaving plb2800_eth_halt()\n");
315 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
322 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
327 /* send one packet through CPU port
328 * in order to learn system MAC address
331 /* Set DA_LOOKUP register */
332 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
335 /* Set MA_LEARN register */
336 temp = 50 << MA_DEST_SHF; /* static entry */
339 /* set destination address */
343 /* set source address = system MAC address */
345 packet[6+ix] = addr[ix];
352 for(ix=14;ix<60;ix++)
357 printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
361 plb2800_eth_send(dev, packet, sizeof(packet));
363 /* delay for a while */
364 for(ix=0;ix<65535;ix++)
367 /* Set CMAC_CTX_CTRL register */
368 temp = TSTAMP_MS; /* no autocast */
369 CMAC_CTX_CTRL = temp;
371 /* Set DA_LOOKUP register */
378 static unsigned char * plb2800_get_mac_addr(void)
380 static unsigned char addr[6];
384 tmp = getenv ("ethaddr");
385 if (NULL == tmp) return NULL;
387 for (i=0; i<6; i++) {
388 addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
390 tmp = (*end) ? end+1 : end;
396 #endif /* CONFIG_PLB2800_ETHER */