2 * PLB2800 internal switch ethernet driver.
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/addrspace.h>
17 #define NUM_RX_DESC PKTBUFSRX
18 #define TOUT_LOOP 1000000
20 #define LONG_REF(addr) (*((volatile unsigned long*)addr))
22 #define CMAC_CRX_CTRL LONG_REF(0xb800c870)
23 #define CMAC_CTX_CTRL LONG_REF(0xb800c874)
24 #define SYS_MAC_ADDR_0 LONG_REF(0xb800c878)
25 #define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c)
26 #define MIPS_H_MASK LONG_REF(0xB800C810)
28 #define MA_LEARN LONG_REF(0xb8008004)
29 #define DA_LOOKUP LONG_REF(0xb8008008)
31 #define CMAC_CRX_CTRL_PD 0x00000001
32 #define CMAC_CRX_CTRL_CG 0x00000002
33 #define CMAC_CRX_CTRL_PL_SHIFT 2
35 #define CMAC_NON_CRIT 0x1
36 #define MBOX_STAT_ID_SHF 28
37 #define MBOX_STAT_CP 0x80000000
38 #define MBOX_STAT_MB 0x00000001
39 #define EN_MA_LEARN 0x02000000
40 #define EN_DA_LKUP 0x01000000
41 #define MA_DEST_SHF 11
42 #define DA_DEST_SHF 11
43 #define DA_STATE_SHF 19
44 #define TSTAMP_MS 0x00000000
45 #define SW_H_MBOX4_MASK 0x08000000
46 #define SW_H_MBOX3_MASK 0x04000000
47 #define SW_H_MBOX2_MASK 0x02000000
48 #define SW_H_MBOX1_MASK 0x01000000
50 typedef volatile struct {
57 #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
59 typedef volatile struct {
65 #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
68 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
69 static int plb2800_eth_send(struct eth_device *dev, void *packet, int length);
70 static int plb2800_eth_recv(struct eth_device *dev);
71 static void plb2800_eth_halt(struct eth_device *dev);
73 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
74 static unsigned char * plb2800_get_mac_addr(void);
77 static int mac_addr_set = 0;
80 int plb2800_eth_initialize(bd_t * bis)
82 struct eth_device *dev;
86 printf("Entered plb2800_eth_initialize()\n");
89 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
91 printf("Failed to allocate memory\n");
94 memset(dev, 0, sizeof(*dev));
96 sprintf(dev->name, "PLB2800 Switch");
97 dev->init = plb2800_eth_init;
98 dev->halt = plb2800_eth_halt;
99 dev->send = plb2800_eth_send;
100 dev->recv = plb2800_eth_recv;
105 *(ulong *)0xb800e800 = 0x838;
107 /* Set MBOX ownership */
108 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
109 MBOX_REG(0)->stat = temp;
110 MBOX_REG(1)->stat = temp;
112 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
113 MBOX_REG(2)->stat = temp;
114 MBOX_REG(3)->stat = temp;
116 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
118 /* Disable all Mbox interrupt */
120 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
124 printf("Leaving plb2800_eth_initialize()\n");
130 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
133 printf("Entering plb2800_eth_init()\n");
136 plb2800_set_mac_addr(dev, dev->enetaddr);
141 printf("Leaving plb2800_eth_init()\n");
148 static int plb2800_eth_send(struct eth_device *dev, void *packet, int length)
153 mailbox_t * mb = MBOX_REG(0);
154 char * mem = MBOX_MEM(0);
157 printf("Entered plb2800_eth_send()\n");
162 printf ("%s: bad packet size: %d\n", dev->name, length);
171 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
174 printf("0 mb->stat = 0x%x\n", mb->stat);
177 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
181 printf("%s: tx buffer not ready\n", dev->name);
182 printf("1 mb->stat = 0x%x\n", mb->stat);
187 /* For some strange reason, memcpy doesn't work, here!
191 int words = (length >> 2) + 1;
192 unsigned int* dst = (unsigned int*)(mem);
193 unsigned int* src = (unsigned int*)(packet);
194 for (i = 0; i < words; i++)
202 CMAC_CRX_CTRL = temp;
203 mb->cmd = MBOX_STAT_CP;
206 printf("2 mb->stat = 0x%x\n", mb->stat);
213 printf("Leaving plb2800_eth_send()\n");
220 static int plb2800_eth_recv(struct eth_device *dev)
223 mailbox_t * mbox = MBOX_REG(3);
224 unsigned char * hdr = MBOX_MEM(3);
228 printf("Entered plb2800_eth_recv()\n");
235 if (!(stat & MBOX_STAT_CP))
240 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
241 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
243 stat &= ~MBOX_STAT_CP;
248 for (i=0;i<length - 4;i++)
250 if (i % 16 == 0) printf("\n%04x: ", i);
251 printf("%02X ", NetRxPackets[rx_new][i]);
260 printf("Received %d bytes\n", length);
262 NetReceive((void*)(NetRxPackets[rx_new]),
268 printf("Zero length!!!\n");
272 rx_new = (rx_new + 1) % NUM_RX_DESC;
276 printf("Leaving plb2800_eth_recv()\n");
283 static void plb2800_eth_halt(struct eth_device *dev)
286 printf("Entered plb2800_eth_halt()\n");
290 printf("Leaving plb2800_eth_halt()\n");
294 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
301 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
306 /* send one packet through CPU port
307 * in order to learn system MAC address
310 /* Set DA_LOOKUP register */
311 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
314 /* Set MA_LEARN register */
315 temp = 50 << MA_DEST_SHF; /* static entry */
318 /* set destination address */
322 /* set source address = system MAC address */
324 packet[6+ix] = addr[ix];
331 for(ix=14;ix<60;ix++)
336 printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
340 plb2800_eth_send(dev, packet, sizeof(packet));
342 /* delay for a while */
343 for(ix=0;ix<65535;ix++)
346 /* Set CMAC_CTX_CTRL register */
347 temp = TSTAMP_MS; /* no autocast */
348 CMAC_CTX_CTRL = temp;
350 /* Set DA_LOOKUP register */
357 static unsigned char * plb2800_get_mac_addr(void)
359 static unsigned char addr[6];
363 tmp = getenv ("ethaddr");
364 if (NULL == tmp) return NULL;
366 for (i=0; i<6; i++) {
367 addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
369 tmp = (*end) ? end+1 : end;