summaryrefslogtreecommitdiff
path: root/drivers/net/plb2800_eth.c
blob: 308bf8fe313738344b4a1df84d04a01e8aa285cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * PLB2800 internal switch ethernet driver.
 *
 * (C) Copyright 2003
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <malloc.h>
#include <net.h>
#include <netdev.h>
#include <asm/addrspace.h>


#define NUM_RX_DESC	PKTBUFSRX
#define TOUT_LOOP	1000000

#define LONG_REF(addr) (*((volatile unsigned long*)addr))

#define CMAC_CRX_CTRL	LONG_REF(0xb800c870)
#define CMAC_CTX_CTRL	LONG_REF(0xb800c874)
#define SYS_MAC_ADDR_0	LONG_REF(0xb800c878)
#define SYS_MAC_ADDR_1	LONG_REF(0xb800c87c)
#define MIPS_H_MASK	LONG_REF(0xB800C810)

#define MA_LEARN	LONG_REF(0xb8008004)
#define DA_LOOKUP	LONG_REF(0xb8008008)

#define CMAC_CRX_CTRL_PD	0x00000001
#define CMAC_CRX_CTRL_CG	0x00000002
#define CMAC_CRX_CTRL_PL_SHIFT	2
#define CMAC_CRIT		0x0
#define CMAC_NON_CRIT		0x1
#define MBOX_STAT_ID_SHF	28
#define MBOX_STAT_CP		0x80000000
#define MBOX_STAT_MB		0x00000001
#define EN_MA_LEARN		0x02000000
#define EN_DA_LKUP		0x01000000
#define MA_DEST_SHF		11
#define DA_DEST_SHF		11
#define DA_STATE_SHF		19
#define TSTAMP_MS		0x00000000
#define SW_H_MBOX4_MASK		0x08000000
#define SW_H_MBOX3_MASK		0x04000000
#define SW_H_MBOX2_MASK		0x02000000
#define SW_H_MBOX1_MASK		0x01000000

typedef volatile struct {
  unsigned int stat;
  unsigned int cmd;
  unsigned int cnt;
  unsigned int adr;
} mailbox_t;

#define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))

typedef volatile struct {
  unsigned int word0;
  unsigned int word1;
  unsigned int word2;
} mbhdr_t;

#define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))


static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
static int plb2800_eth_send(struct eth_device *dev, void *packet, int length);
static int plb2800_eth_recv(struct eth_device *dev);
static void plb2800_eth_halt(struct eth_device *dev);

static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
static unsigned char * plb2800_get_mac_addr(void);

static int rx_new;
static int mac_addr_set = 0;


int plb2800_eth_initialize(bd_t * bis)
{
	struct eth_device *dev;
	ulong temp;

#ifdef DEBUG
	printf("Entered plb2800_eth_initialize()\n");
#endif

	if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
	{
		printf("Failed to allocate memory\n");
		return -1;
	}
	memset(dev, 0, sizeof(*dev));

	sprintf(dev->name, "PLB2800 Switch");
	dev->init = plb2800_eth_init;
	dev->halt = plb2800_eth_halt;
	dev->send = plb2800_eth_send;
	dev->recv = plb2800_eth_recv;

	eth_register(dev);

	/* bug fix */
	*(ulong *)0xb800e800 = 0x838;

	/* Set MBOX ownership */
	temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
	MBOX_REG(0)->stat = temp;
	MBOX_REG(1)->stat = temp;

	temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
	MBOX_REG(2)->stat = temp;
	MBOX_REG(3)->stat = temp;

	plb2800_set_mac_addr(dev, plb2800_get_mac_addr());

	/* Disable all Mbox interrupt */
	temp = MIPS_H_MASK;
	temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
	MIPS_H_MASK = temp;

#ifdef DEBUG
	printf("Leaving plb2800_eth_initialize()\n");
#endif

	return 0;
}

static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
{
#ifdef DEBUG
	printf("Entering plb2800_eth_init()\n");
#endif

	plb2800_set_mac_addr(dev, dev->enetaddr);

	rx_new = 0;

#ifdef DEBUG
	printf("Leaving plb2800_eth_init()\n");
#endif

	return 0;
}


static int plb2800_eth_send(struct eth_device *dev, void *packet, int length)
{
	int                    i;
	int                    res         = -1;
	u32                    temp;
	mailbox_t *            mb          = MBOX_REG(0);
	char      *            mem         = MBOX_MEM(0);

#ifdef DEBUG
	printf("Entered plb2800_eth_send()\n");
#endif

	if (length <= 0)
	{
		printf ("%s: bad packet size: %d\n", dev->name, length);
		goto Done;
	}

	if (length < 64)
	{
		length = 64;
	}

	temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);

#ifdef DEBUG
	printf("0 mb->stat = 0x%x\n",  mb->stat);
#endif

	for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
	{
		if (i >= TOUT_LOOP)
		{
			printf("%s: tx buffer not ready\n", dev->name);
			printf("1 mb->stat = 0x%x\n",  mb->stat);
			goto Done;
		}
	}

		/* For some strange reason, memcpy doesn't work, here!
		 */
	do
	{
		int words = (length >> 2) + 1;
		unsigned int* dst = (unsigned int*)(mem);
		unsigned int* src = (unsigned int*)(packet);
		for (i = 0; i < words; i++)
		{
			*dst = *src;
			dst++;
			src++;
		};
	} while(0);

	CMAC_CRX_CTRL = temp;
	mb->cmd = MBOX_STAT_CP;

#ifdef DEBUG
	printf("2 mb->stat = 0x%x\n",  mb->stat);
#endif

	res = length;
Done:

#ifdef DEBUG
	printf("Leaving plb2800_eth_send()\n");
#endif

	return res;
}


static int plb2800_eth_recv(struct eth_device *dev)
{
	int                    length  = 0;
	mailbox_t            * mbox    = MBOX_REG(3);
	unsigned char        * hdr     = MBOX_MEM(3);
	unsigned int           stat;

#ifdef DEBUG
	printf("Entered plb2800_eth_recv()\n");
#endif

	for (;;)
	{
		stat = mbox->stat;

		if (!(stat & MBOX_STAT_CP))
		{
			break;
		}

		length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
		memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);

		stat &= ~MBOX_STAT_CP;
		mbox->stat = stat;
#ifdef DEBUG
		{
			int i;
			for (i=0;i<length - 4;i++)
			{
				if (i % 16 == 0) printf("\n%04x: ", i);
				printf("%02X ", NetRxPackets[rx_new][i]);
			}
			printf("\n");
		}
#endif

		if (length)
		{
#ifdef DEBUG
			printf("Received %d bytes\n", length);
#endif
			NetReceive((void*)(NetRxPackets[rx_new]),
			            length - 4);
		}
		else
		{
#if 1
			printf("Zero length!!!\n");
#endif
		}

		rx_new = (rx_new + 1) % NUM_RX_DESC;
	}

#ifdef DEBUG
	printf("Leaving plb2800_eth_recv()\n");
#endif

	return length;
}


static void plb2800_eth_halt(struct eth_device *dev)
{
#ifdef DEBUG
	printf("Entered plb2800_eth_halt()\n");
#endif

#ifdef DEBUG
	printf("Leaving plb2800_eth_halt()\n");
#endif
}

static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
{
	char packet[60];
	ulong temp;
	int ix;

	if (mac_addr_set ||
	    NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
	{
		return;
	}

	/* send one packet through CPU port
	 * in order to learn system MAC address
	 */

	/* Set DA_LOOKUP register */
	temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
	DA_LOOKUP = temp;

	/* Set MA_LEARN register */
	temp = 50 << MA_DEST_SHF;	/* static entry */
	MA_LEARN = temp;

	/* set destination address */
	for (ix=0;ix<6;ix++)
		packet[ix] = 0xff;

	/* set source address = system MAC address */
	for (ix=0;ix<6;ix++)
		packet[6+ix] = addr[ix];

	/* set type field */
	packet[12]=0xaa;
	packet[13]=0x55;

	/* set data field */
	for(ix=14;ix<60;ix++)
		packet[ix] = 0x00;

#ifdef DEBUG
	for (ix=0;ix<6;ix++)
		printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
#endif

	/* set one packet */
	plb2800_eth_send(dev, packet, sizeof(packet));

	/* delay for a while */
	for(ix=0;ix<65535;ix++)
		temp = ~temp;

	/* Set CMAC_CTX_CTRL register */
	temp = TSTAMP_MS;	/* no autocast */
	CMAC_CTX_CTRL = temp;

	/* Set DA_LOOKUP register */
	temp = EN_DA_LKUP;
	DA_LOOKUP = temp;

	mac_addr_set = 1;
}

static unsigned char * plb2800_get_mac_addr(void)
{
	static unsigned char addr[6];
	char *tmp, *end;
	int i;

	tmp = getenv ("ethaddr");
	if (NULL == tmp) return NULL;

	for (i=0; i<6; i++) {
		addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
		if (tmp)
			tmp = (*end) ? end+1 : end;
	}

	return addr;
}