File: dev_net_cs8900a.c

package info (click to toggle)
skyeye 1.2.0rc8-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,688 kB
  • ctags: 11,403
  • sloc: ansic: 60,324; sh: 1,209; python: 707; makefile: 474; exp: 38
file content (600 lines) | stat: -rw-r--r-- 15,187 bytes parent folder | download
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//zzc: 2005-2-6 currently not support network simulation on Cygwin
#ifndef __CYGWIN__
/*
	dev_net_cs8900a.c - skyeye Cirrus Logic CS8900A ethernet controllor simulation
	Copyright (C) 2003 - 2005 Skyeye Develop Group
        for help please send mail to <skyeye-developer@lists.gro.clinux.org>
	
	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 
 
*/
/*
 * 06/04/2005   initial verion for cs8900a
 *                      walimis <wlm@student.dlut.edu.cn>
 */

#include <signal.h>
#include <sys/time.h>
//koodailar add for mingw 2005.12.18 ----------------------------------------
#ifdef __MINGW32__
#include "arch/arm/common/armdefs.h"
#include "device/skyeye_device.h"
#else
#include "armdefs.h"
#include "skyeye_device.h"
#endif
// end ----------------------------------------------------------------------

#include "dev_net_cs8900a.h"

static struct device_default_value cs8900a_net_def[] = {
	/* name         base        size   interrupt array */
	{"at91", 0xfffa0000, 0x20, {16, 0, 0, 0}},
	{"s3c2410x", 0x19000300, 0x20, {9, 0, 0, 0}},
	{NULL},
};

#define MAX_DEVICE_NUM 10
static struct device_desc *cs8900a_devs[MAX_DEVICE_NUM];


void
net_cs8900a_set_update_intr (struct device_desc *dev)
{
	struct device_interrupt *intr = &dev->intr;
	struct machine_config *mc = (struct machine_config *) dev->mach;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	mc->mach_set_intr (intr->interrupts[INT_CS8900A]);
	mc->mach_update_intr (mc);
}

inline void
set_time (int packets)
{
         
//koodailar remove it for mingw 2005.12.18--------------------------------------
//it may have timing problem. But i have not found it yet.
//if there is anything wrong please report bugs.         
#ifndef __MINGW32__
	struct itimerval value;
	value.it_value.tv_sec = 0;
	value.it_value.tv_usec = packets;
	value.it_interval = value.it_value;
	setitimer (ITIMER_REAL, &value, NULL);
#endif
//end --------------------------------------------------------------------------
}

static void
send_interrupt ()
{
	int i;
	struct device_desc *dev;

	for (i = 0; i < MAX_DEVICE_NUM; i++) {
		if ((dev = cs8900a_devs[i]) != NULL) {
			struct net_device *net_dev =
				(struct net_device *) dev->dev;
			struct machine_config *mc =
				(struct machine_config *) dev->mach;
			struct net_cs8900a_io *io =
				(struct net_cs8900a_io *) dev->data;
			if ((io->need_update)) {
				/* only update once. */
				net_cs8900a_set_update_intr (dev);
				io->need_update = 0;
				set_time (0);
				return;
			}

		}
	}
}
static void
init_sigaction (void)
{
//koodailar remove it for mingw 2005.12.18--------------------------------------
//may have problem . please see the above function:
//   static inline void set_time (int packets)

#ifndef __MINGW32__               
	struct sigaction act;
	act.sa_handler = send_interrupt;
	act.sa_flags = 0;
	sigemptyset (&act.sa_mask);
	sigaction (SIGALRM, &act, NULL);
#endif
//end --------------------------------------------------------------------------
}


/* ISQ read*/
static void
isq_read (struct device_desc *dev, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	io->ctrl_st[CtrlStNum (PP_ISQ)] = 0x0;
	if (io->ctrl_st[CtrlStNum (PP_RxEvent)] & 0xffc0) {
		io->ctrl_st[CtrlStNum (PP_ISQ)] =
			io->ctrl_st[CtrlStNum (PP_RxEvent)];
		io->ctrl_st[CtrlStNum (PP_RxEvent)] &= 0x3f;
	}
	else if (io->ctrl_st[CtrlStNum (PP_TxEvent)] & 0xffc0) {
		io->ctrl_st[CtrlStNum (PP_ISQ)] =
			io->ctrl_st[CtrlStNum (PP_TxEvent)];
		io->ctrl_st[CtrlStNum (PP_TxEvent)] &= 0x3f;
	}

	*data = io->ctrl_st[CtrlStNum (PP_ISQ)];
}

static void
frame_write (struct device_desc *dev, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	io->tx_frame[io->tx_tail] = data;
	io->tx_tail++;
	if ((io->tx_tail * 2) >= io->tx_length) {
		cs8900a_output (dev, (u8 *) io->tx_frame, (io->tx_tail * 2));
		io->tx_tail = 0;
	}
}

static void
frame_read (struct device_desc *dev, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	if (io->rx_tail > io->rx_head) {
		io->rx_head = io->rx_tail = 0;
		return;
	}
	*data = io->rx_frame[io->rx_tail];
	io->rx_tail++;
}


static void
eeprom_reset (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	u8 offset, checksum = 0, *buf;

	u16 eeprom_val[17] = { 0xa120,
		0x2020, 0x0300, 0x0003, 0x0001,
		0x502c, 0xe000, 0x000f, 0x0, 0xd, 0xc000, 0xf,
		0x2158, 0x0010, 0x0, 0x0,
		0x2800
	};

	/* set eeprom mac address */
	eeprom_val[13] = net_dev->macaddr[0] | (net_dev->macaddr[1] << 8);
	eeprom_val[14] = net_dev->macaddr[2] | (net_dev->macaddr[3] << 8);
	eeprom_val[15] = net_dev->macaddr[4] | (net_dev->macaddr[5] << 8);

	/* re-compute checksum. */
	buf = (u8 *) eeprom_val;
	for (offset = 0; offset < (sizeof (eeprom_val) - 2); offset++)
		checksum += buf[offset];
	eeprom_val[16] = ((u8) (0x100 - checksum)) << 8;

	/* fill eeprom. */
	memcpy (io->eeprom, eeprom_val, sizeof (eeprom_val));

	/* others */
	io->eeprom_writable = 0;

	io->ctrl_st[CtrlStNum (PP_SelfST)] |= EEPROMpresent | EEPROMOK;

	memcpy (io->ieee_addr, net_dev->macaddr, 6);

}

static void
ctrl_status_write (struct device_desc *dev, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	io->ctrl_st[CtrlStNum (io->pp_address)] = data;
	//printf("%s: addr %x, data %x\n", __FUNCTION__, io->pp_address, data);
}

static void
ctrl_status_read (struct device_desc *dev, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	*data = io->ctrl_st[CtrlStNum (io->pp_address)];
	//printf("%s: addr %x, data %x\n", __FUNCTION__, io->pp_address, *data);
}

/* FIXME: now it doesn't support erase-all/write-all commands
 */
static void
eeprom_write (struct device_desc *dev, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	if ((io->eeprom_cmd & EEWriteRegister) && io->eeprom_writable == 1) {
		io->eeprom[io->eeprom_cmd & 0xff] = io->eeprom_data;
	}
	else if ((io->eeprom_cmd & EEEraseRegister)
		 && io->eeprom_writable == 1) {
		io->eeprom[io->eeprom_cmd & 0xff] = 0xffff;
	}
	else {
		if (io->eeprom_cmd & EEWriteEnable) {
			io->eeprom_writable = 1;
		}
		else if (io->eeprom_cmd & EEWriteDis) {
			io->eeprom_writable = 0;
		}
	}

}
static void
eeprom_read (struct device_desc *dev, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	if (!(io->eeprom_cmd & EEReadRegister))
		return;

	*data = io->eeprom[io->eeprom_cmd & 0xff];

}

static void
net_cs8900a_fini (struct device_desc *dev)
{
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	free (dev->dev);
	free (io);
}

static void
net_cs8900a_reset (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	int i;

	/* set ProductID: rev_d */
	io->product_id[0] = EISA_REG_CODE;
	io->product_id[1] = CS8900A | (REV_D << 8);

	/* see section 4.10.5 */
	io->pp_address |= 0x3000;


	/* set control and status registers low 6 bits. see P49. */
	for (i = 0; i < 16; i++) {
		io->ctrl_st[i] |= i * 2 + 1;
		io->ctrl_st[i + 16] |= i * 2;

	}
	io->ctrl_st[CtrlStNum (PP_BusST)] |= Rdy4TxNOW;

	eeprom_reset (dev);

	/* init tx/rx buffer */
	if (!io->rx_frame)
		io->rx_frame = (u16 *) malloc (Rx_Frame_Count);
	memset (io->rx_frame, 0, Rx_Frame_Count);
	io->rx_status_p = &(io->rx_frame[0]);
	io->rx_length_p = &(io->rx_frame[1]);
	io->rx_head = io->rx_tail = 0;

	if (!io->tx_frame)
		io->tx_frame = (u16 *) malloc (Tx_Frame_Count);
	memset (io->tx_frame, 0, Tx_Frame_Count);
	io->tx_head = io->tx_tail = 0;
}

static void
net_cs8900a_update (struct device_desc *dev)
{
	struct device_interrupt *intr = &dev->intr;
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	struct machine_config *mc = (struct machine_config *) dev->mach;
	if ((!mc->mach_pending_intr (intr->interrupts[INT_CS8900A]))) {
//koodailar modify it for mingw 2005.12.18--------------------------------------
#ifdef __MINGW32__
		//whenever possible, update it
		cs8900a_input (dev);
#else                                     
		fd_set frds;
		struct timeval tv;
		int ret;
		FD_ZERO (&frds);
		FD_SET (net_dev->net_fd, &frds);
		tv.tv_sec = 0;
		tv.tv_usec = 0;
		if ((ret =
		     select (net_dev->net_fd + 1, &frds, NULL, NULL,
			     &tv)) > 0) {
			if (FD_ISSET (net_dev->net_fd, &frds)) {
				cs8900a_input (dev);
			}
		}
#endif
//end koodailar ----------------------------------------------------------------				
	}

}


int
net_cs8900a_read_halfword (struct device_desc *dev, u32 addr, u16 * data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	int offset = (u16) (addr - dev->base);
	int ret = ADDR_HIT;

	*data = 0;
	switch (offset) {
	case Rx_Frame_Port:
		frame_read (dev, data);
		break;
	case IO_ISQ:
		isq_read (dev, data);
		break;
	case PP_Address:
		*data = io->pp_address | 0x3000;
		break;
	case PP_Data:
		if (io->pp_address >= 0x100 && io->pp_address < 0x140) {

			ctrl_status_read (dev, data);
		}
		switch (io->pp_address) {
		case PP_ProductID:
			*data = io->product_id[0];
			break;
		case PP_ProductID + 2:
			*data = io->product_id[1];
			break;
		case PP_IntNum:
			*data = io->int_num;
			break;
		case PP_EEPROMCommand:
			*data = io->eeprom_cmd;
			break;
		case PP_EEPROMData:
			eeprom_read (dev, data);
			break;

		case PP_IA:
		case PP_IA + 2:
		case PP_IA + 4:
			*data = io->ieee_addr[io->pp_address - PP_IA] |
				(io->
				 ieee_addr[io->pp_address - PP_IA + 1] << 8);
			break;
		case PP_ISQ:
			isq_read (dev, data);
			break;

		case PP_RxStatus:
			*data = *(io->rx_status_p);
			*(io->rx_status_p) = 0;
			io->rx_tail++;
			break;
		case PP_RxLength:
			*data = *(io->rx_length_p);
			*(io->rx_length_p) = 0;
			io->rx_tail++;
			break;
		}
		//printf("addr:%x, data:%x\n", io->pp_address, *data);
		break;
	default:
		break;
	}
	return ret;

}

int
net_cs8900a_write_halfword (struct device_desc *dev, u32 addr, u16 data)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;

	int offset = (u16) (addr - dev->base);
	int ret = ADDR_HIT;

	switch (offset) {
	case Tx_Frame_Port:
		frame_write (dev, data);
		break;
	case IO_TxCMD:
		io->tx_cmd = data;
		break;
	case IO_TxLength:
		io->tx_length = data;
		break;
	case PP_Address:
		io->pp_address = data;
		break;
	case PP_Data:
		if (io->pp_address >= 0x100 && io->pp_address < 0x140) {

			ctrl_status_write (dev, data);
		}
		switch (io->pp_address) {
		case PP_IntNum:
			io->int_num = data;
			break;
		case PP_EEPROMCommand:
			io->eeprom_cmd = data;
			eeprom_write (dev, data);
			break;
		case PP_EEPROMData:
			if (io->eeprom_writable == 1)
				io->eeprom_data = data;
			break;
		case PP_IA:
		case PP_IA + 2:
		case PP_IA + 4:
			io->ieee_addr[io->pp_address - PP_IA] = data & 0xff;
			io->ieee_addr[io->pp_address - PP_IA + 1] =
				(data >> 8) & 0xff;
			break;
		case PP_TxCMD:
			io->tx_cmd = data;
			break;
		case PP_TxLength:
			io->tx_length = data;
			break;
		}
		break;
	default:
		break;
	}

	return ret;
}

static void
cs8900a_input (struct device_desc *dev)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	int packet_len, cs8900a_len;
	u8 *bufptr;

	bufptr = (u8 *) & (io->rx_frame[2]);
	packet_len = net_dev->net_read (net_dev, bufptr, Rx_Max_Count);
	if (packet_len < 0)
		return;

	*(io->rx_status_p) |= RxOK;
	*(io->rx_length_p) = packet_len;

	io->rx_head = 2;

	io->rx_head = io->rx_head + ((packet_len + 1) / 2) - 1;

	io->rx_tail = 0;

	//printf("io->rx_head:%d, io->rx_tail:%d, packet_len:%d\n", io->rx_head, io->rx_tail, packet_len);

#if 0
	print_packet (bufptr, packet_len);
#endif

	io->ctrl_st[CtrlStNum (PP_RxEvent)] |= 0x100;	//TxOK
	if (io->ctrl_st[CtrlStNum (PP_BusCTL)] & EnableRQ) {
		//printf("%s:%x, packet_len:%d\n", __FUNCTION__, io->ctrl_st[CtrlStNum(PP_RxEvent)], packet_len);
		set_time (packet_len);
		io->need_update = 1;
		net_cs8900a_set_update_intr (dev);
	}
}

static u8
cs8900a_output (struct device_desc *dev, u8 * buf, u16 packet_len)
{
	struct net_device *net_dev = (struct net_device *) dev->dev;
	struct net_cs8900a_io *io = (struct net_cs8900a_io *) dev->data;
	int len;

	//printf("%s: packet_len:%d\n", __FUNCTION__, packet_len);
#if 0
	print_packet (buf, packet_len);
#endif
	if ((len = net_dev->net_write (net_dev, buf, packet_len)) == -1) {
		fprintf (stderr, "write to tapif error in skyeye-ne2k.c\n");
		return -1;
	}
	io->ctrl_st[CtrlStNum (PP_TxEvent)] |= 0x100;
	io->ctrl_st[CtrlStNum (PP_BusST)] |= Rdy4TxNOW;

	if (io->ctrl_st[CtrlStNum (PP_BusCTL)] & EnableRQ) {
		set_time (packet_len);
		io->need_update = 1;
		net_cs8900a_set_update_intr (dev);
	}
	return 0;
}
static int
net_cs8900a_setup (struct device_desc *dev)
{
	int i;
	int enough = 0;
	struct net_cs8900a_io *io;
	struct device_interrupt *intr = &dev->intr;

	dev->fini = net_cs8900a_fini;
	dev->reset = net_cs8900a_reset;
	dev->update = net_cs8900a_update;
	dev->read_halfword = net_cs8900a_read_halfword;
	dev->write_halfword = net_cs8900a_write_halfword;

	io = (struct net_cs8900a_io *)
		malloc (sizeof (struct net_cs8900a_io));
	memset (io, 0, sizeof (struct net_cs8900a_io));
	if (io == NULL)
		return 1;
	dev->data = (void *) io;

	net_cs8900a_reset (dev);

	init_sigaction ();

	/* see if we need to set default values.
	 * */
	set_device_default (dev, cs8900a_net_def);

	for (i = 0; i < MAX_DEVICE_NUM; i++) {
		if (cs8900a_devs[i] == NULL) {
			cs8900a_devs[i] = dev;
			enough = 1;
			break;
		}
	}
	if (enough == 0)
		return 1;

	return 0;
}

void
net_cs8900a_init (struct device_module_set *mod_set)
{
	int i;
	register_device_module ("cs8900a", mod_set, &net_cs8900a_setup);

	for (i = 0; i < MAX_DEVICE_NUM; i++)
		cs8900a_devs[i] = NULL;

}

//zzc:#endif __CYGWIN__
#endif