blob: f869514f29511322ff3ff9414ccfa6c3f315e527 [file] [log] [blame]
wdenk60fbe252003-04-08 23:25:21 +00001/*
2 * PLB2800 internal switch ethernet driver.
3 *
4 * (C) Copyright 2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
wdenk60fbe252003-04-08 23:25:21 +00008 */
9
10#include <common.h>
wdenk60fbe252003-04-08 23:25:21 +000011#include <malloc.h>
12#include <net.h>
Ben Warren4fce2ac2008-08-31 10:40:51 -070013#include <netdev.h>
wdenk60fbe252003-04-08 23:25:21 +000014#include <asm/addrspace.h>
15
16
17#define NUM_RX_DESC PKTBUFSRX
18#define TOUT_LOOP 1000000
19
20#define LONG_REF(addr) (*((volatile unsigned long*)addr))
21
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)
27
28#define MA_LEARN LONG_REF(0xb8008004)
29#define DA_LOOKUP LONG_REF(0xb8008008)
30
31#define CMAC_CRX_CTRL_PD 0x00000001
32#define CMAC_CRX_CTRL_CG 0x00000002
33#define CMAC_CRX_CTRL_PL_SHIFT 2
34#define CMAC_CRIT 0x0
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
wdenk8bde7f72003-06-27 21:31:46 +000041#define MA_DEST_SHF 11
wdenk60fbe252003-04-08 23:25:21 +000042#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
49
50typedef volatile struct {
51 unsigned int stat;
52 unsigned int cmd;
53 unsigned int cnt;
54 unsigned int adr;
55} mailbox_t;
56
57#define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
58
59typedef volatile struct {
60 unsigned int word0;
61 unsigned int word1;
62 unsigned int word2;
63} mbhdr_t;
64
65#define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
66
67
68static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
Joe Hershberger10cbe3b2012-05-22 18:36:19 +000069static int plb2800_eth_send(struct eth_device *dev, void *packet, int length);
wdenk60fbe252003-04-08 23:25:21 +000070static int plb2800_eth_recv(struct eth_device *dev);
71static void plb2800_eth_halt(struct eth_device *dev);
72
73static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
74static unsigned char * plb2800_get_mac_addr(void);
75
76static int rx_new;
77static int mac_addr_set = 0;
78
79
80int plb2800_eth_initialize(bd_t * bis)
81{
82 struct eth_device *dev;
83 ulong temp;
84
85#ifdef DEBUG
86 printf("Entered plb2800_eth_initialize()\n");
87#endif
88
89 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
90 {
91 printf("Failed to allocate memory\n");
Ben Warren4fce2ac2008-08-31 10:40:51 -070092 return -1;
wdenk60fbe252003-04-08 23:25:21 +000093 }
94 memset(dev, 0, sizeof(*dev));
95
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;
101
102 eth_register(dev);
103
wdenk8bde7f72003-06-27 21:31:46 +0000104 /* bug fix */
105 *(ulong *)0xb800e800 = 0x838;
wdenk60fbe252003-04-08 23:25:21 +0000106
107 /* Set MBOX ownership */
108 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
109 MBOX_REG(0)->stat = temp;
110 MBOX_REG(1)->stat = temp;
111
wdenk8bde7f72003-06-27 21:31:46 +0000112 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
wdenk60fbe252003-04-08 23:25:21 +0000113 MBOX_REG(2)->stat = temp;
114 MBOX_REG(3)->stat = temp;
wdenk8bde7f72003-06-27 21:31:46 +0000115
wdenk60fbe252003-04-08 23:25:21 +0000116 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
117
118 /* Disable all Mbox interrupt */
119 temp = MIPS_H_MASK;
wdenk8bde7f72003-06-27 21:31:46 +0000120 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
wdenk60fbe252003-04-08 23:25:21 +0000121 MIPS_H_MASK = temp;
122
123#ifdef DEBUG
124 printf("Leaving plb2800_eth_initialize()\n");
125#endif
126
Ben Warren4fce2ac2008-08-31 10:40:51 -0700127 return 0;
wdenk60fbe252003-04-08 23:25:21 +0000128}
129
130static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
131{
132#ifdef DEBUG
133 printf("Entering plb2800_eth_init()\n");
134#endif
135
136 plb2800_set_mac_addr(dev, dev->enetaddr);
137
138 rx_new = 0;
139
140#ifdef DEBUG
141 printf("Leaving plb2800_eth_init()\n");
142#endif
143
144 return 0;
145}
146
147
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000148static int plb2800_eth_send(struct eth_device *dev, void *packet, int length)
wdenk60fbe252003-04-08 23:25:21 +0000149{
150 int i;
151 int res = -1;
152 u32 temp;
153 mailbox_t * mb = MBOX_REG(0);
154 char * mem = MBOX_MEM(0);
155
156#ifdef DEBUG
157 printf("Entered plb2800_eth_send()\n");
158#endif
159
160 if (length <= 0)
161 {
162 printf ("%s: bad packet size: %d\n", dev->name, length);
163 goto Done;
164 }
165
166 if (length < 64)
167 {
168 length = 64;
169 }
170
171 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
172
173#ifdef DEBUG
174 printf("0 mb->stat = 0x%x\n", mb->stat);
175#endif
176
177 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
178 {
179 if (i >= TOUT_LOOP)
180 {
181 printf("%s: tx buffer not ready\n", dev->name);
182 printf("1 mb->stat = 0x%x\n", mb->stat);
183 goto Done;
184 }
185 }
186
187 /* For some strange reason, memcpy doesn't work, here!
188 */
189 do
190 {
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++)
195 {
196 *dst = *src;
197 dst++;
198 src++;
199 };
200 } while(0);
201
202 CMAC_CRX_CTRL = temp;
203 mb->cmd = MBOX_STAT_CP;
wdenk8bde7f72003-06-27 21:31:46 +0000204
wdenk60fbe252003-04-08 23:25:21 +0000205#ifdef DEBUG
206 printf("2 mb->stat = 0x%x\n", mb->stat);
207#endif
208
209 res = length;
210Done:
211
212#ifdef DEBUG
213 printf("Leaving plb2800_eth_send()\n");
214#endif
215
216 return res;
217}
218
219
220static int plb2800_eth_recv(struct eth_device *dev)
221{
222 int length = 0;
223 mailbox_t * mbox = MBOX_REG(3);
224 unsigned char * hdr = MBOX_MEM(3);
225 unsigned int stat;
226
227#ifdef DEBUG
228 printf("Entered plb2800_eth_recv()\n");
229#endif
230
231 for (;;)
232 {
233 stat = mbox->stat;
234
235 if (!(stat & MBOX_STAT_CP))
236 {
237 break;
238 }
wdenk8bde7f72003-06-27 21:31:46 +0000239
wdenk60fbe252003-04-08 23:25:21 +0000240 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
241 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
242
243 stat &= ~MBOX_STAT_CP;
244 mbox->stat = stat;
245#ifdef DEBUG
246 {
247 int i;
248 for (i=0;i<length - 4;i++)
249 {
250 if (i % 16 == 0) printf("\n%04x: ", i);
251 printf("%02X ", NetRxPackets[rx_new][i]);
252 }
253 printf("\n");
254 }
255#endif
256
257 if (length)
258 {
259#ifdef DEBUG
260 printf("Received %d bytes\n", length);
261#endif
262 NetReceive((void*)(NetRxPackets[rx_new]),
Wolfgang Denk93e14592013-10-04 17:43:24 +0200263 length - 4);
wdenk60fbe252003-04-08 23:25:21 +0000264 }
265 else
266 {
267#if 1
268 printf("Zero length!!!\n");
269#endif
270 }
271
272 rx_new = (rx_new + 1) % NUM_RX_DESC;
273 }
274
275#ifdef DEBUG
276 printf("Leaving plb2800_eth_recv()\n");
277#endif
278
279 return length;
280}
281
282
283static void plb2800_eth_halt(struct eth_device *dev)
284{
285#ifdef DEBUG
286 printf("Entered plb2800_eth_halt()\n");
287#endif
288
289#ifdef DEBUG
290 printf("Leaving plb2800_eth_halt()\n");
291#endif
292}
293
294static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
295{
296 char packet[60];
297 ulong temp;
298 int ix;
299
300 if (mac_addr_set ||
301 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
302 {
303 return;
304 }
wdenk8bde7f72003-06-27 21:31:46 +0000305
wdenk60fbe252003-04-08 23:25:21 +0000306 /* send one packet through CPU port
307 * in order to learn system MAC address
wdenk8bde7f72003-06-27 21:31:46 +0000308 */
wdenk60fbe252003-04-08 23:25:21 +0000309
310 /* Set DA_LOOKUP register */
wdenk8bde7f72003-06-27 21:31:46 +0000311 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
wdenk60fbe252003-04-08 23:25:21 +0000312 DA_LOOKUP = temp;
313
314 /* Set MA_LEARN register */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200315 temp = 50 << MA_DEST_SHF; /* static entry */
wdenk60fbe252003-04-08 23:25:21 +0000316 MA_LEARN = temp;
317
318 /* set destination address */
319 for (ix=0;ix<6;ix++)
320 packet[ix] = 0xff;
wdenk8bde7f72003-06-27 21:31:46 +0000321
wdenk60fbe252003-04-08 23:25:21 +0000322 /* set source address = system MAC address */
323 for (ix=0;ix<6;ix++)
324 packet[6+ix] = addr[ix];
325
326 /* set type field */
327 packet[12]=0xaa;
328 packet[13]=0x55;
329
330 /* set data field */
331 for(ix=14;ix<60;ix++)
332 packet[ix] = 0x00;
wdenk8bde7f72003-06-27 21:31:46 +0000333
wdenk60fbe252003-04-08 23:25:21 +0000334#ifdef DEBUG
335 for (ix=0;ix<6;ix++)
336 printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
337#endif
338
339 /* set one packet */
340 plb2800_eth_send(dev, packet, sizeof(packet));
341
342 /* delay for a while */
343 for(ix=0;ix<65535;ix++)
344 temp = ~temp;
345
wdenk8bde7f72003-06-27 21:31:46 +0000346 /* Set CMAC_CTX_CTRL register */
wdenk60fbe252003-04-08 23:25:21 +0000347 temp = TSTAMP_MS; /* no autocast */
348 CMAC_CTX_CTRL = temp;
wdenk8bde7f72003-06-27 21:31:46 +0000349
wdenk60fbe252003-04-08 23:25:21 +0000350 /* Set DA_LOOKUP register */
351 temp = EN_DA_LKUP;
352 DA_LOOKUP = temp;
353
354 mac_addr_set = 1;
355}
356
357static unsigned char * plb2800_get_mac_addr(void)
358{
359 static unsigned char addr[6];
360 char *tmp, *end;
361 int i;
wdenk8bde7f72003-06-27 21:31:46 +0000362
wdenk60fbe252003-04-08 23:25:21 +0000363 tmp = getenv ("ethaddr");
364 if (NULL == tmp) return NULL;
wdenk8bde7f72003-06-27 21:31:46 +0000365
wdenk60fbe252003-04-08 23:25:21 +0000366 for (i=0; i<6; i++) {
367 addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
368 if (tmp)
369 tmp = (*end) ? end+1 : end;
370 }
371
372 return addr;
373}