xref: /plan9/sys/src/9/pc/etherec2t.c (revision aa72973a2891ccbd3fb042462446761159389e19)
1 /*
2  * Supposed NE2000 PCMCIA clones, see the comments in ether2000.c
3  */
4 #include "u.h"
5 #include "../port/lib.h"
6 #include "mem.h"
7 #include "dat.h"
8 #include "fns.h"
9 #include "io.h"
10 #include "../port/error.h"
11 #include "../port/netif.h"
12 
13 #include "etherif.h"
14 #include "ether8390.h"
15 
16 enum {
17 	Data		= 0x10,		/* offset from I/O base of data port */
18 	Reset		= 0x1F,		/* offset from I/O base of reset port */
19 };
20 
21 typedef struct Ec2t {
22 	char*	name;
23 	int	iochecksum;
24 } Ec2t;
25 
26 static Ec2t ec2tpcmcia[] = {
27 	{ "EC2T", 0, },			/* Linksys Combo PCMCIA EthernetCard */
28 	{ "PCMPC100", 1, },		/* EtherFast 10/100 PC Card */
29 	{ "PCM100", 1, },		/* EtherFast PCM100 Card */
30 	{ "EN2216", 0, },		/* Accton EtherPair-PCMCIA */
31 	{ "FA410TX", 1, },		/* Netgear FA410TX */
32 	{ "FA411", 0 },			/* Netgear FA411 PCMCIA */
33 	{ "Network Everywhere", 0, },	/* Linksys NP10T 10BaseT Card */
34 	{ "10/100 Port Attached", 1, },	/* SMC 8040TX */
35 	{ "8041TX-10/100-PC-Card-V2", 0 }, /* SMC 8041TX */
36 	{ "SMC8022", 0},		/* SMC 8022 / EZCard-10-PCMCIA */
37 	{ nil, 0, },
38 };
39 
40 static int
reset(Ether * ether)41 reset(Ether* ether)
42 {
43 	ushort buf[16];
44 	ulong port;
45 	Dp8390 *ctlr;
46 	int i, slot;
47 	uchar ea[Eaddrlen], sum, x;
48 	Ec2t *ec2t, tmpec2t;
49 
50 	/*
51 	 * Set up the software configuration.
52 	 * Use defaults for port, irq, mem and size
53 	 * if not specified.
54 	 * The manual says 16KB memory, the box
55 	 * says 32KB. The manual seems to be correct.
56 	 */
57 	if(ether->port == 0)
58 		ether->port = 0x300;
59 	if(ether->irq == 0)
60 		ether->irq = 9;
61 	if(ether->mem == 0)
62 		ether->mem = 0x4000;
63 	if(ether->size == 0)
64 		ether->size = 16*1024;
65 	port = ether->port;
66 
67 	if(ioalloc(ether->port, 0x20, 0, "ec2t") < 0)
68 		return -1;
69 	slot = -1;
70 	for(ec2t = ec2tpcmcia; ec2t->name != nil; ec2t++){
71 		if((slot = pcmspecial(ec2t->name, ether)) >= 0)
72 			break;
73 	}
74 	if(ec2t->name == nil){
75 		ec2t = &tmpec2t;
76 		ec2t->name = nil;
77 		ec2t->iochecksum = 0;
78 		for(i = 0; i < ether->nopt; i++){
79 			if(cistrncmp(ether->opt[i], "id=", 3) == 0){
80 				ec2t->name = &ether->opt[i][3];
81 				slot = pcmspecial(ec2t->name, ether);
82 			}
83 			else if(cistrncmp(ether->opt[i], "iochecksum", 10) == 0)
84 				ec2t->iochecksum = 1;
85 		}
86 	}
87 	if(slot < 0){
88 		iofree(port);
89 		return -1;
90 	}
91 
92 	ether->ctlr = malloc(sizeof(Dp8390));
93 	ctlr = ether->ctlr;
94 	if(ctlr == nil) {
95 		iofree(port);
96 		error(Enomem);
97 	}
98 	ctlr->width = 2;
99 	ctlr->ram = 0;
100 
101 	ctlr->port = port;
102 	ctlr->data = port+Data;
103 
104 	ctlr->tstart = HOWMANY(ether->mem, Dp8390BufSz);
105 	ctlr->pstart = ctlr->tstart + HOWMANY(sizeof(Etherpkt), Dp8390BufSz);
106 	ctlr->pstop = ctlr->tstart + HOWMANY(ether->size, Dp8390BufSz);
107 
108 	ctlr->dummyrr = 0;
109 	for(i = 0; i < ether->nopt; i++){
110 		if(cistrcmp(ether->opt[i], "nodummyrr") == 0)
111 			ctlr->dummyrr = 0;
112 		else if(cistrncmp(ether->opt[i], "dummyrr=", 8) == 0)
113 			ctlr->dummyrr = strtol(&ether->opt[i][8], nil, 0);
114 	}
115 
116 	/*
117 	 * Reset the board. This is done by doing a read
118 	 * followed by a write to the Reset address.
119 	 */
120 	buf[0] = inb(port+Reset);
121 	delay(2);
122 	outb(port+Reset, buf[0]);
123 	delay(2);
124 
125 	/*
126 	 * Init the (possible) chip, then use the (possible)
127 	 * chip to read the (possible) PROM for ethernet address
128 	 * and a marker byte.
129 	 * Could just look at the DP8390 command register after
130 	 * initialisation has been tried, but that wouldn't be
131 	 * enough, there are other ethernet boards which could
132 	 * match.
133 	 */
134 	dp8390reset(ether);
135 	sum = 0;
136 	if(ec2t->iochecksum){
137 		/*
138 		 * These cards have the ethernet address in I/O space.
139 		 * There's a checksum over 8 bytes which sums to 0xFF.
140 		 */
141 		for(i = 0; i < 8; i++){
142 			x = inb(port+0x14+i);
143 			sum += x;
144 			buf[i] = (x<<8)|x;
145 		}
146 	}
147 	else{
148 		memset(buf, 0, sizeof(buf));
149 		dp8390read(ctlr, buf, 0, sizeof(buf));
150 		if((buf[0x0E] & 0xFF) == 0x57 && (buf[0x0F] & 0xFF) == 0x57)
151 			sum = 0xFF;
152 	}
153 	if(sum != 0xFF){
154 		pcmspecialclose(slot);
155 		iofree(ether->port);
156 		free(ether->ctlr);
157 		return -1;
158 	}
159 
160 	/*
161 	 * Stupid machine. Shorts were asked for,
162 	 * shorts were delivered, although the PROM is a byte array.
163 	 * Set the ethernet address.
164 	 */
165 	memset(ea, 0, Eaddrlen);
166 	if(memcmp(ea, ether->ea, Eaddrlen) == 0){
167 		for(i = 0; i < sizeof(ether->ea); i++)
168 			ether->ea[i] = buf[i];
169 	}
170 	dp8390setea(ether);
171 
172 	return 0;
173 }
174 
175 void
etherec2tlink(void)176 etherec2tlink(void)
177 {
178 	addethercard("EC2T", reset);
179 }
180