xref: /linux/drivers/net/ethernet/ti/tlan.c (revision 0b87d2ab030fd969ea626b2da95594949a2c0508)
1 /*******************************************************************************
2  *
3  *  Linux ThunderLAN Driver
4  *
5  *  tlan.c
6  *  by James Banks
7  *
8  *  (C) 1997-1998 Caldera, Inc.
9  *  (C) 1998 James Banks
10  *  (C) 1999-2001 Torben Mathiasen
11  *  (C) 2002 Samuel Chessman
12  *
13  *  This software may be used and distributed according to the terms
14  *  of the GNU General Public License, incorporated herein by reference.
15  *
16  ** Useful (if not required) reading:
17  *
18  *		Texas Instruments, ThunderLAN Programmer's Guide,
19  *			TI Literature Number SPWU013A
20  *			available in PDF format from www.ti.com
21  *		Level One, LXT901 and LXT970 Data Sheets
22  *			available in PDF format from www.level1.com
23  *		National Semiconductor, DP83840A Data Sheet
24  *			available in PDF format from www.national.com
25  *		Microchip Technology, 24C01A/02A/04A Data Sheet
26  *			available in PDF format from www.microchip.com
27  *
28  ******************************************************************************/
29 
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 
32 #include <linux/hardirq.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/interrupt.h>
36 #include <linux/ioport.h>
37 #include <linux/eisa.h>
38 #include <linux/pci.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/delay.h>
43 #include <linux/spinlock.h>
44 #include <linux/workqueue.h>
45 #include <linux/mii.h>
46 
47 #include "tlan.h"
48 
49 
50 /* For removing EISA devices */
51 static	struct net_device	*tlan_eisa_devices;
52 
53 static	int		tlan_devices_installed;
54 
55 /* Set speed, duplex and aui settings */
56 static  int aui[MAX_TLAN_BOARDS];
57 static  int duplex[MAX_TLAN_BOARDS];
58 static  int speed[MAX_TLAN_BOARDS];
59 static  int boards_found;
60 module_param_array(aui, int, NULL, 0);
61 module_param_array(duplex, int, NULL, 0);
62 module_param_array(speed, int, NULL, 0);
63 MODULE_PARM_DESC(aui, "ThunderLAN use AUI port(s) (0-1)");
64 MODULE_PARM_DESC(duplex,
65 		 "ThunderLAN duplex setting(s) (0-default, 1-half, 2-full)");
66 MODULE_PARM_DESC(speed, "ThunderLAN port speed setting(s) (0,10,100)");
67 
68 MODULE_AUTHOR("Maintainer: Samuel Chessman <chessman@tux.org>");
69 MODULE_DESCRIPTION("Driver for TI ThunderLAN based ethernet PCI adapters");
70 MODULE_LICENSE("GPL");
71 
72 /* Turn on debugging.
73  * See Documentation/networking/device_drivers/ethernet/ti/tlan.rst for details
74  */
75 static  int		debug;
76 module_param(debug, int, 0);
77 MODULE_PARM_DESC(debug, "ThunderLAN debug mask");
78 
79 static	const char tlan_signature[] = "TLAN";
80 static  const char tlan_banner[] = "ThunderLAN driver v1.17\n";
81 static  int tlan_have_pci;
82 static  int tlan_have_eisa;
83 
84 static const char * const media[] = {
85 	"10BaseT-HD", "10BaseT-FD", "100baseTx-HD",
86 	"100BaseTx-FD", "100BaseT4", NULL
87 };
88 
89 static struct board {
90 	const char	*device_label;
91 	u32		flags;
92 	u16		addr_ofs;
93 } board_info[] = {
94 	{ "Compaq Netelligent 10 T PCI UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
95 	{ "Compaq Netelligent 10/100 TX PCI UTP",
96 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
97 	{ "Compaq Integrated NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
98 	{ "Compaq NetFlex-3/P",
99 	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
100 	{ "Compaq NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
101 	{ "Compaq Netelligent Integrated 10/100 TX UTP",
102 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
103 	{ "Compaq Netelligent Dual 10/100 TX PCI UTP",
104 	  TLAN_ADAPTER_NONE, 0x83 },
105 	{ "Compaq Netelligent 10/100 TX Embedded UTP",
106 	  TLAN_ADAPTER_NONE, 0x83 },
107 	{ "Olicom OC-2183/2185", TLAN_ADAPTER_USE_INTERN_10, 0x83 },
108 	{ "Olicom OC-2325", TLAN_ADAPTER_ACTIVITY_LED |
109 	  TLAN_ADAPTER_UNMANAGED_PHY, 0xf8 },
110 	{ "Olicom OC-2326", TLAN_ADAPTER_ACTIVITY_LED |
111 	  TLAN_ADAPTER_USE_INTERN_10, 0xf8 },
112 	{ "Compaq Netelligent 10/100 TX UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
113 	{ "Compaq Netelligent 10 T/2 PCI UTP/coax", TLAN_ADAPTER_NONE, 0x83 },
114 	{ "Compaq NetFlex-3/E",
115 	  TLAN_ADAPTER_ACTIVITY_LED |	/* EISA card */
116 	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
117 	{ "Compaq NetFlex-3/E",
118 	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 }, /* EISA card */
119 };
120 
121 static const struct pci_device_id tlan_pci_tbl[] = {
122 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL10), .driver_data = 0 },
123 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100), .driver_data = 1 },
124 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3I), .driver_data = 2 },
125 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_THUNDER), .driver_data = 3 },
126 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3B), .driver_data = 4 },
127 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100PI), .driver_data = 5 },
128 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100D), .driver_data = 6 },
129 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100I), .driver_data = 7 },
130 	{ PCI_VDEVICE(OLICOM, PCI_DEVICE_ID_OLICOM_OC2183), .driver_data = 8 },
131 	{ PCI_VDEVICE(OLICOM, PCI_DEVICE_ID_OLICOM_OC2325), .driver_data = 9 },
132 	{ PCI_VDEVICE(OLICOM, PCI_DEVICE_ID_OLICOM_OC2326), .driver_data = 10 },
133 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_100_WS_5100), .driver_data = 11 },
134 	{ PCI_VDEVICE(COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_T2), .driver_data = 12 },
135 	{ }
136 };
137 MODULE_DEVICE_TABLE(pci, tlan_pci_tbl);
138 
139 static void	tlan_eisa_probe(void);
140 static void	tlan_eisa_cleanup(void);
141 static int      tlan_init(struct net_device *);
142 static int	tlan_open(struct net_device *dev);
143 static netdev_tx_t tlan_start_tx(struct sk_buff *, struct net_device *);
144 static irqreturn_t tlan_handle_interrupt(int, void *);
145 static int	tlan_close(struct net_device *);
146 static struct	net_device_stats *tlan_get_stats(struct net_device *);
147 static void	tlan_set_multicast_list(struct net_device *);
148 static int	tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
149 static int      tlan_probe1(struct pci_dev *pdev, long ioaddr,
150 			    int irq, int rev, const struct pci_device_id *ent);
151 static void	tlan_tx_timeout(struct net_device *dev, unsigned int txqueue);
152 static void	tlan_tx_timeout_work(struct work_struct *work);
153 static int	tlan_init_one(struct pci_dev *pdev,
154 			      const struct pci_device_id *ent);
155 
156 static u32	tlan_handle_tx_eof(struct net_device *, u16);
157 static u32	tlan_handle_stat_overflow(struct net_device *, u16);
158 static u32	tlan_handle_rx_eof(struct net_device *, u16);
159 static u32	tlan_handle_dummy(struct net_device *, u16);
160 static u32	tlan_handle_tx_eoc(struct net_device *, u16);
161 static u32	tlan_handle_status_check(struct net_device *, u16);
162 static u32	tlan_handle_rx_eoc(struct net_device *, u16);
163 
164 static void	tlan_timer(struct timer_list *t);
165 static void	tlan_phy_monitor(struct timer_list *t);
166 
167 static void	tlan_reset_lists(struct net_device *);
168 static void	tlan_free_lists(struct net_device *);
169 static void	tlan_print_dio(u16);
170 static void	tlan_print_list(struct tlan_list *, char *, int);
171 static void	tlan_read_and_clear_stats(struct net_device *, int);
172 static void	tlan_reset_adapter(struct net_device *);
173 static void	tlan_finish_reset(struct net_device *);
174 static void	tlan_set_mac(struct net_device *, int areg, const char *mac);
175 
176 static void	__tlan_phy_print(struct net_device *);
177 static void	tlan_phy_print(struct net_device *);
178 static void	tlan_phy_detect(struct net_device *);
179 static void	tlan_phy_power_down(struct net_device *);
180 static void	tlan_phy_power_up(struct net_device *);
181 static void	tlan_phy_reset(struct net_device *);
182 static void	tlan_phy_start_link(struct net_device *);
183 static void	tlan_phy_finish_auto_neg(struct net_device *);
184 
185 /*
186   static int	tlan_phy_nop(struct net_device *);
187   static int	tlan_phy_internal_check(struct net_device *);
188   static int	tlan_phy_internal_service(struct net_device *);
189   static int	tlan_phy_dp83840a_check(struct net_device *);
190 */
191 
192 static bool	__tlan_mii_read_reg(struct net_device *, u16, u16, u16 *);
193 static void	tlan_mii_read_reg(struct net_device *, u16, u16, u16 *);
194 static void	tlan_mii_send_data(u16, u32, unsigned);
195 static void	tlan_mii_sync(u16);
196 static void	__tlan_mii_write_reg(struct net_device *, u16, u16, u16);
197 static void	tlan_mii_write_reg(struct net_device *, u16, u16, u16);
198 
199 static void	tlan_ee_send_start(u16);
200 static int	tlan_ee_send_byte(u16, u8, int);
201 static void	tlan_ee_receive_byte(u16, u8 *, int);
202 static int	tlan_ee_read_byte(struct net_device *, u8, u8 *);
203 
204 
205 static inline void
206 tlan_store_skb(struct tlan_list *tag, struct sk_buff *skb)
207 {
208 	unsigned long addr = (unsigned long)skb;
209 	tag->buffer[9].address = addr;
210 	tag->buffer[8].address = upper_32_bits(addr);
211 }
212 
213 static inline struct sk_buff *
214 tlan_get_skb(const struct tlan_list *tag)
215 {
216 	unsigned long addr;
217 
218 	addr = tag->buffer[9].address;
219 	addr |= ((unsigned long) tag->buffer[8].address << 16) << 16;
220 	return (struct sk_buff *) addr;
221 }
222 
223 static u32
224 (*tlan_int_vector[TLAN_INT_NUMBER_OF_INTS])(struct net_device *, u16) = {
225 	NULL,
226 	tlan_handle_tx_eof,
227 	tlan_handle_stat_overflow,
228 	tlan_handle_rx_eof,
229 	tlan_handle_dummy,
230 	tlan_handle_tx_eoc,
231 	tlan_handle_status_check,
232 	tlan_handle_rx_eoc
233 };
234 
235 static void
236 tlan_set_timer(struct net_device *dev, u32 ticks, u32 type)
237 {
238 	struct tlan_priv *priv = netdev_priv(dev);
239 	unsigned long flags = 0;
240 
241 	spin_lock_irqsave(&priv->lock, flags);
242 	if (priv->timer.function != NULL &&
243 	    priv->timer_type != TLAN_TIMER_ACTIVITY) {
244 		spin_unlock_irqrestore(&priv->lock, flags);
245 		return;
246 	}
247 	priv->timer.function = tlan_timer;
248 	spin_unlock_irqrestore(&priv->lock, flags);
249 
250 	priv->timer_set_at = jiffies;
251 	priv->timer_type = type;
252 	mod_timer(&priv->timer, jiffies + ticks);
253 
254 }
255 
256 
257 /*****************************************************************************
258 ******************************************************************************
259 
260 ThunderLAN driver primary functions
261 
262 these functions are more or less common to all linux network drivers.
263 
264 ******************************************************************************
265 *****************************************************************************/
266 
267 
268 
269 
270 
271 /***************************************************************
272  *	tlan_remove_one
273  *
274  *	Returns:
275  *		Nothing
276  *	Parms:
277  *		None
278  *
279  *	Goes through the TLanDevices list and frees the device
280  *	structs and memory associated with each device (lists
281  *	and buffers).  It also ureserves the IO port regions
282  *	associated with this device.
283  *
284  **************************************************************/
285 
286 
287 static void tlan_remove_one(struct pci_dev *pdev)
288 {
289 	struct net_device *dev = pci_get_drvdata(pdev);
290 	struct tlan_priv	*priv = netdev_priv(dev);
291 
292 	unregister_netdev(dev);
293 
294 	if (priv->dma_storage) {
295 		dma_free_coherent(&priv->pci_dev->dev, priv->dma_size,
296 				  priv->dma_storage, priv->dma_storage_dma);
297 	}
298 
299 #ifdef CONFIG_PCI
300 	pci_release_regions(pdev);
301 #endif
302 
303 	cancel_work_sync(&priv->tlan_tqueue);
304 	free_netdev(dev);
305 }
306 
307 static void tlan_start(struct net_device *dev)
308 {
309 	tlan_reset_lists(dev);
310 	/* NOTE: It might not be necessary to read the stats before a
311 	   reset if you don't care what the values are.
312 	*/
313 	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
314 	tlan_reset_adapter(dev);
315 	netif_wake_queue(dev);
316 }
317 
318 static void tlan_stop(struct net_device *dev)
319 {
320 	struct tlan_priv *priv = netdev_priv(dev);
321 
322 	timer_delete_sync(&priv->media_timer);
323 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
324 	outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
325 	/* Reset and power down phy */
326 	tlan_reset_adapter(dev);
327 	if (priv->timer.function != NULL) {
328 		timer_delete_sync(&priv->timer);
329 		priv->timer.function = NULL;
330 	}
331 }
332 
333 static int __maybe_unused tlan_suspend(struct device *dev_d)
334 {
335 	struct net_device *dev = dev_get_drvdata(dev_d);
336 
337 	if (netif_running(dev))
338 		tlan_stop(dev);
339 
340 	netif_device_detach(dev);
341 
342 	return 0;
343 }
344 
345 static int __maybe_unused tlan_resume(struct device *dev_d)
346 {
347 	struct net_device *dev = dev_get_drvdata(dev_d);
348 	netif_device_attach(dev);
349 
350 	if (netif_running(dev))
351 		tlan_start(dev);
352 
353 	return 0;
354 }
355 
356 static SIMPLE_DEV_PM_OPS(tlan_pm_ops, tlan_suspend, tlan_resume);
357 
358 static struct pci_driver tlan_driver = {
359 	.name		= "tlan",
360 	.id_table	= tlan_pci_tbl,
361 	.probe		= tlan_init_one,
362 	.remove		= tlan_remove_one,
363 	.driver.pm	= &tlan_pm_ops,
364 };
365 
366 static int __init tlan_probe(void)
367 {
368 	int rc = -ENODEV;
369 
370 	pr_info("%s", tlan_banner);
371 
372 	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting PCI Probe....\n");
373 
374 	/* Use new style PCI probing. Now the kernel will
375 	   do most of this for us */
376 	rc = pci_register_driver(&tlan_driver);
377 
378 	if (rc != 0) {
379 		pr_err("Could not register pci driver\n");
380 		goto err_out_pci_free;
381 	}
382 
383 	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting EISA Probe....\n");
384 	tlan_eisa_probe();
385 
386 	pr_info("%d device%s installed, PCI: %d  EISA: %d\n",
387 		tlan_devices_installed, tlan_devices_installed == 1 ? "" : "s",
388 		tlan_have_pci, tlan_have_eisa);
389 
390 	if (tlan_devices_installed == 0) {
391 		rc = -ENODEV;
392 		goto  err_out_pci_unreg;
393 	}
394 	return 0;
395 
396 err_out_pci_unreg:
397 	pci_unregister_driver(&tlan_driver);
398 err_out_pci_free:
399 	return rc;
400 }
401 
402 
403 static int tlan_init_one(struct pci_dev *pdev,
404 				   const struct pci_device_id *ent)
405 {
406 	return tlan_probe1(pdev, -1, -1, 0, ent);
407 }
408 
409 
410 /*
411 ***************************************************************
412 *	tlan_probe1
413 *
414 *	Returns:
415 *		0 on success, error code on error
416 *	Parms:
417 *		none
418 *
419 *	The name is lower case to fit in with all the rest of
420 *	the netcard_probe names.  This function looks for
421 *	another TLan based adapter, setting it up with the
422 *	allocated device struct if one is found.
423 *	tlan_probe has been ported to the new net API and
424 *	now allocates its own device structure. This function
425 *	is also used by modules.
426 *
427 **************************************************************/
428 
429 static int tlan_probe1(struct pci_dev *pdev, long ioaddr, int irq, int rev,
430 		       const struct pci_device_id *ent)
431 {
432 
433 	struct net_device  *dev;
434 	struct tlan_priv  *priv;
435 	u16		   device_id;
436 	int		   reg, rc = -ENODEV;
437 
438 #ifdef CONFIG_PCI
439 	if (pdev) {
440 		rc = pci_enable_device(pdev);
441 		if (rc)
442 			return rc;
443 
444 		rc = pci_request_regions(pdev, tlan_signature);
445 		if (rc) {
446 			pr_err("Could not reserve IO regions\n");
447 			goto err_out;
448 		}
449 	}
450 #endif  /*  CONFIG_PCI  */
451 
452 	dev = alloc_etherdev(sizeof(struct tlan_priv));
453 	if (dev == NULL) {
454 		rc = -ENOMEM;
455 		goto err_out_regions;
456 	}
457 	SET_NETDEV_DEV(dev, &pdev->dev);
458 
459 	priv = netdev_priv(dev);
460 
461 	priv->pci_dev = pdev;
462 	priv->dev = dev;
463 
464 	/* Is this a PCI device? */
465 	if (pdev) {
466 		u32		   pci_io_base = 0;
467 
468 		priv->adapter = &board_info[ent->driver_data];
469 
470 		rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
471 		if (rc) {
472 			pr_err("No suitable PCI mapping available\n");
473 			goto err_out_free_dev;
474 		}
475 
476 		for (reg = 0; reg <= 5; reg++) {
477 			if (pci_resource_flags(pdev, reg) & IORESOURCE_IO) {
478 				pci_io_base = pci_resource_start(pdev, reg);
479 				TLAN_DBG(TLAN_DEBUG_GNRL,
480 					 "IO mapping is available at %x.\n",
481 					 pci_io_base);
482 				break;
483 			}
484 		}
485 		if (!pci_io_base) {
486 			pr_err("No IO mappings available\n");
487 			rc = -EIO;
488 			goto err_out_free_dev;
489 		}
490 
491 		dev->base_addr = pci_io_base;
492 		dev->irq = pdev->irq;
493 		priv->adapter_rev = pdev->revision;
494 		pci_set_master(pdev);
495 		pci_set_drvdata(pdev, dev);
496 
497 	} else	{     /* EISA card */
498 		/* This is a hack. We need to know which board structure
499 		 * is suited for this adapter */
500 		device_id = inw(ioaddr + EISA_ID2);
501 		if (device_id == 0x20F1) {
502 			priv->adapter = &board_info[13]; /* NetFlex-3/E */
503 			priv->adapter_rev = 23;		/* TLAN 2.3 */
504 		} else {
505 			priv->adapter = &board_info[14];
506 			priv->adapter_rev = 10;		/* TLAN 1.0 */
507 		}
508 		dev->base_addr = ioaddr;
509 		dev->irq = irq;
510 	}
511 
512 	/* Kernel parameters */
513 	if (dev->mem_start) {
514 		priv->aui    = dev->mem_start & 0x01;
515 		priv->duplex = ((dev->mem_start & 0x06) == 0x06) ? 0
516 			: (dev->mem_start & 0x06) >> 1;
517 		priv->speed  = ((dev->mem_start & 0x18) == 0x18) ? 0
518 			: (dev->mem_start & 0x18) >> 3;
519 
520 		if (priv->speed == 0x1)
521 			priv->speed = TLAN_SPEED_10;
522 		else if (priv->speed == 0x2)
523 			priv->speed = TLAN_SPEED_100;
524 
525 		debug = priv->debug = dev->mem_end;
526 	} else {
527 		priv->aui    = aui[boards_found];
528 		priv->speed  = speed[boards_found];
529 		priv->duplex = duplex[boards_found];
530 		priv->debug = debug;
531 	}
532 
533 	/* This will be used when we get an adapter error from
534 	 * within our irq handler */
535 	INIT_WORK(&priv->tlan_tqueue, tlan_tx_timeout_work);
536 
537 	spin_lock_init(&priv->lock);
538 
539 	rc = tlan_init(dev);
540 	if (rc) {
541 		pr_err("Could not set up device\n");
542 		goto err_out_free_dev;
543 	}
544 
545 	rc = register_netdev(dev);
546 	if (rc) {
547 		pr_err("Could not register device\n");
548 		goto err_out_uninit;
549 	}
550 
551 
552 	tlan_devices_installed++;
553 	boards_found++;
554 
555 	/* pdev is NULL if this is an EISA device */
556 	if (pdev)
557 		tlan_have_pci++;
558 	else {
559 		priv->next_device = tlan_eisa_devices;
560 		tlan_eisa_devices = dev;
561 		tlan_have_eisa++;
562 	}
563 
564 	netdev_info(dev, "irq=%2d, io=%04x, %s, Rev. %d\n",
565 		    (int)dev->irq,
566 		    (int)dev->base_addr,
567 		    priv->adapter->device_label,
568 		    priv->adapter_rev);
569 	return 0;
570 
571 err_out_uninit:
572 	dma_free_coherent(&priv->pci_dev->dev, priv->dma_size,
573 			  priv->dma_storage, priv->dma_storage_dma);
574 err_out_free_dev:
575 	free_netdev(dev);
576 err_out_regions:
577 #ifdef CONFIG_PCI
578 	if (pdev)
579 		pci_release_regions(pdev);
580 err_out:
581 #endif
582 	if (pdev)
583 		pci_disable_device(pdev);
584 	return rc;
585 }
586 
587 
588 static void tlan_eisa_cleanup(void)
589 {
590 	struct net_device *dev;
591 	struct tlan_priv *priv;
592 
593 	while (tlan_have_eisa) {
594 		dev = tlan_eisa_devices;
595 		priv = netdev_priv(dev);
596 		if (priv->dma_storage) {
597 			dma_free_coherent(&priv->pci_dev->dev, priv->dma_size,
598 					  priv->dma_storage,
599 					  priv->dma_storage_dma);
600 		}
601 		release_region(dev->base_addr, 0x10);
602 		unregister_netdev(dev);
603 		tlan_eisa_devices = priv->next_device;
604 		free_netdev(dev);
605 		tlan_have_eisa--;
606 	}
607 }
608 
609 
610 static void __exit tlan_exit(void)
611 {
612 	pci_unregister_driver(&tlan_driver);
613 
614 	if (tlan_have_eisa)
615 		tlan_eisa_cleanup();
616 
617 }
618 
619 
620 /* Module loading/unloading */
621 module_init(tlan_probe);
622 module_exit(tlan_exit);
623 
624 
625 
626 /**************************************************************
627  *	tlan_eisa_probe
628  *
629  *	Returns: 0 on success, 1 otherwise
630  *
631  *	Parms:	 None
632  *
633  *
634  *	This functions probes for EISA devices and calls
635  *	TLan_probe1 when one is found.
636  *
637  *************************************************************/
638 
639 static void  __init tlan_eisa_probe(void)
640 {
641 	long	ioaddr;
642 	int	irq;
643 	u16	device_id;
644 
645 	if (!EISA_bus) {
646 		TLAN_DBG(TLAN_DEBUG_PROBE, "No EISA bus present\n");
647 		return;
648 	}
649 
650 	/* Loop through all slots of the EISA bus */
651 	for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
652 
653 		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
654 			 (int) ioaddr + 0xc80, inw(ioaddr + EISA_ID));
655 		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
656 			 (int) ioaddr + 0xc82, inw(ioaddr + EISA_ID2));
657 
658 
659 		TLAN_DBG(TLAN_DEBUG_PROBE,
660 			 "Probing for EISA adapter at IO: 0x%4x : ",
661 			 (int) ioaddr);
662 		if (request_region(ioaddr, 0x10, tlan_signature) == NULL)
663 			goto out;
664 
665 		if (inw(ioaddr + EISA_ID) != 0x110E) {
666 			release_region(ioaddr, 0x10);
667 			goto out;
668 		}
669 
670 		device_id = inw(ioaddr + EISA_ID2);
671 		if (device_id !=  0x20F1 && device_id != 0x40F1) {
672 			release_region(ioaddr, 0x10);
673 			goto out;
674 		}
675 
676 		/* check if adapter is enabled */
677 		if (inb(ioaddr + EISA_CR) != 0x1) {
678 			release_region(ioaddr, 0x10);
679 			goto out2;
680 		}
681 
682 		if (debug == 0x10)
683 			pr_info("Found one\n");
684 
685 
686 		/* Get irq from board */
687 		switch (inb(ioaddr + 0xcc0)) {
688 		case(0x10):
689 			irq = 5;
690 			break;
691 		case(0x20):
692 			irq = 9;
693 			break;
694 		case(0x40):
695 			irq = 10;
696 			break;
697 		case(0x80):
698 			irq = 11;
699 			break;
700 		default:
701 			goto out;
702 		}
703 
704 
705 		/* Setup the newly found eisa adapter */
706 		tlan_probe1(NULL, ioaddr, irq, 12, NULL);
707 		continue;
708 
709 out:
710 		if (debug == 0x10)
711 			pr_info("None found\n");
712 		continue;
713 
714 out2:
715 		if (debug == 0x10)
716 			pr_info("Card found but it is not enabled, skipping\n");
717 		continue;
718 
719 	}
720 
721 }
722 
723 #ifdef CONFIG_NET_POLL_CONTROLLER
724 static void tlan_poll(struct net_device *dev)
725 {
726 	disable_irq(dev->irq);
727 	tlan_handle_interrupt(dev->irq, dev);
728 	enable_irq(dev->irq);
729 }
730 #endif
731 
732 static const struct net_device_ops tlan_netdev_ops = {
733 	.ndo_open		= tlan_open,
734 	.ndo_stop		= tlan_close,
735 	.ndo_start_xmit		= tlan_start_tx,
736 	.ndo_tx_timeout		= tlan_tx_timeout,
737 	.ndo_get_stats		= tlan_get_stats,
738 	.ndo_set_rx_mode	= tlan_set_multicast_list,
739 	.ndo_eth_ioctl		= tlan_ioctl,
740 	.ndo_set_mac_address	= eth_mac_addr,
741 	.ndo_validate_addr	= eth_validate_addr,
742 #ifdef CONFIG_NET_POLL_CONTROLLER
743 	.ndo_poll_controller	 = tlan_poll,
744 #endif
745 };
746 
747 static void tlan_get_drvinfo(struct net_device *dev,
748 			     struct ethtool_drvinfo *info)
749 {
750 	struct tlan_priv *priv = netdev_priv(dev);
751 
752 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
753 	if (priv->pci_dev)
754 		strscpy(info->bus_info, pci_name(priv->pci_dev),
755 			sizeof(info->bus_info));
756 	else
757 		strscpy(info->bus_info, "EISA",	sizeof(info->bus_info));
758 }
759 
760 static int tlan_get_eeprom_len(struct net_device *dev)
761 {
762 	return TLAN_EEPROM_SIZE;
763 }
764 
765 static int tlan_get_eeprom(struct net_device *dev,
766 			   struct ethtool_eeprom *eeprom, u8 *data)
767 {
768 	int i;
769 
770 	for (i = 0; i < TLAN_EEPROM_SIZE; i++)
771 		if (tlan_ee_read_byte(dev, i, &data[i]))
772 			return -EIO;
773 
774 	return 0;
775 }
776 
777 static const struct ethtool_ops tlan_ethtool_ops = {
778 	.get_drvinfo	= tlan_get_drvinfo,
779 	.get_link	= ethtool_op_get_link,
780 	.get_eeprom_len	= tlan_get_eeprom_len,
781 	.get_eeprom	= tlan_get_eeprom,
782 };
783 
784 /***************************************************************
785  *	tlan_init
786  *
787  *	Returns:
788  *		0 on success, error code otherwise.
789  *	Parms:
790  *		dev	The structure of the device to be
791  *			init'ed.
792  *
793  *	This function completes the initialization of the
794  *	device structure and driver.  It reserves the IO
795  *	addresses, allocates memory for the lists and bounce
796  *	buffers, retrieves the MAC address from the eeprom
797  *	and assignes the device's methods.
798  *
799  **************************************************************/
800 
801 static int tlan_init(struct net_device *dev)
802 {
803 	int		dma_size;
804 	int		err;
805 	int		i;
806 	struct tlan_priv	*priv;
807 	u8 addr[ETH_ALEN];
808 
809 	priv = netdev_priv(dev);
810 
811 	dma_size = (TLAN_NUM_RX_LISTS + TLAN_NUM_TX_LISTS)
812 		* (sizeof(struct tlan_list));
813 	priv->dma_storage = dma_alloc_coherent(&priv->pci_dev->dev, dma_size,
814 					       &priv->dma_storage_dma, GFP_KERNEL);
815 	priv->dma_size = dma_size;
816 
817 	if (priv->dma_storage == NULL) {
818 		pr_err("Could not allocate lists and buffers for %s\n",
819 		       dev->name);
820 		return -ENOMEM;
821 	}
822 	priv->rx_list = (struct tlan_list *)
823 		ALIGN((unsigned long)priv->dma_storage, 8);
824 	priv->rx_list_dma = ALIGN(priv->dma_storage_dma, 8);
825 	priv->tx_list = priv->rx_list + TLAN_NUM_RX_LISTS;
826 	priv->tx_list_dma =
827 		priv->rx_list_dma + sizeof(struct tlan_list)*TLAN_NUM_RX_LISTS;
828 
829 	err = 0;
830 	for (i = 0; i < ETH_ALEN; i++)
831 		err |= tlan_ee_read_byte(dev,
832 					 (u8) priv->adapter->addr_ofs + i,
833 					 addr + i);
834 	if (err) {
835 		pr_err("%s: Error reading MAC from eeprom: %d\n",
836 		       dev->name, err);
837 	}
838 	/* Olicom OC-2325/OC-2326 have the address byte-swapped */
839 	if (priv->adapter->addr_ofs == 0xf8) {
840 		for (i = 0; i < ETH_ALEN; i += 2) {
841 			char tmp = addr[i];
842 			addr[i] = addr[i + 1];
843 			addr[i + 1] = tmp;
844 		}
845 	}
846 	eth_hw_addr_set(dev, addr);
847 
848 	netif_carrier_off(dev);
849 
850 	/* Device methods */
851 	dev->netdev_ops = &tlan_netdev_ops;
852 	dev->ethtool_ops = &tlan_ethtool_ops;
853 	dev->watchdog_timeo = TX_TIMEOUT;
854 
855 	return 0;
856 
857 }
858 
859 
860 
861 
862 /***************************************************************
863  *	tlan_open
864  *
865  *	Returns:
866  *		0 on success, error code otherwise.
867  *	Parms:
868  *		dev	Structure of device to be opened.
869  *
870  *	This routine puts the driver and TLAN adapter in a
871  *	state where it is ready to send and receive packets.
872  *	It allocates the IRQ, resets and brings the adapter
873  *	out of reset, and allows interrupts.  It also delays
874  *	the startup for autonegotiation or sends a Rx GO
875  *	command to the adapter, as appropriate.
876  *
877  **************************************************************/
878 
879 static int tlan_open(struct net_device *dev)
880 {
881 	struct tlan_priv	*priv = netdev_priv(dev);
882 	int		err;
883 
884 	priv->tlan_rev = tlan_dio_read8(dev->base_addr, TLAN_DEF_REVISION);
885 	err = request_irq(dev->irq, tlan_handle_interrupt, IRQF_SHARED,
886 			  dev->name, dev);
887 
888 	if (err) {
889 		netdev_err(dev, "Cannot open because IRQ %d is already in use\n",
890 			   dev->irq);
891 		return err;
892 	}
893 
894 	timer_setup(&priv->timer, NULL, 0);
895 	timer_setup(&priv->media_timer, tlan_phy_monitor, 0);
896 
897 	tlan_start(dev);
898 
899 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Opened.  TLAN Chip Rev: %x\n",
900 		 dev->name, priv->tlan_rev);
901 
902 	return 0;
903 
904 }
905 
906 
907 
908 /**************************************************************
909  *	tlan_ioctl
910  *
911  *	Returns:
912  *		0 on success, error code otherwise
913  *	Params:
914  *		dev	structure of device to receive ioctl.
915  *
916  *		rq	ifreq structure to hold userspace data.
917  *
918  *		cmd	ioctl command.
919  *
920  *
921  *************************************************************/
922 
923 static int tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
924 {
925 	struct tlan_priv *priv = netdev_priv(dev);
926 	struct mii_ioctl_data *data = if_mii(rq);
927 	u32 phy   = priv->phy[priv->phy_num];
928 
929 	if (!priv->phy_online)
930 		return -EAGAIN;
931 
932 	switch (cmd) {
933 	case SIOCGMIIPHY:		/* get address of MII PHY in use. */
934 		data->phy_id = phy;
935 		fallthrough;
936 
937 
938 	case SIOCGMIIREG:		/* read MII PHY register. */
939 		tlan_mii_read_reg(dev, data->phy_id & 0x1f,
940 				  data->reg_num & 0x1f, &data->val_out);
941 		return 0;
942 
943 
944 	case SIOCSMIIREG:		/* write MII PHY register. */
945 		tlan_mii_write_reg(dev, data->phy_id & 0x1f,
946 				   data->reg_num & 0x1f, data->val_in);
947 		return 0;
948 	default:
949 		return -EOPNOTSUPP;
950 	}
951 }
952 
953 
954 /***************************************************************
955  *	tlan_tx_timeout
956  *
957  *	Returns: nothing
958  *
959  *	Params:
960  *		dev	structure of device which timed out
961  *			during transmit.
962  *
963  **************************************************************/
964 
965 static void tlan_tx_timeout(struct net_device *dev, unsigned int txqueue)
966 {
967 
968 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Transmit timed out.\n", dev->name);
969 
970 	/* Ok so we timed out, lets see what we can do about it...*/
971 	tlan_free_lists(dev);
972 	tlan_reset_lists(dev);
973 	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
974 	tlan_reset_adapter(dev);
975 	netif_trans_update(dev); /* prevent tx timeout */
976 	netif_wake_queue(dev);
977 
978 }
979 
980 
981 /***************************************************************
982  *	tlan_tx_timeout_work
983  *
984  *	Returns: nothing
985  *
986  *	Params:
987  *		work	work item of device which timed out
988  *
989  **************************************************************/
990 
991 static void tlan_tx_timeout_work(struct work_struct *work)
992 {
993 	struct tlan_priv	*priv =
994 		container_of(work, struct tlan_priv, tlan_tqueue);
995 
996 	tlan_tx_timeout(priv->dev, UINT_MAX);
997 }
998 
999 
1000 
1001 /***************************************************************
1002  *	tlan_start_tx
1003  *
1004  *	Returns:
1005  *		0 on success, non-zero on failure.
1006  *	Parms:
1007  *		skb	A pointer to the sk_buff containing the
1008  *			frame to be sent.
1009  *		dev	The device to send the data on.
1010  *
1011  *	This function adds a frame to the Tx list to be sent
1012  *	ASAP.  First it	verifies that the adapter is ready and
1013  *	there is room in the queue.  Then it sets up the next
1014  *	available list, copies the frame to the	corresponding
1015  *	buffer.  If the adapter Tx channel is idle, it gives
1016  *	the adapter a Tx Go command on the list, otherwise it
1017  *	sets the forward address of the previous list to point
1018  *	to this one.  Then it frees the sk_buff.
1019  *
1020  **************************************************************/
1021 
1022 static netdev_tx_t tlan_start_tx(struct sk_buff *skb, struct net_device *dev)
1023 {
1024 	struct tlan_priv *priv = netdev_priv(dev);
1025 	dma_addr_t	tail_list_phys;
1026 	struct tlan_list	*tail_list;
1027 	unsigned long	flags;
1028 	unsigned int    txlen;
1029 
1030 	if (!priv->phy_online) {
1031 		TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s PHY is not ready\n",
1032 			 dev->name);
1033 		dev_kfree_skb_any(skb);
1034 		return NETDEV_TX_OK;
1035 	}
1036 
1037 	if (skb_padto(skb, TLAN_MIN_FRAME_SIZE))
1038 		return NETDEV_TX_OK;
1039 	txlen = max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE);
1040 
1041 	tail_list = priv->tx_list + priv->tx_tail;
1042 	tail_list_phys =
1043 		priv->tx_list_dma + sizeof(struct tlan_list)*priv->tx_tail;
1044 
1045 	if (tail_list->c_stat != TLAN_CSTAT_UNUSED) {
1046 		TLAN_DBG(TLAN_DEBUG_TX,
1047 			 "TRANSMIT:  %s is busy (Head=%d Tail=%d)\n",
1048 			 dev->name, priv->tx_head, priv->tx_tail);
1049 		netif_stop_queue(dev);
1050 		priv->tx_busy_count++;
1051 		return NETDEV_TX_BUSY;
1052 	}
1053 
1054 	tail_list->forward = 0;
1055 
1056 	tail_list->buffer[0].address = dma_map_single(&priv->pci_dev->dev,
1057 						      skb->data, txlen,
1058 						      DMA_TO_DEVICE);
1059 	tlan_store_skb(tail_list, skb);
1060 
1061 	tail_list->frame_size = (u16) txlen;
1062 	tail_list->buffer[0].count = TLAN_LAST_BUFFER | (u32) txlen;
1063 	tail_list->buffer[1].count = 0;
1064 	tail_list->buffer[1].address = 0;
1065 
1066 	spin_lock_irqsave(&priv->lock, flags);
1067 	tail_list->c_stat = TLAN_CSTAT_READY;
1068 	if (!priv->tx_in_progress) {
1069 		priv->tx_in_progress = 1;
1070 		TLAN_DBG(TLAN_DEBUG_TX,
1071 			 "TRANSMIT:  Starting TX on buffer %d\n",
1072 			 priv->tx_tail);
1073 		outl(tail_list_phys, dev->base_addr + TLAN_CH_PARM);
1074 		outl(TLAN_HC_GO, dev->base_addr + TLAN_HOST_CMD);
1075 	} else {
1076 		TLAN_DBG(TLAN_DEBUG_TX,
1077 			 "TRANSMIT:  Adding buffer %d to TX channel\n",
1078 			 priv->tx_tail);
1079 		if (priv->tx_tail == 0) {
1080 			(priv->tx_list + (TLAN_NUM_TX_LISTS - 1))->forward
1081 				= tail_list_phys;
1082 		} else {
1083 			(priv->tx_list + (priv->tx_tail - 1))->forward
1084 				= tail_list_phys;
1085 		}
1086 	}
1087 	spin_unlock_irqrestore(&priv->lock, flags);
1088 
1089 	CIRC_INC(priv->tx_tail, TLAN_NUM_TX_LISTS);
1090 
1091 	return NETDEV_TX_OK;
1092 
1093 }
1094 
1095 
1096 
1097 
1098 /***************************************************************
1099  *	tlan_handle_interrupt
1100  *
1101  *	Returns:
1102  *		Nothing
1103  *	Parms:
1104  *		irq	The line on which the interrupt
1105  *			occurred.
1106  *		dev_id	A pointer to the device assigned to
1107  *			this irq line.
1108  *
1109  *	This function handles an interrupt generated by its
1110  *	assigned TLAN adapter.  The function deactivates
1111  *	interrupts on its adapter, records the type of
1112  *	interrupt, executes the appropriate subhandler, and
1113  *	acknowdges the interrupt to the adapter (thus
1114  *	re-enabling adapter interrupts.
1115  *
1116  **************************************************************/
1117 
1118 static irqreturn_t tlan_handle_interrupt(int irq, void *dev_id)
1119 {
1120 	struct net_device	*dev = dev_id;
1121 	struct tlan_priv *priv = netdev_priv(dev);
1122 	u16		host_int;
1123 	u16		type;
1124 
1125 	spin_lock(&priv->lock);
1126 
1127 	host_int = inw(dev->base_addr + TLAN_HOST_INT);
1128 	type = (host_int & TLAN_HI_IT_MASK) >> 2;
1129 	if (type) {
1130 		u32	ack;
1131 		u32	host_cmd;
1132 
1133 		outw(host_int, dev->base_addr + TLAN_HOST_INT);
1134 		ack = tlan_int_vector[type](dev, host_int);
1135 
1136 		if (ack) {
1137 			host_cmd = TLAN_HC_ACK | ack | (type << 18);
1138 			outl(host_cmd, dev->base_addr + TLAN_HOST_CMD);
1139 		}
1140 	}
1141 
1142 	spin_unlock(&priv->lock);
1143 
1144 	return IRQ_RETVAL(type);
1145 }
1146 
1147 
1148 
1149 
1150 /***************************************************************
1151  *	tlan_close
1152  *
1153  *	Returns:
1154  *		An error code.
1155  *	Parms:
1156  *		dev	The device structure of the device to
1157  *			close.
1158  *
1159  *	This function shuts down the adapter.  It records any
1160  *	stats, puts the adapter into reset state, deactivates
1161  *	its time as needed, and	frees the irq it is using.
1162  *
1163  **************************************************************/
1164 
1165 static int tlan_close(struct net_device *dev)
1166 {
1167 	tlan_stop(dev);
1168 
1169 	free_irq(dev->irq, dev);
1170 	tlan_free_lists(dev);
1171 	TLAN_DBG(TLAN_DEBUG_GNRL, "Device %s closed.\n", dev->name);
1172 
1173 	return 0;
1174 
1175 }
1176 
1177 
1178 
1179 
1180 /***************************************************************
1181  *	tlan_get_stats
1182  *
1183  *	Returns:
1184  *		A pointer to the device's statistics structure.
1185  *	Parms:
1186  *		dev	The device structure to return the
1187  *			stats for.
1188  *
1189  *	This function updates the devices statistics by reading
1190  *	the TLAN chip's onboard registers.  Then it returns the
1191  *	address of the statistics structure.
1192  *
1193  **************************************************************/
1194 
1195 static struct net_device_stats *tlan_get_stats(struct net_device *dev)
1196 {
1197 	struct tlan_priv	*priv = netdev_priv(dev);
1198 	int i;
1199 
1200 	/* Should only read stats if open ? */
1201 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
1202 
1203 	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  %s EOC count = %d\n", dev->name,
1204 		 priv->rx_eoc_count);
1205 	TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s Busy count = %d\n", dev->name,
1206 		 priv->tx_busy_count);
1207 	if (debug & TLAN_DEBUG_GNRL) {
1208 		tlan_print_dio(dev->base_addr);
1209 		tlan_phy_print(dev);
1210 	}
1211 	if (debug & TLAN_DEBUG_LIST) {
1212 		for (i = 0; i < TLAN_NUM_RX_LISTS; i++)
1213 			tlan_print_list(priv->rx_list + i, "RX", i);
1214 		for (i = 0; i < TLAN_NUM_TX_LISTS; i++)
1215 			tlan_print_list(priv->tx_list + i, "TX", i);
1216 	}
1217 
1218 	return &dev->stats;
1219 
1220 }
1221 
1222 
1223 
1224 
1225 /***************************************************************
1226  *	tlan_set_multicast_list
1227  *
1228  *	Returns:
1229  *		Nothing
1230  *	Parms:
1231  *		dev	The device structure to set the
1232  *			multicast list for.
1233  *
1234  *	This function sets the TLAN adaptor to various receive
1235  *	modes.  If the IFF_PROMISC flag is set, promiscuous
1236  *	mode is acitviated.  Otherwise,	promiscuous mode is
1237  *	turned off.  If the IFF_ALLMULTI flag is set, then
1238  *	the hash table is set to receive all group addresses.
1239  *	Otherwise, the first three multicast addresses are
1240  *	stored in AREG_1-3, and the rest are selected via the
1241  *	hash table, as necessary.
1242  *
1243  **************************************************************/
1244 
1245 static void tlan_set_multicast_list(struct net_device *dev)
1246 {
1247 	struct netdev_hw_addr *ha;
1248 	u32			hash1 = 0;
1249 	u32			hash2 = 0;
1250 	int			i;
1251 	u32			offset;
1252 	u8			tmp;
1253 
1254 	if (dev->flags & IFF_PROMISC) {
1255 		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
1256 		tlan_dio_write8(dev->base_addr,
1257 				TLAN_NET_CMD, tmp | TLAN_NET_CMD_CAF);
1258 	} else {
1259 		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
1260 		tlan_dio_write8(dev->base_addr,
1261 				TLAN_NET_CMD, tmp & ~TLAN_NET_CMD_CAF);
1262 		if (dev->flags & IFF_ALLMULTI) {
1263 			for (i = 0; i < 3; i++)
1264 				tlan_set_mac(dev, i + 1, NULL);
1265 			tlan_dio_write32(dev->base_addr, TLAN_HASH_1,
1266 					 0xffffffff);
1267 			tlan_dio_write32(dev->base_addr, TLAN_HASH_2,
1268 					 0xffffffff);
1269 		} else {
1270 			i = 0;
1271 			netdev_for_each_mc_addr(ha, dev) {
1272 				if (i < 3) {
1273 					tlan_set_mac(dev, i + 1,
1274 						     (char *) &ha->addr);
1275 				} else {
1276 					offset =
1277 						tlan_hash_func((u8 *)&ha->addr);
1278 					if (offset < 32)
1279 						hash1 |= (1 << offset);
1280 					else
1281 						hash2 |= (1 << (offset - 32));
1282 				}
1283 				i++;
1284 			}
1285 			for ( ; i < 3; i++)
1286 				tlan_set_mac(dev, i + 1, NULL);
1287 			tlan_dio_write32(dev->base_addr, TLAN_HASH_1, hash1);
1288 			tlan_dio_write32(dev->base_addr, TLAN_HASH_2, hash2);
1289 		}
1290 	}
1291 
1292 }
1293 
1294 
1295 
1296 /*****************************************************************************
1297 ******************************************************************************
1298 
1299 ThunderLAN driver interrupt vectors and table
1300 
1301 please see chap. 4, "Interrupt Handling" of the "ThunderLAN
1302 Programmer's Guide" for more informations on handling interrupts
1303 generated by TLAN based adapters.
1304 
1305 ******************************************************************************
1306 *****************************************************************************/
1307 
1308 
1309 
1310 
1311 /***************************************************************
1312  *	tlan_handle_tx_eof
1313  *
1314  *	Returns:
1315  *		1
1316  *	Parms:
1317  *		dev		Device assigned the IRQ that was
1318  *				raised.
1319  *		host_int	The contents of the HOST_INT
1320  *				port.
1321  *
1322  *	This function handles Tx EOF interrupts which are raised
1323  *	by the adapter when it has completed sending the
1324  *	contents of a buffer.  If detemines which list/buffer
1325  *	was completed and resets it.  If the buffer was the last
1326  *	in the channel (EOC), then the function checks to see if
1327  *	another buffer is ready to send, and if so, sends a Tx
1328  *	Go command.  Finally, the driver activates/continues the
1329  *	activity LED.
1330  *
1331  **************************************************************/
1332 
1333 static u32 tlan_handle_tx_eof(struct net_device *dev, u16 host_int)
1334 {
1335 	struct tlan_priv	*priv = netdev_priv(dev);
1336 	int		eoc = 0;
1337 	struct tlan_list	*head_list;
1338 	dma_addr_t	head_list_phys;
1339 	u32		ack = 0;
1340 	u16		tmp_c_stat;
1341 
1342 	TLAN_DBG(TLAN_DEBUG_TX,
1343 		 "TRANSMIT:  Handling TX EOF (Head=%d Tail=%d)\n",
1344 		 priv->tx_head, priv->tx_tail);
1345 	head_list = priv->tx_list + priv->tx_head;
1346 
1347 	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
1348 	       && (ack < 255)) {
1349 		struct sk_buff *skb = tlan_get_skb(head_list);
1350 
1351 		ack++;
1352 		dma_unmap_single(&priv->pci_dev->dev,
1353 				 head_list->buffer[0].address,
1354 				 max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE),
1355 				 DMA_TO_DEVICE);
1356 		dev_kfree_skb_any(skb);
1357 		head_list->buffer[8].address = 0;
1358 		head_list->buffer[9].address = 0;
1359 
1360 		if (tmp_c_stat & TLAN_CSTAT_EOC)
1361 			eoc = 1;
1362 
1363 		dev->stats.tx_bytes += head_list->frame_size;
1364 
1365 		head_list->c_stat = TLAN_CSTAT_UNUSED;
1366 		netif_start_queue(dev);
1367 		CIRC_INC(priv->tx_head, TLAN_NUM_TX_LISTS);
1368 		head_list = priv->tx_list + priv->tx_head;
1369 	}
1370 
1371 	if (!ack)
1372 		netdev_info(dev,
1373 			    "Received interrupt for uncompleted TX frame\n");
1374 
1375 	if (eoc) {
1376 		TLAN_DBG(TLAN_DEBUG_TX,
1377 			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d)\n",
1378 			 priv->tx_head, priv->tx_tail);
1379 		head_list = priv->tx_list + priv->tx_head;
1380 		head_list_phys = priv->tx_list_dma
1381 			+ sizeof(struct tlan_list)*priv->tx_head;
1382 		if ((head_list->c_stat & TLAN_CSTAT_READY)
1383 		    == TLAN_CSTAT_READY) {
1384 			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1385 			ack |= TLAN_HC_GO;
1386 		} else {
1387 			priv->tx_in_progress = 0;
1388 		}
1389 	}
1390 
1391 	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
1392 		tlan_dio_write8(dev->base_addr,
1393 				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
1394 		if (priv->timer.function == NULL) {
1395 			priv->timer.function = tlan_timer;
1396 			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
1397 			priv->timer_set_at = jiffies;
1398 			priv->timer_type = TLAN_TIMER_ACTIVITY;
1399 			add_timer(&priv->timer);
1400 		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
1401 			priv->timer_set_at = jiffies;
1402 		}
1403 	}
1404 
1405 	return ack;
1406 
1407 }
1408 
1409 
1410 
1411 
1412 /***************************************************************
1413  *	TLan_HandleStatOverflow
1414  *
1415  *	Returns:
1416  *		1
1417  *	Parms:
1418  *		dev		Device assigned the IRQ that was
1419  *				raised.
1420  *		host_int	The contents of the HOST_INT
1421  *				port.
1422  *
1423  *	This function handles the Statistics Overflow interrupt
1424  *	which means that one or more of the TLAN statistics
1425  *	registers has reached 1/2 capacity and needs to be read.
1426  *
1427  **************************************************************/
1428 
1429 static u32 tlan_handle_stat_overflow(struct net_device *dev, u16 host_int)
1430 {
1431 	tlan_read_and_clear_stats(dev, TLAN_RECORD);
1432 
1433 	return 1;
1434 
1435 }
1436 
1437 
1438 
1439 
1440 /***************************************************************
1441  *	TLan_HandleRxEOF
1442  *
1443  *	Returns:
1444  *		1
1445  *	Parms:
1446  *		dev		Device assigned the IRQ that was
1447  *				raised.
1448  *		host_int	The contents of the HOST_INT
1449  *				port.
1450  *
1451  *	This function handles the Rx EOF interrupt which
1452  *	indicates a frame has been received by the adapter from
1453  *	the net and the frame has been transferred to memory.
1454  *	The function determines the bounce buffer the frame has
1455  *	been loaded into, creates a new sk_buff big enough to
1456  *	hold the frame, and sends it to protocol stack.  It
1457  *	then resets the used buffer and appends it to the end
1458  *	of the list.  If the frame was the last in the Rx
1459  *	channel (EOC), the function restarts the receive channel
1460  *	by sending an Rx Go command to the adapter.  Then it
1461  *	activates/continues the activity LED.
1462  *
1463  **************************************************************/
1464 
1465 static u32 tlan_handle_rx_eof(struct net_device *dev, u16 host_int)
1466 {
1467 	struct tlan_priv	*priv = netdev_priv(dev);
1468 	u32		ack = 0;
1469 	int		eoc = 0;
1470 	struct tlan_list	*head_list;
1471 	struct sk_buff	*skb;
1472 	struct tlan_list	*tail_list;
1473 	u16		tmp_c_stat;
1474 	dma_addr_t	head_list_phys;
1475 
1476 	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  handling RX EOF (Head=%d Tail=%d)\n",
1477 		 priv->rx_head, priv->rx_tail);
1478 	head_list = priv->rx_list + priv->rx_head;
1479 	head_list_phys =
1480 		priv->rx_list_dma + sizeof(struct tlan_list)*priv->rx_head;
1481 
1482 	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
1483 	       && (ack < 255)) {
1484 		dma_addr_t frame_dma = head_list->buffer[0].address;
1485 		u32 frame_size = head_list->frame_size;
1486 		struct sk_buff *new_skb;
1487 
1488 		ack++;
1489 		if (tmp_c_stat & TLAN_CSTAT_EOC)
1490 			eoc = 1;
1491 
1492 		new_skb = netdev_alloc_skb_ip_align(dev,
1493 						    TLAN_MAX_FRAME_SIZE + 5);
1494 		if (!new_skb)
1495 			goto drop_and_reuse;
1496 
1497 		skb = tlan_get_skb(head_list);
1498 		dma_unmap_single(&priv->pci_dev->dev, frame_dma,
1499 				 TLAN_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
1500 		skb_put(skb, frame_size);
1501 
1502 		dev->stats.rx_bytes += frame_size;
1503 
1504 		skb->protocol = eth_type_trans(skb, dev);
1505 		netif_rx(skb);
1506 
1507 		head_list->buffer[0].address =
1508 			dma_map_single(&priv->pci_dev->dev, new_skb->data,
1509 				       TLAN_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
1510 
1511 		tlan_store_skb(head_list, new_skb);
1512 drop_and_reuse:
1513 		head_list->forward = 0;
1514 		head_list->c_stat = 0;
1515 		tail_list = priv->rx_list + priv->rx_tail;
1516 		tail_list->forward = head_list_phys;
1517 
1518 		CIRC_INC(priv->rx_head, TLAN_NUM_RX_LISTS);
1519 		CIRC_INC(priv->rx_tail, TLAN_NUM_RX_LISTS);
1520 		head_list = priv->rx_list + priv->rx_head;
1521 		head_list_phys = priv->rx_list_dma
1522 			+ sizeof(struct tlan_list)*priv->rx_head;
1523 	}
1524 
1525 	if (!ack)
1526 		netdev_info(dev,
1527 			    "Received interrupt for uncompleted RX frame\n");
1528 
1529 
1530 	if (eoc) {
1531 		TLAN_DBG(TLAN_DEBUG_RX,
1532 			 "RECEIVE:  handling RX EOC (Head=%d Tail=%d)\n",
1533 			 priv->rx_head, priv->rx_tail);
1534 		head_list = priv->rx_list + priv->rx_head;
1535 		head_list_phys = priv->rx_list_dma
1536 			+ sizeof(struct tlan_list)*priv->rx_head;
1537 		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1538 		ack |= TLAN_HC_GO | TLAN_HC_RT;
1539 		priv->rx_eoc_count++;
1540 	}
1541 
1542 	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
1543 		tlan_dio_write8(dev->base_addr,
1544 				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
1545 		if (priv->timer.function == NULL)  {
1546 			priv->timer.function = tlan_timer;
1547 			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
1548 			priv->timer_set_at = jiffies;
1549 			priv->timer_type = TLAN_TIMER_ACTIVITY;
1550 			add_timer(&priv->timer);
1551 		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
1552 			priv->timer_set_at = jiffies;
1553 		}
1554 	}
1555 
1556 	return ack;
1557 
1558 }
1559 
1560 
1561 
1562 
1563 /***************************************************************
1564  *	tlan_handle_dummy
1565  *
1566  *	Returns:
1567  *		1
1568  *	Parms:
1569  *		dev		Device assigned the IRQ that was
1570  *				raised.
1571  *		host_int	The contents of the HOST_INT
1572  *				port.
1573  *
1574  *	This function handles the Dummy interrupt, which is
1575  *	raised whenever a test interrupt is generated by setting
1576  *	the Req_Int bit of HOST_CMD to 1.
1577  *
1578  **************************************************************/
1579 
1580 static u32 tlan_handle_dummy(struct net_device *dev, u16 host_int)
1581 {
1582 	netdev_info(dev, "Test interrupt\n");
1583 	return 1;
1584 
1585 }
1586 
1587 
1588 
1589 
1590 /***************************************************************
1591  *	tlan_handle_tx_eoc
1592  *
1593  *	Returns:
1594  *		1
1595  *	Parms:
1596  *		dev		Device assigned the IRQ that was
1597  *				raised.
1598  *		host_int	The contents of the HOST_INT
1599  *				port.
1600  *
1601  *	This driver is structured to determine EOC occurrences by
1602  *	reading the CSTAT member of the list structure.  Tx EOC
1603  *	interrupts are disabled via the DIO INTDIS register.
1604  *	However, TLAN chips before revision 3.0 didn't have this
1605  *	functionality, so process EOC events if this is the
1606  *	case.
1607  *
1608  **************************************************************/
1609 
1610 static u32 tlan_handle_tx_eoc(struct net_device *dev, u16 host_int)
1611 {
1612 	struct tlan_priv	*priv = netdev_priv(dev);
1613 	struct tlan_list		*head_list;
1614 	dma_addr_t		head_list_phys;
1615 	u32			ack = 1;
1616 
1617 	if (priv->tlan_rev < 0x30) {
1618 		TLAN_DBG(TLAN_DEBUG_TX,
1619 			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d) -- IRQ\n",
1620 			 priv->tx_head, priv->tx_tail);
1621 		head_list = priv->tx_list + priv->tx_head;
1622 		head_list_phys = priv->tx_list_dma
1623 			+ sizeof(struct tlan_list)*priv->tx_head;
1624 		if ((head_list->c_stat & TLAN_CSTAT_READY)
1625 		    == TLAN_CSTAT_READY) {
1626 			netif_stop_queue(dev);
1627 			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1628 			ack |= TLAN_HC_GO;
1629 		} else {
1630 			priv->tx_in_progress = 0;
1631 		}
1632 	}
1633 
1634 	return ack;
1635 
1636 }
1637 
1638 
1639 
1640 
1641 /***************************************************************
1642  *	tlan_handle_status_check
1643  *
1644  *	Returns:
1645  *		0 if Adapter check, 1 if Network Status check.
1646  *	Parms:
1647  *		dev		Device assigned the IRQ that was
1648  *				raised.
1649  *		host_int	The contents of the HOST_INT
1650  *				port.
1651  *
1652  *	This function handles Adapter Check/Network Status
1653  *	interrupts generated by the adapter.  It checks the
1654  *	vector in the HOST_INT register to determine if it is
1655  *	an Adapter Check interrupt.  If so, it resets the
1656  *	adapter.  Otherwise it clears the status registers
1657  *	and services the PHY.
1658  *
1659  **************************************************************/
1660 
1661 static u32 tlan_handle_status_check(struct net_device *dev, u16 host_int)
1662 {
1663 	struct tlan_priv	*priv = netdev_priv(dev);
1664 	u32		ack;
1665 	u32		error;
1666 	u8		net_sts;
1667 	u32		phy;
1668 	u16		tlphy_ctl;
1669 	u16		tlphy_sts;
1670 
1671 	ack = 1;
1672 	if (host_int & TLAN_HI_IV_MASK) {
1673 		netif_stop_queue(dev);
1674 		error = inl(dev->base_addr + TLAN_CH_PARM);
1675 		netdev_info(dev, "Adaptor Error = 0x%x\n", error);
1676 		tlan_read_and_clear_stats(dev, TLAN_RECORD);
1677 		outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
1678 
1679 		schedule_work(&priv->tlan_tqueue);
1680 
1681 		netif_wake_queue(dev);
1682 		ack = 0;
1683 	} else {
1684 		TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Status Check\n", dev->name);
1685 		phy = priv->phy[priv->phy_num];
1686 
1687 		net_sts = tlan_dio_read8(dev->base_addr, TLAN_NET_STS);
1688 		if (net_sts) {
1689 			tlan_dio_write8(dev->base_addr, TLAN_NET_STS, net_sts);
1690 			TLAN_DBG(TLAN_DEBUG_GNRL, "%s:    Net_Sts = %x\n",
1691 				 dev->name, (unsigned) net_sts);
1692 		}
1693 		if ((net_sts & TLAN_NET_STS_MIRQ) &&  (priv->phy_num == 0)) {
1694 			__tlan_mii_read_reg(dev, phy, TLAN_TLPHY_STS, &tlphy_sts);
1695 			__tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
1696 			if (!(tlphy_sts & TLAN_TS_POLOK) &&
1697 			    !(tlphy_ctl & TLAN_TC_SWAPOL)) {
1698 				tlphy_ctl |= TLAN_TC_SWAPOL;
1699 				__tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
1700 						     tlphy_ctl);
1701 			} else if ((tlphy_sts & TLAN_TS_POLOK) &&
1702 				   (tlphy_ctl & TLAN_TC_SWAPOL)) {
1703 				tlphy_ctl &= ~TLAN_TC_SWAPOL;
1704 				__tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
1705 						     tlphy_ctl);
1706 			}
1707 
1708 			if (debug)
1709 				__tlan_phy_print(dev);
1710 		}
1711 	}
1712 
1713 	return ack;
1714 
1715 }
1716 
1717 
1718 
1719 
1720 /***************************************************************
1721  *	tlan_handle_rx_eoc
1722  *
1723  *	Returns:
1724  *		1
1725  *	Parms:
1726  *		dev		Device assigned the IRQ that was
1727  *				raised.
1728  *		host_int	The contents of the HOST_INT
1729  *				port.
1730  *
1731  *	This driver is structured to determine EOC occurrences by
1732  *	reading the CSTAT member of the list structure.  Rx EOC
1733  *	interrupts are disabled via the DIO INTDIS register.
1734  *	However, TLAN chips before revision 3.0 didn't have this
1735  *	CSTAT member or a INTDIS register, so if this chip is
1736  *	pre-3.0, process EOC interrupts normally.
1737  *
1738  **************************************************************/
1739 
1740 static u32 tlan_handle_rx_eoc(struct net_device *dev, u16 host_int)
1741 {
1742 	struct tlan_priv	*priv = netdev_priv(dev);
1743 	dma_addr_t	head_list_phys;
1744 	u32		ack = 1;
1745 
1746 	if (priv->tlan_rev < 0x30) {
1747 		TLAN_DBG(TLAN_DEBUG_RX,
1748 			 "RECEIVE:  Handling RX EOC (head=%d tail=%d) -- IRQ\n",
1749 			 priv->rx_head, priv->rx_tail);
1750 		head_list_phys = priv->rx_list_dma
1751 			+ sizeof(struct tlan_list)*priv->rx_head;
1752 		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
1753 		ack |= TLAN_HC_GO | TLAN_HC_RT;
1754 		priv->rx_eoc_count++;
1755 	}
1756 
1757 	return ack;
1758 
1759 }
1760 
1761 
1762 
1763 
1764 /*****************************************************************************
1765 ******************************************************************************
1766 
1767 ThunderLAN driver timer function
1768 
1769 ******************************************************************************
1770 *****************************************************************************/
1771 
1772 
1773 /***************************************************************
1774  *	tlan_timer
1775  *
1776  *	Returns:
1777  *		Nothing
1778  *	Parms:
1779  *		data	A value given to add timer when
1780  *			add_timer was called.
1781  *
1782  *	This function handles timed functionality for the
1783  *	TLAN driver.  The two current timer uses are for
1784  *	delaying for autonegotionation and driving the ACT LED.
1785  *	-	Autonegotiation requires being allowed about
1786  *		2 1/2 seconds before attempting to transmit a
1787  *		packet.  It would be a very bad thing to hang
1788  *		the kernel this long, so the driver doesn't
1789  *		allow transmission 'til after this time, for
1790  *		certain PHYs.  It would be much nicer if all
1791  *		PHYs were interrupt-capable like the internal
1792  *		PHY.
1793  *	-	The ACT LED, which shows adapter activity, is
1794  *		driven by the driver, and so must be left on
1795  *		for a short period to power up the LED so it
1796  *		can be seen.  This delay can be changed by
1797  *		changing the TLAN_TIMER_ACT_DELAY in tlan.h,
1798  *		if desired.  100 ms  produces a slightly
1799  *		sluggish response.
1800  *
1801  **************************************************************/
1802 
1803 static void tlan_timer(struct timer_list *t)
1804 {
1805 	struct tlan_priv	*priv = timer_container_of(priv, t, timer);
1806 	struct net_device	*dev = priv->dev;
1807 	u32		elapsed;
1808 	unsigned long	flags = 0;
1809 
1810 	priv->timer.function = NULL;
1811 
1812 	switch (priv->timer_type) {
1813 	case TLAN_TIMER_PHY_PDOWN:
1814 		tlan_phy_power_down(dev);
1815 		break;
1816 	case TLAN_TIMER_PHY_PUP:
1817 		tlan_phy_power_up(dev);
1818 		break;
1819 	case TLAN_TIMER_PHY_RESET:
1820 		tlan_phy_reset(dev);
1821 		break;
1822 	case TLAN_TIMER_PHY_START_LINK:
1823 		tlan_phy_start_link(dev);
1824 		break;
1825 	case TLAN_TIMER_PHY_FINISH_AN:
1826 		tlan_phy_finish_auto_neg(dev);
1827 		break;
1828 	case TLAN_TIMER_FINISH_RESET:
1829 		tlan_finish_reset(dev);
1830 		break;
1831 	case TLAN_TIMER_ACTIVITY:
1832 		spin_lock_irqsave(&priv->lock, flags);
1833 		if (priv->timer.function == NULL) {
1834 			elapsed = jiffies - priv->timer_set_at;
1835 			if (elapsed >= TLAN_TIMER_ACT_DELAY) {
1836 				tlan_dio_write8(dev->base_addr,
1837 						TLAN_LED_REG, TLAN_LED_LINK);
1838 			} else  {
1839 				priv->timer.expires = priv->timer_set_at
1840 					+ TLAN_TIMER_ACT_DELAY;
1841 				spin_unlock_irqrestore(&priv->lock, flags);
1842 				add_timer(&priv->timer);
1843 				break;
1844 			}
1845 		}
1846 		spin_unlock_irqrestore(&priv->lock, flags);
1847 		break;
1848 	default:
1849 		break;
1850 	}
1851 
1852 }
1853 
1854 
1855 /*****************************************************************************
1856 ******************************************************************************
1857 
1858 ThunderLAN driver adapter related routines
1859 
1860 ******************************************************************************
1861 *****************************************************************************/
1862 
1863 
1864 /***************************************************************
1865  *	tlan_reset_lists
1866  *
1867  *	Returns:
1868  *		Nothing
1869  *	Parms:
1870  *		dev	The device structure with the list
1871  *			structures to be reset.
1872  *
1873  *	This routine sets the variables associated with managing
1874  *	the TLAN lists to their initial values.
1875  *
1876  **************************************************************/
1877 
1878 static void tlan_reset_lists(struct net_device *dev)
1879 {
1880 	struct tlan_priv *priv = netdev_priv(dev);
1881 	int		i;
1882 	struct tlan_list	*list;
1883 	dma_addr_t	list_phys;
1884 	struct sk_buff	*skb;
1885 
1886 	priv->tx_head = 0;
1887 	priv->tx_tail = 0;
1888 	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
1889 		list = priv->tx_list + i;
1890 		list->c_stat = TLAN_CSTAT_UNUSED;
1891 		list->buffer[0].address = 0;
1892 		list->buffer[2].count = 0;
1893 		list->buffer[2].address = 0;
1894 		list->buffer[8].address = 0;
1895 		list->buffer[9].address = 0;
1896 	}
1897 
1898 	priv->rx_head = 0;
1899 	priv->rx_tail = TLAN_NUM_RX_LISTS - 1;
1900 	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
1901 		list = priv->rx_list + i;
1902 		list_phys = priv->rx_list_dma + sizeof(struct tlan_list)*i;
1903 		list->c_stat = TLAN_CSTAT_READY;
1904 		list->frame_size = TLAN_MAX_FRAME_SIZE;
1905 		list->buffer[0].count = TLAN_MAX_FRAME_SIZE | TLAN_LAST_BUFFER;
1906 		skb = netdev_alloc_skb_ip_align(dev, TLAN_MAX_FRAME_SIZE + 5);
1907 		if (!skb)
1908 			break;
1909 
1910 		list->buffer[0].address = dma_map_single(&priv->pci_dev->dev,
1911 							 skb->data,
1912 							 TLAN_MAX_FRAME_SIZE,
1913 							 DMA_FROM_DEVICE);
1914 		tlan_store_skb(list, skb);
1915 		list->buffer[1].count = 0;
1916 		list->buffer[1].address = 0;
1917 		list->forward = list_phys + sizeof(struct tlan_list);
1918 	}
1919 
1920 	/* in case ran out of memory early, clear bits */
1921 	while (i < TLAN_NUM_RX_LISTS) {
1922 		tlan_store_skb(priv->rx_list + i, NULL);
1923 		++i;
1924 	}
1925 	list->forward = 0;
1926 
1927 }
1928 
1929 
1930 static void tlan_free_lists(struct net_device *dev)
1931 {
1932 	struct tlan_priv *priv = netdev_priv(dev);
1933 	int		i;
1934 	struct tlan_list	*list;
1935 	struct sk_buff	*skb;
1936 
1937 	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
1938 		list = priv->tx_list + i;
1939 		skb = tlan_get_skb(list);
1940 		if (skb) {
1941 			dma_unmap_single(&priv->pci_dev->dev,
1942 					 list->buffer[0].address,
1943 					 max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE),
1944 					 DMA_TO_DEVICE);
1945 			dev_kfree_skb_any(skb);
1946 			list->buffer[8].address = 0;
1947 			list->buffer[9].address = 0;
1948 		}
1949 	}
1950 
1951 	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
1952 		list = priv->rx_list + i;
1953 		skb = tlan_get_skb(list);
1954 		if (skb) {
1955 			dma_unmap_single(&priv->pci_dev->dev,
1956 					 list->buffer[0].address,
1957 					 TLAN_MAX_FRAME_SIZE, DMA_FROM_DEVICE);
1958 			dev_kfree_skb_any(skb);
1959 			list->buffer[8].address = 0;
1960 			list->buffer[9].address = 0;
1961 		}
1962 	}
1963 }
1964 
1965 
1966 
1967 
1968 /***************************************************************
1969  *	tlan_print_dio
1970  *
1971  *	Returns:
1972  *		Nothing
1973  *	Parms:
1974  *		io_base		Base IO port of the device of
1975  *				which to print DIO registers.
1976  *
1977  *	This function prints out all the internal (DIO)
1978  *	registers of a TLAN chip.
1979  *
1980  **************************************************************/
1981 
1982 static void tlan_print_dio(u16 io_base)
1983 {
1984 	u32 data0, data1;
1985 	int	i;
1986 
1987 	pr_info("Contents of internal registers for io base 0x%04hx\n",
1988 		io_base);
1989 	pr_info("Off.  +0        +4\n");
1990 	for (i = 0; i < 0x4C; i += 8) {
1991 		data0 = tlan_dio_read32(io_base, i);
1992 		data1 = tlan_dio_read32(io_base, i + 0x4);
1993 		pr_info("0x%02x  0x%08x 0x%08x\n", i, data0, data1);
1994 	}
1995 
1996 }
1997 
1998 
1999 
2000 
2001 /***************************************************************
2002  *	TLan_PrintList
2003  *
2004  *	Returns:
2005  *		Nothing
2006  *	Parms:
2007  *		list	A pointer to the struct tlan_list structure to
2008  *			be printed.
2009  *		type	A string to designate type of list,
2010  *			"Rx" or "Tx".
2011  *		num	The index of the list.
2012  *
2013  *	This function prints out the contents of the list
2014  *	pointed to by the list parameter.
2015  *
2016  **************************************************************/
2017 
2018 static void tlan_print_list(struct tlan_list *list, char *type, int num)
2019 {
2020 	int i;
2021 
2022 	pr_info("%s List %d at %p\n", type, num, list);
2023 	pr_info("   Forward    = 0x%08x\n",  list->forward);
2024 	pr_info("   CSTAT      = 0x%04hx\n", list->c_stat);
2025 	pr_info("   Frame Size = 0x%04hx\n", list->frame_size);
2026 	/* for (i = 0; i < 10; i++) { */
2027 	for (i = 0; i < 2; i++) {
2028 		pr_info("   Buffer[%d].count, addr = 0x%08x, 0x%08x\n",
2029 			i, list->buffer[i].count, list->buffer[i].address);
2030 	}
2031 
2032 }
2033 
2034 
2035 
2036 
2037 /***************************************************************
2038  *	tlan_read_and_clear_stats
2039  *
2040  *	Returns:
2041  *		Nothing
2042  *	Parms:
2043  *		dev	Pointer to device structure of adapter
2044  *			to which to read stats.
2045  *		record	Flag indicating whether to add
2046  *
2047  *	This functions reads all the internal status registers
2048  *	of the TLAN chip, which clears them as a side effect.
2049  *	It then either adds the values to the device's status
2050  *	struct, or discards them, depending on whether record
2051  *	is TLAN_RECORD (!=0)  or TLAN_IGNORE (==0).
2052  *
2053  **************************************************************/
2054 
2055 static void tlan_read_and_clear_stats(struct net_device *dev, int record)
2056 {
2057 	u32		tx_good, tx_under;
2058 	u32		rx_good, rx_over;
2059 	u32		def_tx, crc, code;
2060 	u32		multi_col, single_col;
2061 	u32		excess_col, late_col, loss;
2062 
2063 	outw(TLAN_GOOD_TX_FRMS, dev->base_addr + TLAN_DIO_ADR);
2064 	tx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
2065 	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2066 	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
2067 	tx_under = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2068 
2069 	outw(TLAN_GOOD_RX_FRMS, dev->base_addr + TLAN_DIO_ADR);
2070 	rx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
2071 	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2072 	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
2073 	rx_over  = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2074 
2075 	outw(TLAN_DEFERRED_TX, dev->base_addr + TLAN_DIO_ADR);
2076 	def_tx  = inb(dev->base_addr + TLAN_DIO_DATA);
2077 	def_tx += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2078 	crc     = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2079 	code    = inb(dev->base_addr + TLAN_DIO_DATA + 3);
2080 
2081 	outw(TLAN_MULTICOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
2082 	multi_col   = inb(dev->base_addr + TLAN_DIO_DATA);
2083 	multi_col  += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
2084 	single_col  = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2085 	single_col += inb(dev->base_addr + TLAN_DIO_DATA + 3) << 8;
2086 
2087 	outw(TLAN_EXCESSCOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
2088 	excess_col = inb(dev->base_addr + TLAN_DIO_DATA);
2089 	late_col   = inb(dev->base_addr + TLAN_DIO_DATA + 1);
2090 	loss       = inb(dev->base_addr + TLAN_DIO_DATA + 2);
2091 
2092 	if (record) {
2093 		dev->stats.rx_packets += rx_good;
2094 		dev->stats.rx_errors  += rx_over + crc + code;
2095 		dev->stats.tx_packets += tx_good;
2096 		dev->stats.tx_errors  += tx_under + loss;
2097 		dev->stats.collisions += multi_col
2098 			+ single_col + excess_col + late_col;
2099 
2100 		dev->stats.rx_over_errors    += rx_over;
2101 		dev->stats.rx_crc_errors     += crc;
2102 		dev->stats.rx_frame_errors   += code;
2103 
2104 		dev->stats.tx_aborted_errors += tx_under;
2105 		dev->stats.tx_carrier_errors += loss;
2106 	}
2107 
2108 }
2109 
2110 
2111 
2112 
2113 /***************************************************************
2114  *	TLan_Reset
2115  *
2116  *	Returns:
2117  *		0
2118  *	Parms:
2119  *		dev	Pointer to device structure of adapter
2120  *			to be reset.
2121  *
2122  *	This function resets the adapter and it's physical
2123  *	device.  See Chap. 3, pp. 9-10 of the "ThunderLAN
2124  *	Programmer's Guide" for details.  The routine tries to
2125  *	implement what is detailed there, though adjustments
2126  *	have been made.
2127  *
2128  **************************************************************/
2129 
2130 static void
2131 tlan_reset_adapter(struct net_device *dev)
2132 {
2133 	struct tlan_priv	*priv = netdev_priv(dev);
2134 	int		i;
2135 	u32		addr;
2136 	u32		data;
2137 	u8		data8;
2138 
2139 	priv->tlan_full_duplex = false;
2140 	priv->phy_online = 0;
2141 	netif_carrier_off(dev);
2142 
2143 /*  1.	Assert reset bit. */
2144 
2145 	data = inl(dev->base_addr + TLAN_HOST_CMD);
2146 	data |= TLAN_HC_AD_RST;
2147 	outl(data, dev->base_addr + TLAN_HOST_CMD);
2148 
2149 	udelay(1000);
2150 
2151 /*  2.	Turn off interrupts. (Probably isn't necessary) */
2152 
2153 	data = inl(dev->base_addr + TLAN_HOST_CMD);
2154 	data |= TLAN_HC_INT_OFF;
2155 	outl(data, dev->base_addr + TLAN_HOST_CMD);
2156 
2157 /*  3.	Clear AREGs and HASHs. */
2158 
2159 	for (i = TLAN_AREG_0; i <= TLAN_HASH_2; i += 4)
2160 		tlan_dio_write32(dev->base_addr, (u16) i, 0);
2161 
2162 /*  4.	Setup NetConfig register. */
2163 
2164 	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN | TLAN_NET_CFG_PHY_EN;
2165 	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
2166 
2167 /*  5.	Load Ld_Tmr and Ld_Thr in HOST_CMD. */
2168 
2169 	outl(TLAN_HC_LD_TMR | 0x3f, dev->base_addr + TLAN_HOST_CMD);
2170 	outl(TLAN_HC_LD_THR | 0x9, dev->base_addr + TLAN_HOST_CMD);
2171 
2172 /*  6.	Unreset the MII by setting NMRST (in NetSio) to 1. */
2173 
2174 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
2175 	addr = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2176 	tlan_set_bit(TLAN_NET_SIO_NMRST, addr);
2177 
2178 /*  7.	Setup the remaining registers. */
2179 
2180 	if (priv->tlan_rev >= 0x30) {
2181 		data8 = TLAN_ID_TX_EOC | TLAN_ID_RX_EOC;
2182 		tlan_dio_write8(dev->base_addr, TLAN_INT_DIS, data8);
2183 	}
2184 	tlan_phy_detect(dev);
2185 	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN;
2186 
2187 	if (priv->adapter->flags & TLAN_ADAPTER_BIT_RATE_PHY) {
2188 		data |= TLAN_NET_CFG_BIT;
2189 		if (priv->aui == 1) {
2190 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x0a);
2191 		} else if (priv->duplex == TLAN_DUPLEX_FULL) {
2192 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x00);
2193 			priv->tlan_full_duplex = true;
2194 		} else {
2195 			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x08);
2196 		}
2197 	}
2198 
2199 	/* don't power down internal PHY if we're going to use it */
2200 	if (priv->phy_num == 0 ||
2201 	   (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10))
2202 		data |= TLAN_NET_CFG_PHY_EN;
2203 	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
2204 
2205 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY)
2206 		tlan_finish_reset(dev);
2207 	else
2208 		tlan_phy_power_down(dev);
2209 
2210 }
2211 
2212 
2213 
2214 
2215 static void
2216 tlan_finish_reset(struct net_device *dev)
2217 {
2218 	struct tlan_priv	*priv = netdev_priv(dev);
2219 	u8		data;
2220 	u32		phy;
2221 	u8		sio;
2222 	u16		status;
2223 	u16		partner;
2224 	u16		tlphy_ctl;
2225 	u16		tlphy_par;
2226 	u16		tlphy_id1, tlphy_id2;
2227 	int		i;
2228 
2229 	phy = priv->phy[priv->phy_num];
2230 
2231 	data = TLAN_NET_CMD_NRESET | TLAN_NET_CMD_NWRAP;
2232 	if (priv->tlan_full_duplex)
2233 		data |= TLAN_NET_CMD_DUPLEX;
2234 	tlan_dio_write8(dev->base_addr, TLAN_NET_CMD, data);
2235 	data = TLAN_NET_MASK_MASK4 | TLAN_NET_MASK_MASK5;
2236 	if (priv->phy_num == 0)
2237 		data |= TLAN_NET_MASK_MASK7;
2238 	tlan_dio_write8(dev->base_addr, TLAN_NET_MASK, data);
2239 	tlan_dio_write16(dev->base_addr, TLAN_MAX_RX, ((1536)+7)&~7);
2240 	tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &tlphy_id1);
2241 	tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &tlphy_id2);
2242 
2243 	if ((priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) ||
2244 	    (priv->aui)) {
2245 		status = MII_GS_LINK;
2246 		netdev_info(dev, "Link forced\n");
2247 	} else {
2248 		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2249 		udelay(1000);
2250 		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2251 		if (status & MII_GS_LINK) {
2252 			/* We only support link info on Nat.Sem. PHY's */
2253 			if ((tlphy_id1 == NAT_SEM_ID1) &&
2254 			    (tlphy_id2 == NAT_SEM_ID2)) {
2255 				tlan_mii_read_reg(dev, phy, MII_AN_LPA,
2256 					&partner);
2257 				tlan_mii_read_reg(dev, phy, TLAN_TLPHY_PAR,
2258 					&tlphy_par);
2259 
2260 				netdev_info(dev,
2261 					"Link active, %s %uMbps %s-Duplex\n",
2262 					!(tlphy_par & TLAN_PHY_AN_EN_STAT)
2263 					? "forced" : "Autonegotiation enabled,",
2264 					tlphy_par & TLAN_PHY_SPEED_100
2265 					? 100 : 10,
2266 					tlphy_par & TLAN_PHY_DUPLEX_FULL
2267 					? "Full" : "Half");
2268 
2269 				if (tlphy_par & TLAN_PHY_AN_EN_STAT) {
2270 					netdev_info(dev, "Partner capability:");
2271 					for (i = 5; i < 10; i++)
2272 						if (partner & (1 << i))
2273 							pr_cont(" %s",
2274 								media[i-5]);
2275 					pr_cont("\n");
2276 				}
2277 			} else
2278 				netdev_info(dev, "Link active\n");
2279 			/* Enabling link beat monitoring */
2280 			priv->media_timer.expires = jiffies + HZ;
2281 			add_timer(&priv->media_timer);
2282 		}
2283 	}
2284 
2285 	if (priv->phy_num == 0) {
2286 		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
2287 		tlphy_ctl |= TLAN_TC_INTEN;
2288 		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tlphy_ctl);
2289 		sio = tlan_dio_read8(dev->base_addr, TLAN_NET_SIO);
2290 		sio |= TLAN_NET_SIO_MINTEN;
2291 		tlan_dio_write8(dev->base_addr, TLAN_NET_SIO, sio);
2292 	}
2293 
2294 	if (status & MII_GS_LINK) {
2295 		tlan_set_mac(dev, 0, dev->dev_addr);
2296 		priv->phy_online = 1;
2297 		outb((TLAN_HC_INT_ON >> 8), dev->base_addr + TLAN_HOST_CMD + 1);
2298 		if (debug >= 1 && debug != TLAN_DEBUG_PROBE)
2299 			outb((TLAN_HC_REQ_INT >> 8),
2300 			     dev->base_addr + TLAN_HOST_CMD + 1);
2301 		outl(priv->rx_list_dma, dev->base_addr + TLAN_CH_PARM);
2302 		outl(TLAN_HC_GO | TLAN_HC_RT, dev->base_addr + TLAN_HOST_CMD);
2303 		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
2304 		netif_carrier_on(dev);
2305 	} else {
2306 		netdev_info(dev, "Link inactive, will retry in 10 secs...\n");
2307 		tlan_set_timer(dev, (10*HZ), TLAN_TIMER_FINISH_RESET);
2308 		return;
2309 	}
2310 	tlan_set_multicast_list(dev);
2311 
2312 }
2313 
2314 
2315 
2316 
2317 /***************************************************************
2318  *	tlan_set_mac
2319  *
2320  *	Returns:
2321  *		Nothing
2322  *	Parms:
2323  *		dev	Pointer to device structure of adapter
2324  *			on which to change the AREG.
2325  *		areg	The AREG to set the address in (0 - 3).
2326  *		mac	A pointer to an array of chars.  Each
2327  *			element stores one byte of the address.
2328  *			IE, it isn't in ascii.
2329  *
2330  *	This function transfers a MAC address to one of the
2331  *	TLAN AREGs (address registers).  The TLAN chip locks
2332  *	the register on writing to offset 0 and unlocks the
2333  *	register after writing to offset 5.  If NULL is passed
2334  *	in mac, then the AREG is filled with 0's.
2335  *
2336  **************************************************************/
2337 
2338 static void tlan_set_mac(struct net_device *dev, int areg, const char *mac)
2339 {
2340 	int i;
2341 
2342 	areg *= 6;
2343 
2344 	if (mac != NULL) {
2345 		for (i = 0; i < 6; i++)
2346 			tlan_dio_write8(dev->base_addr,
2347 					TLAN_AREG_0 + areg + i, mac[i]);
2348 	} else {
2349 		for (i = 0; i < 6; i++)
2350 			tlan_dio_write8(dev->base_addr,
2351 					TLAN_AREG_0 + areg + i, 0);
2352 	}
2353 
2354 }
2355 
2356 
2357 
2358 
2359 /*****************************************************************************
2360 ******************************************************************************
2361 
2362 ThunderLAN driver PHY layer routines
2363 
2364 ******************************************************************************
2365 *****************************************************************************/
2366 
2367 
2368 
2369 /*********************************************************************
2370  *	__tlan_phy_print
2371  *
2372  *	Returns:
2373  *		Nothing
2374  *	Parms:
2375  *		dev	A pointer to the device structure of the
2376  *			TLAN device having the PHYs to be detailed.
2377  *
2378  *	This function prints the registers a PHY (aka transceiver).
2379  *
2380  ********************************************************************/
2381 
2382 static void __tlan_phy_print(struct net_device *dev)
2383 {
2384 	struct tlan_priv *priv = netdev_priv(dev);
2385 	u16 i, data0, data1, data2, data3, phy;
2386 
2387 	lockdep_assert_held(&priv->lock);
2388 
2389 	phy = priv->phy[priv->phy_num];
2390 
2391 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
2392 		netdev_info(dev, "Unmanaged PHY\n");
2393 	} else if (phy <= TLAN_PHY_MAX_ADDR) {
2394 		netdev_info(dev, "PHY 0x%02x\n", phy);
2395 		pr_info("   Off.  +0     +1     +2     +3\n");
2396 		for (i = 0; i < 0x20; i += 4) {
2397 			__tlan_mii_read_reg(dev, phy, i, &data0);
2398 			__tlan_mii_read_reg(dev, phy, i + 1, &data1);
2399 			__tlan_mii_read_reg(dev, phy, i + 2, &data2);
2400 			__tlan_mii_read_reg(dev, phy, i + 3, &data3);
2401 			pr_info("   0x%02x 0x%04hx 0x%04hx 0x%04hx 0x%04hx\n",
2402 				i, data0, data1, data2, data3);
2403 		}
2404 	} else {
2405 		netdev_info(dev, "Invalid PHY\n");
2406 	}
2407 
2408 }
2409 
2410 static void tlan_phy_print(struct net_device *dev)
2411 {
2412 	struct tlan_priv *priv = netdev_priv(dev);
2413 	unsigned long flags;
2414 
2415 	spin_lock_irqsave(&priv->lock, flags);
2416 	__tlan_phy_print(dev);
2417 	spin_unlock_irqrestore(&priv->lock, flags);
2418 }
2419 
2420 
2421 /*********************************************************************
2422  *	tlan_phy_detect
2423  *
2424  *	Returns:
2425  *		Nothing
2426  *	Parms:
2427  *		dev	A pointer to the device structure of the adapter
2428  *			for which the PHY needs determined.
2429  *
2430  *	So far I've found that adapters which have external PHYs
2431  *	may also use the internal PHY for part of the functionality.
2432  *	(eg, AUI/Thinnet).  This function finds out if this TLAN
2433  *	chip has an internal PHY, and then finds the first external
2434  *	PHY (starting from address 0) if it exists).
2435  *
2436  ********************************************************************/
2437 
2438 static void tlan_phy_detect(struct net_device *dev)
2439 {
2440 	struct tlan_priv *priv = netdev_priv(dev);
2441 	u16		control;
2442 	u16		hi;
2443 	u16		lo;
2444 	u32		phy;
2445 
2446 	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
2447 		priv->phy_num = 0xffff;
2448 		return;
2449 	}
2450 
2451 	tlan_mii_read_reg(dev, TLAN_PHY_MAX_ADDR, MII_GEN_ID_HI, &hi);
2452 
2453 	if (hi != 0xffff)
2454 		priv->phy[0] = TLAN_PHY_MAX_ADDR;
2455 	else
2456 		priv->phy[0] = TLAN_PHY_NONE;
2457 
2458 	priv->phy[1] = TLAN_PHY_NONE;
2459 	for (phy = 0; phy <= TLAN_PHY_MAX_ADDR; phy++) {
2460 		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &control);
2461 		tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &hi);
2462 		tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &lo);
2463 		if ((control != 0xffff) ||
2464 		    (hi != 0xffff) || (lo != 0xffff)) {
2465 			TLAN_DBG(TLAN_DEBUG_GNRL,
2466 				 "PHY found at %02x %04x %04x %04x\n",
2467 				 phy, control, hi, lo);
2468 			if ((priv->phy[1] == TLAN_PHY_NONE) &&
2469 			    (phy != TLAN_PHY_MAX_ADDR)) {
2470 				priv->phy[1] = phy;
2471 			}
2472 		}
2473 	}
2474 
2475 	if (priv->phy[1] != TLAN_PHY_NONE)
2476 		priv->phy_num = 1;
2477 	else if (priv->phy[0] != TLAN_PHY_NONE)
2478 		priv->phy_num = 0;
2479 	else
2480 		netdev_info(dev, "Cannot initialize device, no PHY was found!\n");
2481 
2482 }
2483 
2484 
2485 
2486 
2487 static void tlan_phy_power_down(struct net_device *dev)
2488 {
2489 	struct tlan_priv	*priv = netdev_priv(dev);
2490 	u16		value;
2491 
2492 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering down PHY(s).\n", dev->name);
2493 	value = MII_GC_PDOWN | MII_GC_LOOPBK | MII_GC_ISOLATE;
2494 	tlan_mii_sync(dev->base_addr);
2495 	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
2496 	if ((priv->phy_num == 0) && (priv->phy[1] != TLAN_PHY_NONE)) {
2497 		/* if using internal PHY, the external PHY must be powered on */
2498 		if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10)
2499 			value = MII_GC_ISOLATE; /* just isolate it from MII */
2500 		tlan_mii_sync(dev->base_addr);
2501 		tlan_mii_write_reg(dev, priv->phy[1], MII_GEN_CTL, value);
2502 	}
2503 
2504 	/* Wait for 50 ms and powerup
2505 	 * This is arbitrary.  It is intended to make sure the
2506 	 * transceiver settles.
2507 	 */
2508 	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_PUP);
2509 
2510 }
2511 
2512 
2513 
2514 
2515 static void tlan_phy_power_up(struct net_device *dev)
2516 {
2517 	struct tlan_priv	*priv = netdev_priv(dev);
2518 	u16		value;
2519 
2520 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering up PHY.\n", dev->name);
2521 	tlan_mii_sync(dev->base_addr);
2522 	value = MII_GC_LOOPBK;
2523 	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
2524 	tlan_mii_sync(dev->base_addr);
2525 	/* Wait for 500 ms and reset the
2526 	 * transceiver.  The TLAN docs say both 50 ms and
2527 	 * 500 ms, so do the longer, just in case.
2528 	 */
2529 	tlan_set_timer(dev, msecs_to_jiffies(500), TLAN_TIMER_PHY_RESET);
2530 
2531 }
2532 
2533 
2534 
2535 
2536 static void tlan_phy_reset(struct net_device *dev)
2537 {
2538 	struct tlan_priv	*priv = netdev_priv(dev);
2539 	u16		phy;
2540 	u16		value;
2541 	unsigned long timeout = jiffies + HZ;
2542 
2543 	phy = priv->phy[priv->phy_num];
2544 
2545 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Resetting PHY.\n", dev->name);
2546 	tlan_mii_sync(dev->base_addr);
2547 	value = MII_GC_LOOPBK | MII_GC_RESET;
2548 	tlan_mii_write_reg(dev, phy, MII_GEN_CTL, value);
2549 	do {
2550 		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &value);
2551 		if (time_after(jiffies, timeout)) {
2552 			netdev_err(dev, "PHY reset timeout\n");
2553 			return;
2554 		}
2555 	} while (value & MII_GC_RESET);
2556 
2557 	/* Wait for 500 ms and initialize.
2558 	 * I don't remember why I wait this long.
2559 	 * I've changed this to 50ms, as it seems long enough.
2560 	 */
2561 	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_START_LINK);
2562 
2563 }
2564 
2565 
2566 
2567 
2568 static void tlan_phy_start_link(struct net_device *dev)
2569 {
2570 	struct tlan_priv	*priv = netdev_priv(dev);
2571 	u16		ability;
2572 	u16		control;
2573 	u16		data;
2574 	u16		phy;
2575 	u16		status;
2576 	u16		tctl;
2577 
2578 	phy = priv->phy[priv->phy_num];
2579 	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Trying to activate link.\n", dev->name);
2580 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2581 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &ability);
2582 
2583 	if ((status & MII_GS_AUTONEG) &&
2584 	    (!priv->aui)) {
2585 		ability = status >> 11;
2586 		if (priv->speed  == TLAN_SPEED_10 &&
2587 		    priv->duplex == TLAN_DUPLEX_HALF) {
2588 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0000);
2589 		} else if (priv->speed == TLAN_SPEED_10 &&
2590 			   priv->duplex == TLAN_DUPLEX_FULL) {
2591 			priv->tlan_full_duplex = true;
2592 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0100);
2593 		} else if (priv->speed == TLAN_SPEED_100 &&
2594 			   priv->duplex == TLAN_DUPLEX_HALF) {
2595 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2000);
2596 		} else if (priv->speed == TLAN_SPEED_100 &&
2597 			   priv->duplex == TLAN_DUPLEX_FULL) {
2598 			priv->tlan_full_duplex = true;
2599 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2100);
2600 		} else {
2601 
2602 			/* Set Auto-Neg advertisement */
2603 			tlan_mii_write_reg(dev, phy, MII_AN_ADV,
2604 					   (ability << 5) | 1);
2605 			/* Enablee Auto-Neg */
2606 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1000);
2607 			/* Restart Auto-Neg */
2608 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1200);
2609 			/* Wait for 4 sec for autonegotiation
2610 			 * to complete.  The max spec time is less than this
2611 			 * but the card need additional time to start AN.
2612 			 * .5 sec should be plenty extra.
2613 			 */
2614 			netdev_info(dev, "Starting autonegotiation\n");
2615 			tlan_set_timer(dev, (2*HZ), TLAN_TIMER_PHY_FINISH_AN);
2616 			return;
2617 		}
2618 
2619 	}
2620 
2621 	if ((priv->aui) && (priv->phy_num != 0)) {
2622 		priv->phy_num = 0;
2623 		data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN
2624 			| TLAN_NET_CFG_PHY_EN;
2625 		tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, data);
2626 		tlan_set_timer(dev, msecs_to_jiffies(40), TLAN_TIMER_PHY_PDOWN);
2627 		return;
2628 	} else if (priv->phy_num == 0) {
2629 		control = 0;
2630 		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tctl);
2631 		if (priv->aui) {
2632 			tctl |= TLAN_TC_AUISEL;
2633 		} else {
2634 			tctl &= ~TLAN_TC_AUISEL;
2635 			if (priv->duplex == TLAN_DUPLEX_FULL) {
2636 				control |= MII_GC_DUPLEX;
2637 				priv->tlan_full_duplex = true;
2638 			}
2639 			if (priv->speed == TLAN_SPEED_100)
2640 				control |= MII_GC_SPEEDSEL;
2641 		}
2642 		tlan_mii_write_reg(dev, phy, MII_GEN_CTL, control);
2643 		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tctl);
2644 	}
2645 
2646 	/* Wait for 2 sec to give the transceiver time
2647 	 * to establish link.
2648 	 */
2649 	tlan_set_timer(dev, (4*HZ), TLAN_TIMER_FINISH_RESET);
2650 
2651 }
2652 
2653 
2654 
2655 
2656 static void tlan_phy_finish_auto_neg(struct net_device *dev)
2657 {
2658 	struct tlan_priv	*priv = netdev_priv(dev);
2659 	u16		an_adv;
2660 	u16		an_lpa;
2661 	u16		mode;
2662 	u16		phy;
2663 	u16		status;
2664 
2665 	phy = priv->phy[priv->phy_num];
2666 
2667 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2668 	udelay(1000);
2669 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
2670 
2671 	if (!(status & MII_GS_AUTOCMPLT)) {
2672 		/* Wait for 8 sec to give the process
2673 		 * more time.  Perhaps we should fail after a while.
2674 		 */
2675 		tlan_set_timer(dev, 2 * HZ, TLAN_TIMER_PHY_FINISH_AN);
2676 		return;
2677 	}
2678 
2679 	netdev_info(dev, "Autonegotiation complete\n");
2680 	tlan_mii_read_reg(dev, phy, MII_AN_ADV, &an_adv);
2681 	tlan_mii_read_reg(dev, phy, MII_AN_LPA, &an_lpa);
2682 	mode = an_adv & an_lpa & 0x03E0;
2683 	if (mode & 0x0100)
2684 		priv->tlan_full_duplex = true;
2685 	else if (!(mode & 0x0080) && (mode & 0x0040))
2686 		priv->tlan_full_duplex = true;
2687 
2688 	/* switch to internal PHY for 10 Mbps */
2689 	if ((!(mode & 0x0180)) &&
2690 	    (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) &&
2691 	    (priv->phy_num != 0)) {
2692 		priv->phy_num = 0;
2693 		tlan_set_timer(dev, msecs_to_jiffies(400), TLAN_TIMER_PHY_PDOWN);
2694 		return;
2695 	}
2696 
2697 	if (priv->phy_num == 0) {
2698 		if ((priv->duplex == TLAN_DUPLEX_FULL) ||
2699 		    (an_adv & an_lpa & 0x0040)) {
2700 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
2701 					   MII_GC_AUTOENB | MII_GC_DUPLEX);
2702 			netdev_info(dev, "Starting internal PHY with FULL-DUPLEX\n");
2703 		} else {
2704 			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
2705 					   MII_GC_AUTOENB);
2706 			netdev_info(dev, "Starting internal PHY with HALF-DUPLEX\n");
2707 		}
2708 	}
2709 
2710 	/* Wait for 100 ms.  No reason in partiticular.
2711 	 */
2712 	tlan_set_timer(dev, msecs_to_jiffies(100), TLAN_TIMER_FINISH_RESET);
2713 
2714 }
2715 
2716 
2717 /*********************************************************************
2718  *
2719  *     tlan_phy_monitor
2720  *
2721  *     Returns:
2722  *	      None
2723  *
2724  *     Params:
2725  *	      data	     The device structure of this device.
2726  *
2727  *
2728  *     This function monitors PHY condition by reading the status
2729  *     register via the MII bus, controls LINK LED and notifies the
2730  *     kernel about link state.
2731  *
2732  *******************************************************************/
2733 
2734 static void tlan_phy_monitor(struct timer_list *t)
2735 {
2736 	struct tlan_priv *priv = timer_container_of(priv, t, media_timer);
2737 	struct net_device *dev = priv->dev;
2738 	u16     phy;
2739 	u16     phy_status;
2740 
2741 	phy = priv->phy[priv->phy_num];
2742 
2743 	/* Get PHY status register */
2744 	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &phy_status);
2745 
2746 	/* Check if link has been lost */
2747 	if (!(phy_status & MII_GS_LINK)) {
2748 		if (netif_carrier_ok(dev)) {
2749 			printk(KERN_DEBUG "TLAN: %s has lost link\n",
2750 			       dev->name);
2751 			tlan_dio_write8(dev->base_addr, TLAN_LED_REG, 0);
2752 			netif_carrier_off(dev);
2753 			if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) {
2754 				/* power down internal PHY */
2755 				u16 data = MII_GC_PDOWN | MII_GC_LOOPBK |
2756 					   MII_GC_ISOLATE;
2757 
2758 				tlan_mii_sync(dev->base_addr);
2759 				tlan_mii_write_reg(dev, priv->phy[0],
2760 						   MII_GEN_CTL, data);
2761 				/* set to external PHY */
2762 				priv->phy_num = 1;
2763 				/* restart autonegotiation */
2764 				tlan_set_timer(dev, msecs_to_jiffies(400),
2765 					       TLAN_TIMER_PHY_PDOWN);
2766 				return;
2767 			}
2768 		}
2769 	}
2770 
2771 	/* Link restablished? */
2772 	if ((phy_status & MII_GS_LINK) && !netif_carrier_ok(dev)) {
2773 		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
2774 		printk(KERN_DEBUG "TLAN: %s has reestablished link\n",
2775 		       dev->name);
2776 		netif_carrier_on(dev);
2777 	}
2778 	priv->media_timer.expires = jiffies + HZ;
2779 	add_timer(&priv->media_timer);
2780 }
2781 
2782 
2783 /*****************************************************************************
2784 ******************************************************************************
2785 
2786 ThunderLAN driver MII routines
2787 
2788 these routines are based on the information in chap. 2 of the
2789 "ThunderLAN Programmer's Guide", pp. 15-24.
2790 
2791 ******************************************************************************
2792 *****************************************************************************/
2793 
2794 
2795 /***************************************************************
2796  *	__tlan_mii_read_reg
2797  *
2798  *	Returns:
2799  *		false	if ack received ok
2800  *		true	if no ack received or other error
2801  *
2802  *	Parms:
2803  *		dev		The device structure containing
2804  *				The io address and interrupt count
2805  *				for this device.
2806  *		phy		The address of the PHY to be queried.
2807  *		reg		The register whose contents are to be
2808  *				retrieved.
2809  *		val		A pointer to a variable to store the
2810  *				retrieved value.
2811  *
2812  *	This function uses the TLAN's MII bus to retrieve the contents
2813  *	of a given register on a PHY.  It sends the appropriate info
2814  *	and then reads the 16-bit register value from the MII bus via
2815  *	the TLAN SIO register.
2816  *
2817  **************************************************************/
2818 
2819 static bool
2820 __tlan_mii_read_reg(struct net_device *dev, u16 phy, u16 reg, u16 *val)
2821 {
2822 	u8	nack;
2823 	u16	sio, tmp;
2824 	u32	i;
2825 	bool	err;
2826 	int	minten;
2827 	struct tlan_priv *priv = netdev_priv(dev);
2828 
2829 	lockdep_assert_held(&priv->lock);
2830 
2831 	err = false;
2832 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
2833 	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2834 
2835 	tlan_mii_sync(dev->base_addr);
2836 
2837 	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
2838 	if (minten)
2839 		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
2840 
2841 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
2842 	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* read  (10b) */
2843 	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
2844 	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
2845 
2846 
2847 	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);	/* change direction */
2848 
2849 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* clock idle bit */
2850 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2851 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* wait 300ns */
2852 
2853 	nack = tlan_get_bit(TLAN_NET_SIO_MDATA, sio);	/* check for ACK */
2854 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);		/* finish ACK */
2855 	if (nack) {					/* no ACK, so fake it */
2856 		for (i = 0; i < 16; i++) {
2857 			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2858 			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2859 		}
2860 		tmp = 0xffff;
2861 		err = true;
2862 	} else {					/* ACK, so read data */
2863 		for (tmp = 0, i = 0x8000; i; i >>= 1) {
2864 			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2865 			if (tlan_get_bit(TLAN_NET_SIO_MDATA, sio))
2866 				tmp |= i;
2867 			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2868 		}
2869 	}
2870 
2871 
2872 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* idle cycle */
2873 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2874 
2875 	if (minten)
2876 		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
2877 
2878 	*val = tmp;
2879 
2880 	return err;
2881 }
2882 
2883 static void tlan_mii_read_reg(struct net_device *dev, u16 phy, u16 reg,
2884 			      u16 *val)
2885 {
2886 	struct tlan_priv *priv = netdev_priv(dev);
2887 	unsigned long flags;
2888 
2889 	spin_lock_irqsave(&priv->lock, flags);
2890 	__tlan_mii_read_reg(dev, phy, reg, val);
2891 	spin_unlock_irqrestore(&priv->lock, flags);
2892 }
2893 
2894 /***************************************************************
2895  *	tlan_mii_send_data
2896  *
2897  *	Returns:
2898  *		Nothing
2899  *	Parms:
2900  *		base_port	The base IO port of the adapter	in
2901  *				question.
2902  *		dev		The address of the PHY to be queried.
2903  *		data		The value to be placed on the MII bus.
2904  *		num_bits	The number of bits in data that are to
2905  *				be placed on the MII bus.
2906  *
2907  *	This function sends on sequence of bits on the MII
2908  *	configuration bus.
2909  *
2910  **************************************************************/
2911 
2912 static void tlan_mii_send_data(u16 base_port, u32 data, unsigned num_bits)
2913 {
2914 	u16 sio;
2915 	u32 i;
2916 
2917 	if (num_bits == 0)
2918 		return;
2919 
2920 	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
2921 	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
2922 	tlan_set_bit(TLAN_NET_SIO_MTXEN, sio);
2923 
2924 	for (i = (0x1 << (num_bits - 1)); i; i >>= 1) {
2925 		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2926 		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
2927 		if (data & i)
2928 			tlan_set_bit(TLAN_NET_SIO_MDATA, sio);
2929 		else
2930 			tlan_clear_bit(TLAN_NET_SIO_MDATA, sio);
2931 		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2932 		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
2933 	}
2934 
2935 }
2936 
2937 
2938 
2939 
2940 /***************************************************************
2941  *	TLan_MiiSync
2942  *
2943  *	Returns:
2944  *		Nothing
2945  *	Parms:
2946  *		base_port	The base IO port of the adapter in
2947  *				question.
2948  *
2949  *	This functions syncs all PHYs in terms of the MII configuration
2950  *	bus.
2951  *
2952  **************************************************************/
2953 
2954 static void tlan_mii_sync(u16 base_port)
2955 {
2956 	int i;
2957 	u16 sio;
2958 
2959 	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
2960 	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
2961 
2962 	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);
2963 	for (i = 0; i < 32; i++) {
2964 		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
2965 		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
2966 	}
2967 
2968 }
2969 
2970 
2971 
2972 
2973 /***************************************************************
2974  *	__tlan_mii_write_reg
2975  *
2976  *	Returns:
2977  *		Nothing
2978  *	Parms:
2979  *		dev		The device structure for the device
2980  *				to write to.
2981  *		phy		The address of the PHY to be written to.
2982  *		reg		The register whose contents are to be
2983  *				written.
2984  *		val		The value to be written to the register.
2985  *
2986  *	This function uses the TLAN's MII bus to write the contents of a
2987  *	given register on a PHY.  It sends the appropriate info and then
2988  *	writes the 16-bit register value from the MII configuration bus
2989  *	via the TLAN SIO register.
2990  *
2991  **************************************************************/
2992 
2993 static void
2994 __tlan_mii_write_reg(struct net_device *dev, u16 phy, u16 reg, u16 val)
2995 {
2996 	u16	sio;
2997 	int	minten;
2998 	struct tlan_priv *priv = netdev_priv(dev);
2999 
3000 	lockdep_assert_held(&priv->lock);
3001 
3002 	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
3003 	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
3004 
3005 	tlan_mii_sync(dev->base_addr);
3006 
3007 	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
3008 	if (minten)
3009 		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
3010 
3011 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
3012 	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* write (01b) */
3013 	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
3014 	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
3015 
3016 	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* send ACK */
3017 	tlan_mii_send_data(dev->base_addr, val, 16);	/* send data */
3018 
3019 	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);	/* idle cycle */
3020 	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
3021 
3022 	if (minten)
3023 		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
3024 
3025 }
3026 
3027 static void
3028 tlan_mii_write_reg(struct net_device *dev, u16 phy, u16 reg, u16 val)
3029 {
3030 	struct tlan_priv *priv = netdev_priv(dev);
3031 	unsigned long flags;
3032 
3033 	spin_lock_irqsave(&priv->lock, flags);
3034 	__tlan_mii_write_reg(dev, phy, reg, val);
3035 	spin_unlock_irqrestore(&priv->lock, flags);
3036 }
3037 
3038 
3039 /*****************************************************************************
3040 ******************************************************************************
3041 
3042 ThunderLAN driver eeprom routines
3043 
3044 the Compaq netelligent 10 and 10/100 cards use a microchip 24C02A
3045 EEPROM.  these functions are based on information in microchip's
3046 data sheet.  I don't know how well this functions will work with
3047 other Eeproms.
3048 
3049 ******************************************************************************
3050 *****************************************************************************/
3051 
3052 
3053 /***************************************************************
3054  *	tlan_ee_send_start
3055  *
3056  *	Returns:
3057  *		Nothing
3058  *	Parms:
3059  *		io_base		The IO port base address for the
3060  *				TLAN device with the EEPROM to
3061  *				use.
3062  *
3063  *	This function sends a start cycle to an EEPROM attached
3064  *	to a TLAN chip.
3065  *
3066  **************************************************************/
3067 
3068 static void tlan_ee_send_start(u16 io_base)
3069 {
3070 	u16	sio;
3071 
3072 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3073 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3074 
3075 	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3076 	tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3077 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3078 	tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3079 	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3080 
3081 }
3082 
3083 
3084 
3085 
3086 /***************************************************************
3087  *	tlan_ee_send_byte
3088  *
3089  *	Returns:
3090  *		If the correct ack was received, 0, otherwise 1
3091  *	Parms:	io_base		The IO port base address for the
3092  *				TLAN device with the EEPROM to
3093  *				use.
3094  *		data		The 8 bits of information to
3095  *				send to the EEPROM.
3096  *		stop		If TLAN_EEPROM_STOP is passed, a
3097  *				stop cycle is sent after the
3098  *				byte is sent after the ack is
3099  *				read.
3100  *
3101  *	This function sends a byte on the serial EEPROM line,
3102  *	driving the clock to send each bit. The function then
3103  *	reverses transmission direction and reads an acknowledge
3104  *	bit.
3105  *
3106  **************************************************************/
3107 
3108 static int tlan_ee_send_byte(u16 io_base, u8 data, int stop)
3109 {
3110 	int	err;
3111 	u8	place;
3112 	u16	sio;
3113 
3114 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3115 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3116 
3117 	/* Assume clock is low, tx is enabled; */
3118 	for (place = 0x80; place != 0; place >>= 1) {
3119 		if (place & data)
3120 			tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3121 		else
3122 			tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3123 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3124 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3125 	}
3126 	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
3127 	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3128 	err = tlan_get_bit(TLAN_NET_SIO_EDATA, sio);
3129 	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3130 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3131 
3132 	if ((!err) && stop) {
3133 		/* STOP, raise data while clock is high */
3134 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3135 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3136 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3137 	}
3138 
3139 	return err;
3140 
3141 }
3142 
3143 
3144 
3145 
3146 /***************************************************************
3147  *	tlan_ee_receive_byte
3148  *
3149  *	Returns:
3150  *		Nothing
3151  *	Parms:
3152  *		io_base		The IO port base address for the
3153  *				TLAN device with the EEPROM to
3154  *				use.
3155  *		data		An address to a char to hold the
3156  *				data sent from the EEPROM.
3157  *		stop		If TLAN_EEPROM_STOP is passed, a
3158  *				stop cycle is sent after the
3159  *				byte is received, and no ack is
3160  *				sent.
3161  *
3162  *	This function receives 8 bits of data from the EEPROM
3163  *	over the serial link.  It then sends and ack bit, or no
3164  *	ack and a stop bit.  This function is used to retrieve
3165  *	data after the address of a byte in the EEPROM has been
3166  *	sent.
3167  *
3168  **************************************************************/
3169 
3170 static void tlan_ee_receive_byte(u16 io_base, u8 *data, int stop)
3171 {
3172 	u8  place;
3173 	u16 sio;
3174 
3175 	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
3176 	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
3177 	*data = 0;
3178 
3179 	/* Assume clock is low, tx is enabled; */
3180 	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
3181 	for (place = 0x80; place; place >>= 1) {
3182 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3183 		if (tlan_get_bit(TLAN_NET_SIO_EDATA, sio))
3184 			*data |= place;
3185 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3186 	}
3187 
3188 	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
3189 	if (!stop) {
3190 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio); /* ack = 0 */
3191 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3192 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3193 	} else {
3194 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);	/* no ack = 1 (?) */
3195 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3196 		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
3197 		/* STOP, raise data while clock is high */
3198 		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
3199 		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
3200 		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
3201 	}
3202 
3203 }
3204 
3205 
3206 
3207 
3208 /***************************************************************
3209  *	tlan_ee_read_byte
3210  *
3211  *	Returns:
3212  *		No error = 0, else, the stage at which the error
3213  *		occurred.
3214  *	Parms:
3215  *		io_base		The IO port base address for the
3216  *				TLAN device with the EEPROM to
3217  *				use.
3218  *		ee_addr		The address of the byte in the
3219  *				EEPROM whose contents are to be
3220  *				retrieved.
3221  *		data		An address to a char to hold the
3222  *				data obtained from the EEPROM.
3223  *
3224  *	This function reads a byte of information from an byte
3225  *	cell in the EEPROM.
3226  *
3227  **************************************************************/
3228 
3229 static int tlan_ee_read_byte(struct net_device *dev, u8 ee_addr, u8 *data)
3230 {
3231 	int err;
3232 	struct tlan_priv *priv = netdev_priv(dev);
3233 	unsigned long flags = 0;
3234 	int ret = 0;
3235 
3236 	spin_lock_irqsave(&priv->lock, flags);
3237 
3238 	tlan_ee_send_start(dev->base_addr);
3239 	err = tlan_ee_send_byte(dev->base_addr, 0xa0, TLAN_EEPROM_ACK);
3240 	if (err) {
3241 		ret = 1;
3242 		goto fail;
3243 	}
3244 	err = tlan_ee_send_byte(dev->base_addr, ee_addr, TLAN_EEPROM_ACK);
3245 	if (err) {
3246 		ret = 2;
3247 		goto fail;
3248 	}
3249 	tlan_ee_send_start(dev->base_addr);
3250 	err = tlan_ee_send_byte(dev->base_addr, 0xa1, TLAN_EEPROM_ACK);
3251 	if (err) {
3252 		ret = 3;
3253 		goto fail;
3254 	}
3255 	tlan_ee_receive_byte(dev->base_addr, data, TLAN_EEPROM_STOP);
3256 fail:
3257 	spin_unlock_irqrestore(&priv->lock, flags);
3258 
3259 	return ret;
3260 
3261 }
3262 
3263 
3264 
3265