summaryrefslogtreecommitdiff
path: root/cpu/arm926ejs/at91sam926x/ether.c
blob: fc58c4af4c325aec94f2789cd1e0136025061a1a (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * (C) Copyright 2006
 * Author : Lacressonniere Nicolas (Atmel)
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#ifndef CONFIG_DRIVER_DM9000	/* SAM9261EK uses DM9000 Phy */

#ifdef CONFIG_DRIVER_ETHER
#ifdef CONFIG_CMD_NET

#include <net.h>
#include <at91_net.h>
#include <miiphy.h>
#include <dm9161.h>

/* ----- Ethernet Buffer definitions ----- */
 
/* Receive Transfer descriptor structure */
typedef struct  _AT91S_RxTdDescriptor {
	unsigned int addr;
	union
	{
		unsigned int status;
		struct {
			unsigned int Length:12;
			unsigned int Rxbuf_off:2;
			unsigned int StartOfFrame:1;
			unsigned int EndOfFrame:1;
			unsigned int Cfi:1;
			unsigned int VlanPriority:3;
			unsigned int PriorityTag:1;
			unsigned int VlanTag:1;
			unsigned int TypeID:1;
			unsigned int Sa4Match:1;
			unsigned int Sa3Match:1;
			unsigned int Sa2Match:1;
			unsigned int Sa1Match:1;
			unsigned int Res0:1;
			unsigned int ExternalAdd:1;
			unsigned int UniCast:1;
			unsigned int MultiCast:1;
			unsigned int BroadCast:1;
		} S_Status;		
	} U_Status;
} AT91S_RxTdDescriptor, *AT91PS_RxTdDescriptor;


/* Transmit Transfer descriptor structure */
typedef struct _AT91S_TxTdDescriptor {
	unsigned int addr;
	union
	{
		unsigned int status;
		struct {
			unsigned int Length:11;
			unsigned int Res0:4;
			unsigned int LastBuff:1;
			unsigned int NoCrc:1;
			unsigned int Res1:10;
			unsigned int BufExhausted:1;
			unsigned int TransmitUnderrun:1;
			unsigned int TransmitError:1;
			unsigned int Wrap:1;
			unsigned int BuffUsed:1;
		} S_Status;		
	} U_Status;
} AT91S_TxTdDescriptor, *AT91PS_TxTdDescriptor;


#define RBF_ADDR      0xfffffffc
#define RBF_OWNER     (1<<0)
#define RBF_WRAP      (1<<1)
#define RBF_BROADCAST (1<<31)
#define RBF_MULTICAST (1<<30)
#define RBF_UNICAST   (1<<29)
#define RBF_EXTERNAL  (1<<28)
#define RBF_UNKOWN    (1<<27)
#define RBF_SIZE      0x07ff
#define RBF_LOCAL4    (1<<26)
#define RBF_LOCAL3    (1<<25)
#define RBF_LOCAL2    (1<<24)
#define RBF_LOCAL1    (1<<23)
#define RBF_EOF       (1<<15)
#define RBF_SOF       (1<<14)

#define MAX_ETH_FRAME_LEN  0x600        /* 1536 bytes. Max ethernet frame size */

#define RBF_FRAMEMAX 64
#define RBF_FRAMELEN 128

#define TBF_FRAMEMAX 64
#define TBF_FRAMELEN 0x600

/* alignment as per Errata #11 (64 bytes) is insufficient! */
AT91S_RxTdDescriptor RxtdList[RBF_FRAMEMAX] __attribute((aligned(512)));
AT91S_TxTdDescriptor TxtdList[TBF_FRAMEMAX] __attribute((aligned(512)));

unsigned char rbf_framebuf[RBF_FRAMEMAX][RBF_FRAMELEN] __attribute((aligned(4)));
unsigned char tbf_framebuf[TBF_FRAMEMAX][TBF_FRAMELEN] __attribute((aligned(4)));

unsigned int RxBuffIndex = 0;
unsigned int TxBuffIndex = 0;

static unsigned char stage_rx_buffer[MAX_ETH_FRAME_LEN];
volatile unsigned char *rx_pkt_tmp = stage_rx_buffer;

/* structure to interface the PHY */
AT91S_PhyOps PhyOps;

AT91PS_EMAC p_mac;

extern int AT91F_EMAC_Hardware_Init(void);

/*********** EMAC Phy layer Management functions *************************/
/*
 * Name:
 *	at91_EmacEnableMDIO
 * Description:
 *	Enables the MDIO bit in MAC control register
 * Arguments:
 *	p_mac - pointer to struct AT91S_EMAC
 * Return value:
 *	none
 */
void at91_EmacEnableMDIO (AT91PS_EMAC p_mac)
{
	/* Mac CTRL reg set for MDIO enable */
	p_mac->EMAC_NCR |= AT91C_EMAC_MPE;	/* Management port enable */
}

/*
 * Name:
 *	at91_EmacDisableMDIO
 * Description:
 *	Disables the MDIO bit in MAC control register
 * Arguments:
 *	p_mac - pointer to struct AT91S_EMAC
 * Return value:
 *	none
 */
void at91_EmacDisableMDIO (AT91PS_EMAC p_mac)
{
	/* Mac CTRL reg set for MDIO disable */
	p_mac->EMAC_NCR &= ~AT91C_EMAC_MPE;	/* Management port disable */
}


/*
 * Name:
 *	at91_EmacReadPhy
 * Description:
 *	Reads data from the PHY register
 * Arguments:
 *	dev - pointer to struct net_device
 *	RegisterAddress - unsigned char
 * 	pInput - pointer to value read from register
 * Return value:
 *	TRUE - if data read successfully
 */
uchar at91_EmacReadPhy (AT91PS_EMAC p_mac,
				unsigned char PhyAddress,
				unsigned char RegisterAddress,
				unsigned short *pInput)
{
	p_mac->EMAC_MAN = (AT91C_EMAC_SOF & (0x01 << 30)) |
			  (AT91C_EMAC_RW & (0x2 << 28)) |
			  ((PhyAddress & 0x1F) << 23) |
			  (RegisterAddress << 18) |
			  (AT91C_EMAC_CODE & (0x2 << 16));

	/* Wait until IDLE bit in Network Status register is cleared */
	while (!(p_mac->EMAC_NSR & AT91C_EMAC_IDLE));

	*pInput = (unsigned short) (p_mac->EMAC_MAN & 0x0000FFFF);

	return TRUE;
}


/*
 * Name:
 *	at91_EmacWritePhy
 * Description:
 *	Writes data to the PHY register
 * Arguments:
 *	dev - pointer to struct net_device
 *	RegisterAddress - unsigned char
 * 	pOutput - pointer to value to be written in the register
 * Return value:
 *	TRUE - if data read successfully
 */
uchar at91_EmacWritePhy (AT91PS_EMAC p_mac,
				unsigned char PhyAddress,
				unsigned char RegisterAddress,
				unsigned short *pOutput)
{
	p_mac->EMAC_MAN = (AT91C_EMAC_SOF & (0x01 << 30)) |
			  (AT91C_EMAC_RW & (0x1 << 28)) |
			  ((PhyAddress & 0x1F) << 23) |
			  (RegisterAddress << 18) |
			  (AT91C_EMAC_CODE & (0x2 << 16)) |
			  *pOutput;
	
	/* Wait until IDLE bit in Network Status register is cleared */
	while (!(p_mac->EMAC_NSR & AT91C_EMAC_IDLE));
	
	return TRUE;
}

/*---------------------------------------------------------------------------- */
/* \fn    AT91F_EMACInit */
/* \brief This function initialise the ethernet */
/* \return Status ( Success = 0) */
/*---------------------------------------------------------------------------- */
int AT91F_EMACInit(bd_t * bd,
		   unsigned int pRxTdList,
		   unsigned int pTxTdList)
{
	unsigned int tick = 0;
	unsigned short status;
	  
	/* Wait for PHY auto negotiation completed */
	at91_EmacEnableMDIO(p_mac);
    
	do {
		at91_EmacReadPhy(p_mac, AT91C_PHY_ADDR, DM9161_BMSR, &status);
		at91_EmacReadPhy(p_mac, AT91C_PHY_ADDR, DM9161_BMSR, &status); 

		tick++;
	}
	while (!(status & DM9161_AUTONEG_COMP) && (tick < AT91C_ETH_TIMEOUT));

	at91_EmacDisableMDIO(p_mac);
	
	if (tick == AT91C_ETH_TIMEOUT)
	{
		printf ("-E- Autonegotiation Timeout\n\r");
		return 1;
	} else
		printf ("End of Autonegotiation\n\r");
    
	/* the sequence write EMAC_SA1L and write EMAC_SA1H must be respected */
	p_mac->EMAC_SA1L = (bd->bi_enetaddr[3] << 24) | (bd->bi_enetaddr[2] << 16)
			 | (bd->bi_enetaddr[1] <<  8) | (bd->bi_enetaddr[0]);
	p_mac->EMAC_SA1H = (bd->bi_enetaddr[5] <<  8) | (bd->bi_enetaddr[4]);

	p_mac->EMAC_RBQP = pRxTdList;
	p_mac->EMAC_TBQP = pTxTdList;


	p_mac->EMAC_NCFGR  |= (AT91C_EMAC_CAF | AT91C_EMAC_NBC );
	p_mac->EMAC_NCFGR  &= ~(AT91C_EMAC_CLK);
	
	p_mac->EMAC_NCR  |= (AT91C_EMAC_TE | AT91C_EMAC_RE);

	return 0;
}

int eth_init (bd_t * bd)
{
	volatile unsigned int uStatus;
	unsigned int val, i;
	int ret;
	static int first_time = 0;
	
	if (!first_time)
	{
		/* Init PIOs in peripheral mode */
		AT91F_EMAC_Hardware_Init();

		*AT91C_PMC_PCER = 1 << AT91C_ID_EMAC;	/* Peripheral Clock Enable Register */
	
		/* Disable Tx and Rx */
		p_mac->EMAC_NCR = 0;
		/* Clear statistics */
		p_mac->EMAC_NCR = AT91C_EMAC_CLRSTAT;
	
		uStatus = p_mac->EMAC_ISR;
		p_mac->EMAC_TSR = 0xFFFFFFFF;
		p_mac->EMAC_RSR = 0xFFFFFFFF;

		/* We don't use interrupts. disable them */
		p_mac->EMAC_IDR = 0xFFFFFFFF;
			
		/* Enable EMAC in MII mode and enable MII Clock (25MHz) */
		p_mac->EMAC_USRIO = AT91C_EMAC_CLKEN;

#ifdef CONFIG_AT91C_USE_RMII
		p_mac->EMAC_USRIO |= AT91C_EMAC_RMII;
#endif

		/* Set MDIO clock */
#if (AT91C_MASTER_CLOCK > 80000000)
		p_mac->EMAC_NCFGR |= AT91C_EMAC_CLK_HCLK_64; 
#else
		p_mac->EMAC_NCFGR |= AT91C_EMAC_CLK_HCLK_32; 
#endif
	
		/* Init Ethernet buffers */
		RxBuffIndex = 0;
		TxBuffIndex = 0;

		/* Initialise RxtdList descriptor */
		for (i = 0; i < RBF_FRAMEMAX; ++i) {
			val = (unsigned int)(&rbf_framebuf[i][0]);
			RxtdList[i].addr = val & 0xFFFFFFF8;
			RxtdList[i].U_Status.status = 0;
		}	
		/* Set the WRAP bit at the end of the list descriptor */
		RxtdList[RBF_FRAMEMAX-1].addr |= RBF_WRAP;

		/* Initialise TxtdList descriptor */
		for (i = 0; i < TBF_FRAMEMAX; ++i) {
			val = (unsigned int)(&tbf_framebuf[i][0]);
			TxtdList[i].addr = val & 0xFFFFFFF8;
			TxtdList[i].U_Status.status = 0;
			TxtdList[i].U_Status.S_Status.BuffUsed = 1;
		}
		TxtdList[0].U_Status.S_Status.BuffUsed = 0;
		/* Set the WRAP bit at the end of the list descriptor */
		TxtdList[TBF_FRAMEMAX-1].U_Status.S_Status.Wrap = 1;
	
		at91_GetPhyInterface (&PhyOps);

		/* Phy Software Reset */
		if (!PhyOps.Reset(p_mac))
		{
			printf ("PHY not reset!!\n\r");
			return 0;
		}

		if (!PhyOps.IsPhyConnected (p_mac))
		{
			printf ("PHY not connected!!\n\r");
			return 0;
		}

		/* MII management start from here */
		if (!(ret = PhyOps.Init (p_mac)))
		{
			printf ("MAC: error during RMII initialization\n");
			return 0;
		}

		if (AT91F_EMACInit(bd, (unsigned int)RxtdList, (unsigned int)TxtdList))
			return 0;
			
		first_time = 1;
	}
	
	/* Initialize Receive Frame Pointer */
	rx_pkt_tmp = stage_rx_buffer;
			
	return 0;
}

int eth_send (volatile void *packet, int length)
{

	if ((TxtdList[TxBuffIndex].U_Status.S_Status.BuffUsed == 0)) 
	{
		TxtdList[TxBuffIndex].addr = (unsigned int)packet;
		TxtdList[TxBuffIndex].U_Status.S_Status.Length = length;
		TxtdList[TxBuffIndex].U_Status.S_Status.LastBuff = 1;
	} 
	else
		return 2;

	p_mac->EMAC_NCR |= AT91C_EMAC_TSTART;
		
	while (!(p_mac->EMAC_TSR & AT91C_EMAC_COMP));
	
	p_mac->EMAC_TSR |= AT91C_EMAC_COMP;
		
	TxtdList[TxBuffIndex].U_Status.S_Status.Length = 0;
	TxtdList[TxBuffIndex].U_Status.S_Status.LastBuff = 0;
	
	if (TxBuffIndex == (TBF_FRAMEMAX - 1))
		TxBuffIndex = 0;
	else
		TxBuffIndex ++;

	TxtdList[TxBuffIndex].U_Status.S_Status.BuffUsed = 0;

	if (p_mac->EMAC_TSR & AT91C_EMAC_UBR)
		p_mac->EMAC_TSR |= AT91C_EMAC_UBR;

	return 0;
}

int eth_rx (void)
{
	int size = 0;
				
	while (!size)
	{
		while (!(RxtdList[RxBuffIndex].addr & RBF_OWNER));
	
		if (RxtdList[RxBuffIndex].U_Status.S_Status.StartOfFrame == 1)
			rx_pkt_tmp = stage_rx_buffer;
		
		memcpy((uchar *)rx_pkt_tmp, (uchar *)(RxtdList[RxBuffIndex].addr & RBF_ADDR), RBF_FRAMELEN);
		rx_pkt_tmp += RBF_FRAMELEN;
	      	
		if (RxtdList[RxBuffIndex].U_Status.S_Status.EndOfFrame == 1)
		{
			size = RxtdList[RxBuffIndex].U_Status.S_Status.Length;

			NetReceive ((uchar *)stage_rx_buffer, size);
			if (p_mac->EMAC_RSR & AT91C_EMAC_REC)
				p_mac->EMAC_RSR |= AT91C_EMAC_REC;
			
			rx_pkt_tmp = stage_rx_buffer;
		}

		RxtdList[RxBuffIndex].U_Status.status = 0;
		RxtdList[RxBuffIndex].addr &= ~RBF_OWNER;

		if (RxtdList[RxBuffIndex].addr & RBF_WRAP)
			RxBuffIndex = 0;
		else
			RxBuffIndex++;
	}
	
	return size;
}

void eth_halt (void)
{
};

#endif	/* CONFIG_CMD_NET */
#endif  /* CONFIG_DRIVER_DM9000 */
#endif	/* CONFIG_DRIVER_ETHER */