blob: dad842c728dc03075e03fbe2ddd7231ced4b108f [file] [log] [blame]
wdenka6db71d2003-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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
wdenka6db71d2003-04-08 23:25:21 +000027#include <malloc.h>
28#include <net.h>
29#include <asm/addrspace.h>
30
31
32#define NUM_RX_DESC PKTBUFSRX
33#define TOUT_LOOP 1000000
34
35#define LONG_REF(addr) (*((volatile unsigned long*)addr))
36
37#define CMAC_CRX_CTRL LONG_REF(0xb800c870)
38#define CMAC_CTX_CTRL LONG_REF(0xb800c874)
39#define SYS_MAC_ADDR_0 LONG_REF(0xb800c878)
40#define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c)
41#define MIPS_H_MASK LONG_REF(0xB800C810)
42
43#define MA_LEARN LONG_REF(0xb8008004)
44#define DA_LOOKUP LONG_REF(0xb8008008)
45
46#define CMAC_CRX_CTRL_PD 0x00000001
47#define CMAC_CRX_CTRL_CG 0x00000002
48#define CMAC_CRX_CTRL_PL_SHIFT 2
49#define CMAC_CRIT 0x0
50#define CMAC_NON_CRIT 0x1
51#define MBOX_STAT_ID_SHF 28
52#define MBOX_STAT_CP 0x80000000
53#define MBOX_STAT_MB 0x00000001
54#define EN_MA_LEARN 0x02000000
55#define EN_DA_LKUP 0x01000000
wdenk57b2d802003-06-27 21:31:46 +000056#define MA_DEST_SHF 11
wdenka6db71d2003-04-08 23:25:21 +000057#define DA_DEST_SHF 11
58#define DA_STATE_SHF 19
59#define TSTAMP_MS 0x00000000
60#define SW_H_MBOX4_MASK 0x08000000
61#define SW_H_MBOX3_MASK 0x04000000
62#define SW_H_MBOX2_MASK 0x02000000
63#define SW_H_MBOX1_MASK 0x01000000
64
65typedef volatile struct {
66 unsigned int stat;
67 unsigned int cmd;
68 unsigned int cnt;
69 unsigned int adr;
70} mailbox_t;
71
72#define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
73
74typedef volatile struct {
75 unsigned int word0;
76 unsigned int word1;
77 unsigned int word2;
78} mbhdr_t;
79
80#define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
81
82
83static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
84static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
85 int length);
86static int plb2800_eth_recv(struct eth_device *dev);
87static void plb2800_eth_halt(struct eth_device *dev);
88
89static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
90static unsigned char * plb2800_get_mac_addr(void);
91
92static int rx_new;
93static int mac_addr_set = 0;
94
95
96int plb2800_eth_initialize(bd_t * bis)
97{
98 struct eth_device *dev;
99 ulong temp;
100
101#ifdef DEBUG
102 printf("Entered plb2800_eth_initialize()\n");
103#endif
104
105 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
106 {
107 printf("Failed to allocate memory\n");
108 return 0;
109 }
110 memset(dev, 0, sizeof(*dev));
111
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;
117
118 eth_register(dev);
119
wdenk57b2d802003-06-27 21:31:46 +0000120 /* bug fix */
121 *(ulong *)0xb800e800 = 0x838;
wdenka6db71d2003-04-08 23:25:21 +0000122
123 /* Set MBOX ownership */
124 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
125 MBOX_REG(0)->stat = temp;
126 MBOX_REG(1)->stat = temp;
127
wdenk57b2d802003-06-27 21:31:46 +0000128 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
wdenka6db71d2003-04-08 23:25:21 +0000129 MBOX_REG(2)->stat = temp;
130 MBOX_REG(3)->stat = temp;
wdenk57b2d802003-06-27 21:31:46 +0000131
wdenka6db71d2003-04-08 23:25:21 +0000132 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
133
134 /* Disable all Mbox interrupt */
135 temp = MIPS_H_MASK;
wdenk57b2d802003-06-27 21:31:46 +0000136 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
wdenka6db71d2003-04-08 23:25:21 +0000137 MIPS_H_MASK = temp;
138
139#ifdef DEBUG
140 printf("Leaving plb2800_eth_initialize()\n");
141#endif
142
143 return 1;
144}
145
146static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
147{
148#ifdef DEBUG
149 printf("Entering plb2800_eth_init()\n");
150#endif
151
152 plb2800_set_mac_addr(dev, dev->enetaddr);
153
154 rx_new = 0;
155
156#ifdef DEBUG
157 printf("Leaving plb2800_eth_init()\n");
158#endif
159
160 return 0;
161}
162
163
164static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
165 int length)
166{
167 int i;
168 int res = -1;
169 u32 temp;
170 mailbox_t * mb = MBOX_REG(0);
171 char * mem = MBOX_MEM(0);
172
173#ifdef DEBUG
174 printf("Entered plb2800_eth_send()\n");
175#endif
176
177 if (length <= 0)
178 {
179 printf ("%s: bad packet size: %d\n", dev->name, length);
180 goto Done;
181 }
182
183 if (length < 64)
184 {
185 length = 64;
186 }
187
188 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
189
190#ifdef DEBUG
191 printf("0 mb->stat = 0x%x\n", mb->stat);
192#endif
193
194 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
195 {
196 if (i >= TOUT_LOOP)
197 {
198 printf("%s: tx buffer not ready\n", dev->name);
199 printf("1 mb->stat = 0x%x\n", mb->stat);
200 goto Done;
201 }
202 }
203
204 /* For some strange reason, memcpy doesn't work, here!
205 */
206 do
207 {
208 int words = (length >> 2) + 1;
209 unsigned int* dst = (unsigned int*)(mem);
210 unsigned int* src = (unsigned int*)(packet);
211 for (i = 0; i < words; i++)
212 {
213 *dst = *src;
214 dst++;
215 src++;
216 };
217 } while(0);
218
219 CMAC_CRX_CTRL = temp;
220 mb->cmd = MBOX_STAT_CP;
wdenk57b2d802003-06-27 21:31:46 +0000221
wdenka6db71d2003-04-08 23:25:21 +0000222#ifdef DEBUG
223 printf("2 mb->stat = 0x%x\n", mb->stat);
224#endif
225
226 res = length;
227Done:
228
229#ifdef DEBUG
230 printf("Leaving plb2800_eth_send()\n");
231#endif
232
233 return res;
234}
235
236
237static int plb2800_eth_recv(struct eth_device *dev)
238{
239 int length = 0;
240 mailbox_t * mbox = MBOX_REG(3);
241 unsigned char * hdr = MBOX_MEM(3);
242 unsigned int stat;
243
244#ifdef DEBUG
245 printf("Entered plb2800_eth_recv()\n");
246#endif
247
248 for (;;)
249 {
250 stat = mbox->stat;
251
252 if (!(stat & MBOX_STAT_CP))
253 {
254 break;
255 }
wdenk57b2d802003-06-27 21:31:46 +0000256
wdenka6db71d2003-04-08 23:25:21 +0000257 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
258 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
259
260 stat &= ~MBOX_STAT_CP;
261 mbox->stat = stat;
262#ifdef DEBUG
263 {
264 int i;
265 for (i=0;i<length - 4;i++)
266 {
267 if (i % 16 == 0) printf("\n%04x: ", i);
268 printf("%02X ", NetRxPackets[rx_new][i]);
269 }
270 printf("\n");
271 }
272#endif
273
274 if (length)
275 {
276#ifdef DEBUG
277 printf("Received %d bytes\n", length);
278#endif
279 NetReceive((void*)(NetRxPackets[rx_new]),
280 length - 4);
281 }
282 else
283 {
284#if 1
285 printf("Zero length!!!\n");
286#endif
287 }
288
289 rx_new = (rx_new + 1) % NUM_RX_DESC;
290 }
291
292#ifdef DEBUG
293 printf("Leaving plb2800_eth_recv()\n");
294#endif
295
296 return length;
297}
298
299
300static void plb2800_eth_halt(struct eth_device *dev)
301{
302#ifdef DEBUG
303 printf("Entered plb2800_eth_halt()\n");
304#endif
305
306#ifdef DEBUG
307 printf("Leaving plb2800_eth_halt()\n");
308#endif
309}
310
311static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
312{
313 char packet[60];
314 ulong temp;
315 int ix;
316
317 if (mac_addr_set ||
318 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
319 {
320 return;
321 }
wdenk57b2d802003-06-27 21:31:46 +0000322
wdenka6db71d2003-04-08 23:25:21 +0000323 /* send one packet through CPU port
324 * in order to learn system MAC address
wdenk57b2d802003-06-27 21:31:46 +0000325 */
wdenka6db71d2003-04-08 23:25:21 +0000326
327 /* Set DA_LOOKUP register */
wdenk57b2d802003-06-27 21:31:46 +0000328 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
wdenka6db71d2003-04-08 23:25:21 +0000329 DA_LOOKUP = temp;
330
331 /* Set MA_LEARN register */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200332 temp = 50 << MA_DEST_SHF; /* static entry */
wdenka6db71d2003-04-08 23:25:21 +0000333 MA_LEARN = temp;
334
335 /* set destination address */
336 for (ix=0;ix<6;ix++)
337 packet[ix] = 0xff;
wdenk57b2d802003-06-27 21:31:46 +0000338
wdenka6db71d2003-04-08 23:25:21 +0000339 /* set source address = system MAC address */
340 for (ix=0;ix<6;ix++)
341 packet[6+ix] = addr[ix];
342
343 /* set type field */
344 packet[12]=0xaa;
345 packet[13]=0x55;
346
347 /* set data field */
348 for(ix=14;ix<60;ix++)
349 packet[ix] = 0x00;
wdenk57b2d802003-06-27 21:31:46 +0000350
wdenka6db71d2003-04-08 23:25:21 +0000351#ifdef DEBUG
352 for (ix=0;ix<6;ix++)
353 printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
354#endif
355
356 /* set one packet */
357 plb2800_eth_send(dev, packet, sizeof(packet));
358
359 /* delay for a while */
360 for(ix=0;ix<65535;ix++)
361 temp = ~temp;
362
wdenk57b2d802003-06-27 21:31:46 +0000363 /* Set CMAC_CTX_CTRL register */
wdenka6db71d2003-04-08 23:25:21 +0000364 temp = TSTAMP_MS; /* no autocast */
365 CMAC_CTX_CTRL = temp;
wdenk57b2d802003-06-27 21:31:46 +0000366
wdenka6db71d2003-04-08 23:25:21 +0000367 /* Set DA_LOOKUP register */
368 temp = EN_DA_LKUP;
369 DA_LOOKUP = temp;
370
371 mac_addr_set = 1;
372}
373
374static unsigned char * plb2800_get_mac_addr(void)
375{
376 static unsigned char addr[6];
377 char *tmp, *end;
378 int i;
wdenk57b2d802003-06-27 21:31:46 +0000379
wdenka6db71d2003-04-08 23:25:21 +0000380 tmp = getenv ("ethaddr");
381 if (NULL == tmp) return NULL;
wdenk57b2d802003-06-27 21:31:46 +0000382
wdenka6db71d2003-04-08 23:25:21 +0000383 for (i=0; i<6; i++) {
384 addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
385 if (tmp)
386 tmp = (*end) ? end+1 : end;
387 }
388
389 return addr;
390}