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,
30 #include <asm/addrspace.h>
33 #define NUM_RX_DESC PKTBUFSRX
34 #define TOUT_LOOP 1000000
36 #define LONG_REF(addr) (*((volatile unsigned long*)addr))
38 #define CMAC_CRX_CTRL LONG_REF(0xb800c870)
39 #define CMAC_CTX_CTRL LONG_REF(0xb800c874)
40 #define SYS_MAC_ADDR_0 LONG_REF(0xb800c878)
41 #define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c)
42 #define MIPS_H_MASK LONG_REF(0xB800C810)
44 #define MA_LEARN LONG_REF(0xb8008004)
45 #define DA_LOOKUP LONG_REF(0xb8008008)
47 #define CMAC_CRX_CTRL_PD 0x00000001
48 #define CMAC_CRX_CTRL_CG 0x00000002
49 #define CMAC_CRX_CTRL_PL_SHIFT 2
51 #define CMAC_NON_CRIT 0x1
52 #define MBOX_STAT_ID_SHF 28
53 #define MBOX_STAT_CP 0x80000000
54 #define MBOX_STAT_MB 0x00000001
55 #define EN_MA_LEARN 0x02000000
56 #define EN_DA_LKUP 0x01000000
57 #define MA_DEST_SHF 11
58 #define DA_DEST_SHF 11
59 #define DA_STATE_SHF 19
60 #define TSTAMP_MS 0x00000000
61 #define SW_H_MBOX4_MASK 0x08000000
62 #define SW_H_MBOX3_MASK 0x04000000
63 #define SW_H_MBOX2_MASK 0x02000000
64 #define SW_H_MBOX1_MASK 0x01000000
66 typedef volatile struct {
73 #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
75 typedef volatile struct {
81 #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
84 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
85 static int plb2800_eth_send(struct eth_device *dev, void *packet, int length);
86 static int plb2800_eth_recv(struct eth_device *dev);
87 static void plb2800_eth_halt(struct eth_device *dev);
89 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
90 static unsigned char * plb2800_get_mac_addr(void);
93 static int mac_addr_set = 0;
96 int plb2800_eth_initialize(bd_t * bis)
98 struct eth_device *dev;
102 printf("Entered plb2800_eth_initialize()\n");
105 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
107 printf("Failed to allocate memory\n");
110 memset(dev, 0, sizeof(*dev));
112 sprintf(dev->name, "PLB2800 Switch");
113 dev->init = plb2800_eth_init;
114 dev->halt = plb2800_eth_halt;
115 dev->send = plb2800_eth_send;
116 dev->recv = plb2800_eth_recv;
121 *(ulong *)0xb800e800 = 0x838;
123 /* Set MBOX ownership */
124 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
125 MBOX_REG(0)->stat = temp;
126 MBOX_REG(1)->stat = temp;
128 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
129 MBOX_REG(2)->stat = temp;
130 MBOX_REG(3)->stat = temp;
132 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
134 /* Disable all Mbox interrupt */
136 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
140 printf("Leaving plb2800_eth_initialize()\n");
146 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
149 printf("Entering plb2800_eth_init()\n");
152 plb2800_set_mac_addr(dev, dev->enetaddr);
157 printf("Leaving plb2800_eth_init()\n");
164 static int plb2800_eth_send(struct eth_device *dev, void *packet, int length)
169 mailbox_t * mb = MBOX_REG(0);
170 char * mem = MBOX_MEM(0);
173 printf("Entered plb2800_eth_send()\n");
178 printf ("%s: bad packet size: %d\n", dev->name, length);
187 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
190 printf("0 mb->stat = 0x%x\n", mb->stat);
193 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
197 printf("%s: tx buffer not ready\n", dev->name);
198 printf("1 mb->stat = 0x%x\n", mb->stat);
203 /* For some strange reason, memcpy doesn't work, here!
207 int words = (length >> 2) + 1;
208 unsigned int* dst = (unsigned int*)(mem);
209 unsigned int* src = (unsigned int*)(packet);
210 for (i = 0; i < words; i++)
218 CMAC_CRX_CTRL = temp;
219 mb->cmd = MBOX_STAT_CP;
222 printf("2 mb->stat = 0x%x\n", mb->stat);
229 printf("Leaving plb2800_eth_send()\n");
236 static int plb2800_eth_recv(struct eth_device *dev)
239 mailbox_t * mbox = MBOX_REG(3);
240 unsigned char * hdr = MBOX_MEM(3);
244 printf("Entered plb2800_eth_recv()\n");
251 if (!(stat & MBOX_STAT_CP))
256 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
257 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
259 stat &= ~MBOX_STAT_CP;
264 for (i=0;i<length - 4;i++)
266 if (i % 16 == 0) printf("\n%04x: ", i);
267 printf("%02X ", NetRxPackets[rx_new][i]);
276 printf("Received %d bytes\n", length);
278 NetReceive((void*)(NetRxPackets[rx_new]),
284 printf("Zero length!!!\n");
288 rx_new = (rx_new + 1) % NUM_RX_DESC;
292 printf("Leaving plb2800_eth_recv()\n");
299 static void plb2800_eth_halt(struct eth_device *dev)
302 printf("Entered plb2800_eth_halt()\n");
306 printf("Leaving plb2800_eth_halt()\n");
310 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
317 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
322 /* send one packet through CPU port
323 * in order to learn system MAC address
326 /* Set DA_LOOKUP register */
327 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
330 /* Set MA_LEARN register */
331 temp = 50 << MA_DEST_SHF; /* static entry */
334 /* set destination address */
338 /* set source address = system MAC address */
340 packet[6+ix] = addr[ix];
347 for(ix=14;ix<60;ix++)
352 printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
356 plb2800_eth_send(dev, packet, sizeof(packet));
358 /* delay for a while */
359 for(ix=0;ix<65535;ix++)
362 /* Set CMAC_CTX_CTRL register */
363 temp = TSTAMP_MS; /* no autocast */
364 CMAC_CTX_CTRL = temp;
366 /* Set DA_LOOKUP register */
373 static unsigned char * plb2800_get_mac_addr(void)
375 static unsigned char addr[6];
379 tmp = getenv ("ethaddr");
380 if (NULL == tmp) return NULL;
382 for (i=0; i<6; i++) {
383 addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
385 tmp = (*end) ? end+1 : end;