-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdp8390.c
More file actions
697 lines (525 loc) · 19.4 KB
/
dp8390.c
File metadata and controls
697 lines (525 loc) · 19.4 KB
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "dp8390.h"
#include "system.h"
#include "io.h"
#include "vga.h"
#include "pci-main.h"
#include "gdebug.h"
#include "system.h"
#include "memory.h"
#include "packet.h"
#include "ethernet.h"
#include "idt.h"
#include "task.h"
#define NS_DATA_PORT 0x10
#define NS_RESET 0x1f
#define COMMAND_REG 0x00
#define INTR_STATUS_REG 0x07
#define INTR_MASK_REG 0x0f
#define DATA_CFG_REG 0x0e
#define TRSMT_CFG_REG 0x0d
#define TRSMT_STATUS_REG 0x04
#define RECV_CFG_REG 0x0c
#define RECV_STATUS_REG 0x0c
#define EN1_PHYS 0x01
#define EN0_COUNTER0 0x0d
#define EN0_IMR 0x0f
#define EN0_ISR 0x07
#define EN0_RSA0 0x08 /* Remote Start Address Lo */
#define EN0_RSA1 0x09 /* Remote Start Address Hi */
#define IMR_VL_PRXE 0x01 /* Packet Received Interrupt Enable */
#define IMR_VL_PTXE 0x02 /* Packet Transmit Interrupt Enable */
#define IMR_VL_RXEE 0x04 /* Receive Error Interrupt Enable */
#define IMR_VL_TXEE 0x08 /* Transmit Error Interrupt Enable */
#define IMR_VL_OVWE 0x10 /* Overwrite Error Interrupt Enable */
#define IMR_VL_CNTE 0x20 /* Counter Overflow Interrupt Enable */
#define IMR_VL_RDCE 0x40 /* Remote DMA Complete Interrupt Enable */
#define ISR_VL_PRX 0x01 /* Packet Received */
#define ISR_VL_PTX 0x02 /* Packet Transmitted */
#define ISR_VL_RXE 0x04 /* Receive Error */
#define ISR_VL_TXE 0x08 /* Transmission Error */
#define ISR_VL_OVW 0x10 /* Overwrite */
#define ISR_VL_CNT 0x20 /* Counter Overflow */
#define ISR_VL_RDC 0x40 /* Remote Data Complete */
#define ISR_VL_RST 0x80 /* Reset status */
#define EN0_PSTART_REG 0x01
#define EN0_PSTOP_REG 0x02
#define EN0_BNRY_REG 0x03
#define REG_PAGE0 0x00
#define REG_PAGE1 0x40
#define REG_PAGE2 0x80
#define REG_PAGE3 0xC0
#define CMD_REMOTE_READ 0x08
#define CMD_REMOTE_WRITE 0x10
#define CMD_SEND_PACKET 0x20
#define CMD_TXP_BIT 0x04
#define CMD_NO_DMA 0x20
#define CMD_STOP 0x01
#define CMD_START 0x02
#define P1_PAR0 0x01
#define P1_PAR1 0x02
#define P1_PAR2 0x03
#define P1_PAR3 0x04
#define P1_PAR4 0x05
#define P1_PAR5 0x06
#define P1_CURR 0x07
#define P1_MAR0 0x08
#define P1_MAR1 0x09
#define P1_MAR2 0x0A
#define P1_MAR3 0x0B
#define P1_MAR4 0x0C
#define P1_MAR5 0x0D
#define P1_MAR6 0x0E
#define P1_MAR7 0x0F
#define P0_RCR 0x0c
#define P0_TCR 0x0d
#define P0_ISR 0x07
#define P0_RBCR0 0x0a /* Remote Byte Count Lo */
#define P0_RBCR1 0x0b /* Remote Byte Count Hi */
#define P0_TBCR0 0x05
#define P0_TBCR1 0x06
#define P0_RSAR0 0x08
#define P0_RSAR1 0x09
#define P0_TPSR 0x04
#define P0_RSR 0x0c
#define P1_CURR 0x07
#define RCR_VL_AB 0x04 /* accept Broadcast */
#define ISR_RESET_BIT 0x80
#define DCR_FT1 0x40
#define DCR_WTS 0x01
#define DCR_LB_S 0x08
#define VENDOR_ID 0x10ec
#define DEVICE_ID 0x8029
#define NE_DATAPORT 0x10
#define NE_PAGE_SIZE 256
#define TIME_OUT 100000
typedef struct _dp8390_pkt_hdr {
BYTE status;
BYTE next_pointer;
WORD packet_length;
} __attribute__ ((packed)) dp8390_pkt_hdr;
typedef struct _cmd_subset {
WORD io_addr;
BYTE value;
} cmd_subset;
static void destroy_nic();
static int read_nic_mem(void *dest, unsigned short src, int n);
static int find_myself();
static void reset_event(int *flag);
static void read_physical_address(char *buffer);
static int prob_ne2000();
static void ne2000_setup();
static void receive_packet();
static void irq_event();
static void wait_dma_complete();
static void wait_transmit_complete();
static int trans_packet(int size, const void *buf);
static void test();
/* デバイス登録用 */
device_operator eth0_operator = {
0,
"dp8390 Ethernet Controller",
1, /* ブロックデバイス */
&init_dp8390,
NULL,
NULL,
NULL,
NULL,
&trans_packet
};
static pci_dev *my_dev = NULL;
/*
意味わかんないんだけど、ne2k系のコントローラには
MACアドレスの1byteがそれぞれ二つずつ記録されてるみたい
6 * 2 = 12
*/
static BYTE physical_address[12] = {0};
static WORD e8390_base = 0x0;
volatile int _flag_packet_trans = 0;
volatile int _flag_dma_comp = 0;
static WORD rx_ring_start;
static WORD rx_ring_end;
static WORD rx_page_start = 72;
/* static WORD rx_page_start = 64; */
static WORD rx_page_end = 128;
static BYTE next_pkt;
void init_dp8390(void)
{
BYTE irq_line;
if (!find_myself()) {
_sys_printf(" NE2000 compatible device not found!\n");
return;
}
_sys_printf(" NIC: find device, pci_num=%d\n", my_dev->pci_num);
irq_line = my_dev->irq_num;
eth0_operator.irq_num = irq_line;
if (!pci_register_irq(irq_line, &irq_event)) {
_sys_printf("failed regist irq line\n");
return;
}
e8390_base = my_dev->io_addr[0];
_sys_printf(" NIC: i/o_addr: %x, irq_num=%d\n", e8390_base, irq_line);
/* eoiを手動に変更(デバドラ側がやるようにする) */
//_sys_change_irq_proc_type(irq_line, IRQ_PROC_TYPE_2);
/* ne2000を本格的にセットアップ */
ne2000_setup();
return;
}
static int read_nic_mem(void *dest, unsigned short src, int n)
{
int i = 0;
char *tmp = (char*)dest;
/*
* DMA改めて初期化、転送中であっても停止
* page0選択
*/
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START);
delay(2);
outb(e8390_base + P0_RBCR0, (n & 0xff));
delay(2);
outb(e8390_base + P0_RBCR1, ((n >> 8) & 0xff));
delay(2);
/* srcアドレス設定 */
outb(e8390_base + EN0_RSA0, (src & 0xff));
delay(2);
outb(e8390_base + EN0_RSA1, ((src >> 8) & 0xff));
delay(2);
outb(e8390_base + COMMAND_REG, CMD_REMOTE_READ | CMD_START);
delay(2);
for ( ; i < n ; i++) {
*(tmp + i) = inb(e8390_base + NS_DATA_PORT);
}
return n;
}
/* mac addressを読み込んでみる */
static void read_physical_address(char *buffer)
{
read_nic_mem(buffer, 0, sizeof(BYTE) * 12);
}
/* ne2000ドライバが正規なものかどうかをチェックする */
static int prob_ne2000(void)
{
BYTE ret;
if ((inb(e8390_base)) == 0xff) {
return 0;
}
/* page0選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_STOP);
delay(100);
ret = inb(e8390_base + COMMAND_REG);
ret &= CMD_NO_DMA | CMD_TXP_BIT | CMD_START | CMD_STOP;
if (ret != (CMD_NO_DMA | CMD_STOP)) {
return 0;
}
ret = inb(e8390_base + INTR_STATUS_REG);
ret &= ISR_RESET_BIT;
if (ret != ISR_RESET_BIT) {
return 0;
}
/* 初期化完了 */
return 1;
}
static void irq_event(void)
{
BYTE irq_value;
/* page0を選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START);
/* prxを除くすべて */
while ((irq_value = inb(e8390_base + P0_ISR)) != 0) {
/* 割り込みは受け取ったと通知する */
outb(e8390_base + P0_ISR, irq_value);
if (irq_value & ISR_VL_PRX) {
/* 新しいパケットが届いたよ! */
receive_packet();
}
if (irq_value & ISR_VL_PTX) {
/* パケット転送完了! */
_flag_packet_trans = 1;
}
if (irq_value & ISR_VL_RDC) {
/* remote DMA complete */
_flag_dma_comp = 1;
}
/* 改めてpage0を選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START);
}
return;
}
static void receive_packet(void)
{
dp8390_pkt_hdr header;
WORD packet_ptr;
WORD len, tmp_len;
BYTE *dest;
BYTE boundary_reg;
tmp_len = 0;
/* Page1を選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START | REG_PAGE1);
/* 未読パケットまですべて読む */
while (next_pkt != inb(e8390_base + P1_CURR)) {
/* 実アドレスに変換 */
packet_ptr = next_pkt * NE_PAGE_SIZE;
/* 最初にヘッダーを読みましょう */
read_nic_mem(&header, packet_ptr, sizeof(dp8390_pkt_hdr));
len = header.packet_length - sizeof(dp8390_pkt_hdr);
dest = (BYTE*)_sys_kmalloc(len);
if (dest == NULL) {
_sys_printf("NIC: alloc error!\n");
return;
}
/* ヘッダー解析 */
_sys_printf("packet receive %d [bytes]\n", len);
/* リングバッファなので... */
if ((packet_ptr + len) > rx_ring_end) {
tmp_len = rx_ring_end - packet_ptr;
read_nic_mem(dest, packet_ptr, tmp_len);
/* dest += tmp_len; */
packet_ptr = rx_ring_start;
len -= tmp_len;
}
/* 残りを読む */
read_nic_mem(dest + tmp_len, packet_ptr, len);
in_recv_packet_que(dest + sizeof(dp8390_pkt_hdr),
header.packet_length - sizeof(dp8390_pkt_hdr));
next_pkt = header.next_pointer;
//_sys_printf("next_pkt: %d\n", header.next_pointer);
/* Page0を選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START | REG_PAGE0);
boundary_reg = next_pkt - 1;
/* リングバッファだから、終端に達したことを考えてないと... */
if (boundary_reg < rx_page_start) {
boundary_reg = rx_page_end - 1;
}
/* boundary register updata */
outb(e8390_base + EN0_BNRY_REG, boundary_reg);
/* Page1を再選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START | REG_PAGE1);
}
in_task_que(&start_packet_store);
}
static void get_packet(void *dest, WORD src, int n)
{
WORD size;
/* リングバッファなので... */
if((src + n) > rx_ring_end){
size = rx_ring_end - src;
read_nic_mem(dest, src, size);
dest += size;
src = rx_ring_start;
n -= size;
}
/* 残りを読む */
read_nic_mem(dest, src, n);
}
static void ne2000_setup(void)
{
int i;
BYTE bound_ptr_reg;
ethernet_addr mac_addr;
BYTE c;
if(!prob_ne2000()){
_sys_printf(" NIC: failed initialize\n");
return;
}
_flag_packet_trans = _flag_dma_comp = 0;
bound_ptr_reg = rx_page_start;
next_pkt = rx_page_start + 1;
rx_ring_start = rx_page_start * NE_PAGE_SIZE;
rx_ring_end = rx_page_end * NE_PAGE_SIZE;
/* nicのリセット */
c = inb(e8390_base + NS_RESET);
outb(e8390_base + NS_RESET, c);
delay(600);
/* CMD全ストップ、DMA初期化 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_STOP);
/*
1.FIFO Threshold Select 1
2.Loop Back mode select
3.Byte order 80x86 mode
(bytes単位での転送はあまり効率がよくないかもなぁ、word単位でも設定できるよ)
*/
outb(e8390_base + DATA_CFG_REG, DCR_FT1 | DCR_LB_S);
read_physical_address(physical_address);
for(i=0;i<6;++i){
mac_addr.byte[i] = physical_address[i * 2];
}
/* ethernetに登録 */
regist_eth_addr(mac_addr);
_sys_printf(" NIC MAC-address: %x:%x:%x:%x:%x:%x\n",
physical_address[0],physical_address[2],physical_address[4],
physical_address[6],physical_address[8],physical_address[10]);
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_STOP);
/* Remote Count Register (Low, High)を初期化 */
outb(e8390_base + P0_RBCR0, 0x0);
outb(e8390_base + P0_RBCR1, 0x0);
/* moniter mode */
outb(e8390_base + P0_RCR, 0x20);
/* loop back mode */
outb(e8390_base + P0_TCR, 2);
/* ring bufferの準備 */
outb(e8390_base + EN0_PSTART_REG, rx_page_start);
outb(e8390_base + EN0_PSTOP_REG, rx_page_end);
outb(e8390_base + EN0_BNRY_REG, bound_ptr_reg);
/* 割り込みを有効にする */
/*
Counter Overflow interruptは無し
*/
outb(e8390_base + REG_PAGE0 + EN0_IMR,
IMR_VL_PRXE | IMR_VL_PTXE | IMR_VL_RXEE | IMR_VL_TXEE | IMR_VL_OVWE | IMR_VL_RDCE);
outb(e8390_base + P0_ISR, 0xff);
//outb(e8390_base + REG_PAGE0 + EN0_IMR, 0x7F);
/* page1 選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_STOP | REG_PAGE1);
/* 物理アドレスをPARに書き込む */
outb(e8390_base + P1_PAR0, physical_address[0]);
outb(e8390_base + P1_PAR1, physical_address[2]);
outb(e8390_base + P1_PAR2, physical_address[4]);
outb(e8390_base + P1_PAR3, physical_address[6]);
outb(e8390_base + P1_PAR4, physical_address[8]);
outb(e8390_base + P1_PAR5, physical_address[10]);
/*
Current Page Register
パケットを格納するのに使用される最初のbufferを示す
*/
outb(e8390_base + P1_CURR, next_pkt);
/* マルチキャストアドレスをMARに書き込む */
outb(e8390_base + P1_MAR0, 0x0);
outb(e8390_base + P1_MAR1, 0x0);
outb(e8390_base + P1_MAR2, 0x0);
outb(e8390_base + P1_MAR3, 0x0);
outb(e8390_base + P1_MAR4, 0x0);
outb(e8390_base + P1_MAR5, 0x0);
outb(e8390_base + P1_MAR6, 0x0);
outb(e8390_base + P1_MAR7, 0x0);
/* page0 選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_STOP | REG_PAGE0);
/* broad castを受け付けるようにする */
outb(e8390_base + P0_RCR, RCR_VL_AB);
/* Normal Operation */
outb(e8390_base + P0_TCR, 0x0);
/* 割り込み状態を示すISRを初期化 */
outb(e8390_base + P0_ISR, 0xff);
/* NICリフトオフ! */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START);
/*
delay(10);
for(i=0;i<10;i++){
test();
delay(1000);
}
*/
}
static void test(void)
{
BYTE *test_buf = (BYTE*)_sys_kmalloc(256);
_sys_memset32((void*)test_buf, 'a', 256);
test_buf[0] = 0x00;
test_buf[1] = 0x07;
test_buf[2] = 0x40;
test_buf[3] = 0xcc;
test_buf[4] = 0xe4;
test_buf[5] = 0x78;
test_buf[6] = physical_address[0];
test_buf[7] = physical_address[2];
test_buf[8] = physical_address[4];
test_buf[9] = physical_address[6];
test_buf[10] = physical_address[8];
test_buf[11] = physical_address[10];
test_buf[12] = 0x01;
test_buf[13] = 0x00;
trans_packet(256, test_buf);
_sys_printf("transfer\n send size %d\n", 256);
}
/* pciデバイスの中にありますか? */
static int find_myself(void)
{
pci_dev *ptr;
ptr = find_pci_dev_from_id(VENDOR_ID, DEVICE_ID);
if (ptr != NULL) {
my_dev = ptr;
return 1;
}
return 0;
}
static int trans_packet(int size, const void *buf)
{
DWORD i;
char *ptr = (char*)buf;
DWORD buf_size = (DWORD)size;
WORD dest_ptr = rx_page_end;
/* DMA completeとtransmitが完了するまで待ちましょう */
/*
reset_event(&_flag_dma_comp);
reset_event(&_flag_packet_trans);
*/
/* 送信完了かどうかをチェックする */
while ((inb(e8390_base + COMMAND_REG) & 0x04) != 0);
cli();
/* page0 選択 */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_START | REG_PAGE0);
/* ISRのDMA complete flagを立てる */
outb(e8390_base + P0_ISR, ISR_VL_RDC);
/* どれだけのサイズを転送するのかなぁ */
outb(e8390_base + P0_RBCR0, buf_size);
outb(e8390_base + P0_RBCR1, buf_size >> 8);
/* どこに転送するのかなぁ */
outb(e8390_base + P0_RSAR0, (dest_ptr * NE_PAGE_SIZE));
outb(e8390_base + P0_RSAR1, (dest_ptr * NE_PAGE_SIZE) >> 8);
sti();
/* DMAにNIC内部バッファに書き込んでもらいましょう */
outb(e8390_base + COMMAND_REG, CMD_REMOTE_WRITE | CMD_START);
/* データを書き込む */
for (i = 0 ; i < buf_size ; i++) {
outb(e8390_base + NS_DATA_PORT, ptr[i]);
delay(10);
}
/* IRQから、DMA completeが通知されるまで待ちましょう */
wait_dma_complete();
/* どこから転送しますか? */
outb(e8390_base + P0_TPSR, dest_ptr);
/* 64byte未満のパケットは転送できないんですよ */
if (buf_size > 64) {
outb(e8390_base + P0_TBCR0, buf_size);
outb(e8390_base + P0_TBCR1, buf_size >> 8);
} else {
outb(e8390_base + P0_TBCR0, 64);
outb(e8390_base + P0_TBCR1, 0);
}
/* 転送を開始しましょう */
outb(e8390_base + COMMAND_REG, CMD_NO_DMA | CMD_TXP_BIT | CMD_START);
/* パケットが転送完了されるまで待ちましょう */
wait_transmit_complete();
return buf_size;
}
static void destroy_nic(void)
{
}
static void wait_dma_complete(void)
{
int i = 0;
while (!_flag_dma_comp) {
++i;
if (i >= TIME_OUT) {
_sys_printf("dma time out\n");
return;
}
delay(2);
}
_flag_dma_comp = 0;
}
static void wait_transmit_complete(void)
{
int i = 0;
while (!_flag_packet_trans) {
++i;
if (i >= TIME_OUT) {
_sys_printf("trasmit wait time out\n");
return;
}
delay(2);
}
_flag_packet_trans = 0;
}
static void reset_event(int *flag)
{
*flag = 0;
}