1 /*
2 	drivers/net/tulip/eeprom.c
3 
4 	Maintained by Jeff Garzik <jgarzik@pobox.com>
5 	Copyright 2000,2001  The Linux Kernel Team
6 	Written/copyright 1994-2001 by Donald Becker.
7 
8 	This software may be used and distributed according to the terms
9 	of the GNU General Public License, incorporated herein by reference.
10 
11 	Please refer to Documentation/DocBook/tulip.{pdf,ps,html}
12 	for more information on this driver, or visit the project
13 	Web page at http://sourceforge.net/projects/tulip/
14 
15 */
16 
17 #include <linux/pci.h>
18 #include "tulip.h"
19 #include <linux/init.h>
20 #include <asm/unaligned.h>
21 
22 
23 
24 /* Serial EEPROM section. */
25 /* The main routine to parse the very complicated SROM structure.
26    Search www.digital.com for "21X4 SROM" to get details.
27    This code is very complex, and will require changes to support
28    additional cards, so I'll be verbose about what is going on.
29    */
30 
31 /* Known cards that have old-style EEPROMs. */
32 static struct eeprom_fixup eeprom_fixups[] __devinitdata = {
33   {"Asante", 0, 0, 0x94, {0x1e00, 0x0000, 0x0800, 0x0100, 0x018c,
34 			  0x0000, 0x0000, 0xe078, 0x0001, 0x0050, 0x0018 }},
35   {"SMC9332DST", 0, 0, 0xC0, { 0x1e00, 0x0000, 0x0800, 0x041f,
36 			   0x0000, 0x009E, /* 10baseT */
37 			   0x0004, 0x009E, /* 10baseT-FD */
38 			   0x0903, 0x006D, /* 100baseTx */
39 			   0x0905, 0x006D, /* 100baseTx-FD */ }},
40   {"Cogent EM100", 0, 0, 0x92, { 0x1e00, 0x0000, 0x0800, 0x063f,
41 				 0x0107, 0x8021, /* 100baseFx */
42 				 0x0108, 0x8021, /* 100baseFx-FD */
43 				 0x0100, 0x009E, /* 10baseT */
44 				 0x0104, 0x009E, /* 10baseT-FD */
45 				 0x0103, 0x006D, /* 100baseTx */
46 				 0x0105, 0x006D, /* 100baseTx-FD */ }},
47   {"Maxtech NX-110", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x0513,
48 				   0x1001, 0x009E, /* 10base2, CSR12 0x10*/
49 				   0x0000, 0x009E, /* 10baseT */
50 				   0x0004, 0x009E, /* 10baseT-FD */
51 				   0x0303, 0x006D, /* 100baseTx, CSR12 0x03 */
52 				   0x0305, 0x006D, /* 100baseTx-FD CSR12 0x03 */}},
53   {"Accton EN1207", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x051F,
54 				  0x1B01, 0x0000, /* 10base2,   CSR12 0x1B */
55 				  0x0B00, 0x009E, /* 10baseT,   CSR12 0x0B */
56 				  0x0B04, 0x009E, /* 10baseT-FD,CSR12 0x0B */
57 				  0x1B03, 0x006D, /* 100baseTx, CSR12 0x1B */
58 				  0x1B05, 0x006D, /* 100baseTx-FD CSR12 0x1B */
59    }},
60   {"NetWinder", 0x00, 0x10, 0x57,
61 	/* Default media = MII
62 	 * MII block, reset sequence (3) = 0x0821 0x0000 0x0001, capabilities 0x01e1
63 	 */
64 	{ 0x1e00, 0x0000, 0x000b, 0x8f01, 0x0103, 0x0300, 0x0821, 0x000, 0x0001, 0x0000, 0x01e1 }
65   },
66   {0, 0, 0, 0, {}}};
67 
68 
69 static const char *block_name[] __devinitdata = {
70 	"21140 non-MII",
71 	"21140 MII PHY",
72 	"21142 Serial PHY",
73 	"21142 MII PHY",
74 	"21143 SYM PHY",
75 	"21143 reset method"
76 };
77 
78 
79 /**
80  * tulip_build_fake_mediatable - Build a fake mediatable entry.
81  * @tp: Ptr to the tulip private data.
82  *
83  * Some cards like the 3x5 HSC cards (J3514A) do not have a standard
84  * srom and can not be handled under the fixup routine.  These cards
85  * still need a valid mediatable entry for correct csr12 setup and
86  * mii handling.
87  *
88  * Since this is currently a parisc-linux specific function, the
89  * #ifdef __hppa__ should completely optimize this function away for
90  * non-parisc hardware.
91  */
tulip_build_fake_mediatable(struct tulip_private * tp)92 static void __devinit tulip_build_fake_mediatable(struct tulip_private *tp)
93 {
94 #ifdef __hppa__
95 	unsigned char *ee_data = tp->eeprom;
96 
97 	if (ee_data[0] == 0x3c && ee_data[1] == 0x10 &&
98 		(ee_data[2] == 0x63 || ee_data[2] == 0x61) && ee_data[3] == 0x10) {
99 
100 		static unsigned char leafdata[] =
101 			{ 0x01,       /* phy number */
102 			  0x02,       /* gpr setup sequence length */
103 			  0x02, 0x00, /* gpr setup sequence */
104 			  0x02,       /* phy reset sequence length */
105 			  0x01, 0x00, /* phy reset sequence */
106 			  0x00, 0x78, /* media capabilities */
107 			  0x00, 0xe0, /* nway advertisment */
108 			  0x00, 0x05, /* fdx bit map */
109 			  0x00, 0x06  /* ttm bit map */
110 			};
111 
112 		tp->mtable = (struct mediatable *)
113 			kmalloc(sizeof(struct mediatable) + sizeof(struct medialeaf), GFP_KERNEL);
114 
115 		if (tp->mtable == NULL)
116 			return; /* Horrible, impossible failure. */
117 
118 		tp->mtable->defaultmedia = 0x800;
119 		tp->mtable->leafcount = 1;
120 		tp->mtable->csr12dir = 0x3f; /* inputs on bit7 for hsc-pci, bit6 for pci-fx */
121 		tp->mtable->has_nonmii = 0;
122 		tp->mtable->has_reset = 0;
123 		tp->mtable->has_mii = 1;
124 		tp->mtable->csr15dir = tp->mtable->csr15val = 0;
125 		tp->mtable->mleaf[0].type = 1;
126 		tp->mtable->mleaf[0].media = 11;
127 		tp->mtable->mleaf[0].leafdata = &leafdata[0];
128 		tp->flags |= HAS_PHY_IRQ;
129 		tp->csr12_shadow = -1;
130 	}
131 #endif
132 }
133 
tulip_parse_eeprom(struct net_device * dev)134 void __devinit tulip_parse_eeprom(struct net_device *dev)
135 {
136 	/* The last media info list parsed, for multiport boards.  */
137 	static struct mediatable *last_mediatable;
138 	static unsigned char *last_ee_data;
139 	static int controller_index;
140 	struct tulip_private *tp = (struct tulip_private *)dev->priv;
141 	unsigned char *ee_data = tp->eeprom;
142 	int i;
143 
144 	tp->mtable = 0;
145 	/* Detect an old-style (SA only) EEPROM layout:
146 	   memcmp(eedata, eedata+16, 8). */
147 	for (i = 0; i < 8; i ++)
148 		if (ee_data[i] != ee_data[16+i])
149 			break;
150 	if (i >= 8) {
151 		if (ee_data[0] == 0xff) {
152 			if (last_mediatable) {
153 				controller_index++;
154 				printk(KERN_INFO "%s:  Controller %d of multiport board.\n",
155 					   dev->name, controller_index);
156 				tp->mtable = last_mediatable;
157 				ee_data = last_ee_data;
158 				goto subsequent_board;
159 			} else
160 				printk(KERN_INFO "%s:  Missing EEPROM, this interface may "
161 					   "not work correctly!\n",
162 			   dev->name);
163 			return;
164 		}
165 	  /* Do a fix-up based on the vendor half of the station address prefix. */
166 	  for (i = 0; eeprom_fixups[i].name; i++) {
167 		if (dev->dev_addr[0] == eeprom_fixups[i].addr0
168 			&&  dev->dev_addr[1] == eeprom_fixups[i].addr1
169 			&&  dev->dev_addr[2] == eeprom_fixups[i].addr2) {
170 		  if (dev->dev_addr[2] == 0xE8  &&  ee_data[0x1a] == 0x55)
171 			  i++;			/* An Accton EN1207, not an outlaw Maxtech. */
172 		  memcpy(ee_data + 26, eeprom_fixups[i].newtable,
173 				 sizeof(eeprom_fixups[i].newtable));
174 		  printk(KERN_INFO "%s: Old format EEPROM on '%s' board.  Using"
175 				 " substitute media control info.\n",
176 				 dev->name, eeprom_fixups[i].name);
177 		  break;
178 		}
179 	  }
180 	  if (eeprom_fixups[i].name == NULL) { /* No fixup found. */
181 		  printk(KERN_INFO "%s: Old style EEPROM with no media selection "
182 				 "information.\n",
183 			   dev->name);
184 		return;
185 	  }
186 	}
187 
188 	controller_index = 0;
189 	if (ee_data[19] > 1) {		/* Multiport board. */
190 		last_ee_data = ee_data;
191 	}
192 subsequent_board:
193 
194 	if (ee_data[27] == 0) {		/* No valid media table. */
195 		tulip_build_fake_mediatable(tp);
196 	} else if (tp->chip_id == DC21041) {
197 		unsigned char *p = (void *)ee_data + ee_data[27 + controller_index*3];
198 		int media = get_u16(p);
199 		int count = p[2];
200 		p += 3;
201 
202 		printk(KERN_INFO "%s: 21041 Media table, default media %4.4x (%s).\n",
203 			   dev->name, media,
204 			   media & 0x0800 ? "Autosense" : medianame[media & MEDIA_MASK]);
205 		for (i = 0; i < count; i++) {
206 			unsigned char media_block = *p++;
207 			int media_code = media_block & MEDIA_MASK;
208 			if (media_block & 0x40)
209 				p += 6;
210 			printk(KERN_INFO "%s:  21041 media #%d, %s.\n",
211 				   dev->name, media_code, medianame[media_code]);
212 		}
213 	} else {
214 		unsigned char *p = (void *)ee_data + ee_data[27];
215 		unsigned char csr12dir = 0;
216 		int count, new_advertise = 0;
217 		struct mediatable *mtable;
218 		u16 media = get_u16(p);
219 
220 		p += 2;
221 		if (tp->flags & CSR12_IN_SROM)
222 			csr12dir = *p++;
223 		count = *p++;
224 
225 	        /* there is no phy information, don't even try to build mtable */
226 	        if (count == 0) {
227 			if (tulip_debug > 0)
228 				printk(KERN_WARNING "%s: no phy info, aborting mtable build\n", dev->name);
229 		        return;
230 		}
231 
232 		mtable = (struct mediatable *)
233 			kmalloc(sizeof(struct mediatable) + count*sizeof(struct medialeaf),
234 					GFP_KERNEL);
235 		if (mtable == NULL)
236 			return;				/* Horrible, impossible failure. */
237 		last_mediatable = tp->mtable = mtable;
238 		mtable->defaultmedia = media;
239 		mtable->leafcount = count;
240 		mtable->csr12dir = csr12dir;
241 		mtable->has_nonmii = mtable->has_mii = mtable->has_reset = 0;
242 		mtable->csr15dir = mtable->csr15val = 0;
243 
244 		printk(KERN_INFO "%s:  EEPROM default media type %s.\n", dev->name,
245 			   media & 0x0800 ? "Autosense" : medianame[media & MEDIA_MASK]);
246 		for (i = 0; i < count; i++) {
247 			struct medialeaf *leaf = &mtable->mleaf[i];
248 
249 			if ((p[0] & 0x80) == 0) { /* 21140 Compact block. */
250 				leaf->type = 0;
251 				leaf->media = p[0] & 0x3f;
252 				leaf->leafdata = p;
253 				if ((p[2] & 0x61) == 0x01)	/* Bogus, but Znyx boards do it. */
254 					mtable->has_mii = 1;
255 				p += 4;
256 			} else {
257 				leaf->type = p[1];
258 				if (p[1] == 0x05) {
259 					mtable->has_reset = i;
260 					leaf->media = p[2] & 0x0f;
261 				} else if (tp->chip_id == DM910X && p[1] == 0x80) {
262 					/* Hack to ignore Davicom delay period block */
263 					mtable->leafcount--;
264 					count--;
265 					i--;
266 					leaf->leafdata = p + 2;
267 					p += (p[0] & 0x3f) + 1;
268 					continue;
269 				} else if (p[1] & 1) {
270 					int gpr_len, reset_len;
271 
272 					mtable->has_mii = 1;
273 					leaf->media = 11;
274 					gpr_len=p[3]*2;
275 					reset_len=p[4+gpr_len]*2;
276 					new_advertise |= get_u16(&p[7+gpr_len+reset_len]);
277 				} else {
278 					mtable->has_nonmii = 1;
279 					leaf->media = p[2] & MEDIA_MASK;
280 					/* Davicom's media number for 100BaseTX is strange */
281 					if (tp->chip_id == DM910X && leaf->media == 1)
282 						leaf->media = 3;
283 					switch (leaf->media) {
284 					case 0: new_advertise |= 0x0020; break;
285 					case 4: new_advertise |= 0x0040; break;
286 					case 3: new_advertise |= 0x0080; break;
287 					case 5: new_advertise |= 0x0100; break;
288 					case 6: new_advertise |= 0x0200; break;
289 					}
290 					if (p[1] == 2  &&  leaf->media == 0) {
291 						if (p[2] & 0x40) {
292 							u32 base15 = get_unaligned((u16*)&p[7]);
293 							mtable->csr15dir =
294 								(get_unaligned((u16*)&p[9])<<16) + base15;
295 							mtable->csr15val =
296 								(get_unaligned((u16*)&p[11])<<16) + base15;
297 						} else {
298 							mtable->csr15dir = get_unaligned((u16*)&p[3])<<16;
299 							mtable->csr15val = get_unaligned((u16*)&p[5])<<16;
300 						}
301 					}
302 				}
303 				leaf->leafdata = p + 2;
304 				p += (p[0] & 0x3f) + 1;
305 			}
306 			if (tulip_debug > 1  &&  leaf->media == 11) {
307 				unsigned char *bp = leaf->leafdata;
308 				printk(KERN_INFO "%s:  MII interface PHY %d, setup/reset "
309 					   "sequences %d/%d long, capabilities %2.2x %2.2x.\n",
310 					   dev->name, bp[0], bp[1], bp[2 + bp[1]*2],
311 					   bp[5 + bp[2 + bp[1]*2]*2], bp[4 + bp[2 + bp[1]*2]*2]);
312 			}
313 			printk(KERN_INFO "%s:  Index #%d - Media %s (#%d) described "
314 				   "by a %s (%d) block.\n",
315 				   dev->name, i, medianame[leaf->media & 15], leaf->media,
316 				   leaf->type < ARRAY_SIZE(block_name) ? block_name[leaf->type] : "<unknown>",
317 				   leaf->type);
318 		}
319 		if (new_advertise)
320 			tp->sym_advertise = new_advertise;
321 	}
322 }
323 /* Reading a serial EEPROM is a "bit" grungy, but we work our way through:->.*/
324 
325 /*  EEPROM_Ctrl bits. */
326 #define EE_SHIFT_CLK	0x02	/* EEPROM shift clock. */
327 #define EE_CS			0x01	/* EEPROM chip select. */
328 #define EE_DATA_WRITE	0x04	/* Data from the Tulip to EEPROM. */
329 #define EE_WRITE_0		0x01
330 #define EE_WRITE_1		0x05
331 #define EE_DATA_READ	0x08	/* Data from the EEPROM chip. */
332 #define EE_ENB			(0x4800 | EE_CS)
333 
334 /* Delay between EEPROM clock transitions.
335    Even at 33Mhz current PCI implementations don't overrun the EEPROM clock.
336    We add a bus turn-around to insure that this remains true. */
337 #define eeprom_delay()	inl(ee_addr)
338 
339 /* The EEPROM commands include the alway-set leading bit. */
340 #define EE_READ_CMD		(6)
341 
342 /* Note: this routine returns extra data bits for size detection. */
tulip_read_eeprom(long ioaddr,int location,int addr_len)343 int __devinit tulip_read_eeprom(long ioaddr, int location, int addr_len)
344 {
345 	int i;
346 	unsigned retval = 0;
347 	long ee_addr = ioaddr + CSR9;
348 	int read_cmd = location | (EE_READ_CMD << addr_len);
349 
350 	outl(EE_ENB & ~EE_CS, ee_addr);
351 	outl(EE_ENB, ee_addr);
352 
353 	/* Shift the read command bits out. */
354 	for (i = 4 + addr_len; i >= 0; i--) {
355 		short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
356 		outl(EE_ENB | dataval, ee_addr);
357 		eeprom_delay();
358 		outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
359 		eeprom_delay();
360 		retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
361 	}
362 	outl(EE_ENB, ee_addr);
363 	eeprom_delay();
364 
365 	for (i = 16; i > 0; i--) {
366 		outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
367 		eeprom_delay();
368 		retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
369 		outl(EE_ENB, ee_addr);
370 		eeprom_delay();
371 	}
372 
373 	/* Terminate the EEPROM access. */
374 	outl(EE_ENB & ~EE_CS, ee_addr);
375 	return retval;
376 }
377 
378