tlan.c 85.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*******************************************************************************
 *
 *  Linux ThunderLAN Driver
 *
 *  tlan.c
 *  by James Banks
 *
 *  (C) 1997-1998 Caldera, Inc.
 *  (C) 1998 James Banks
 *  (C) 1999-2001 Torben Mathiasen
 *  (C) 2002 Samuel Chessman
 *
 *  This software may be used and distributed according to the terms
 *  of the GNU General Public License, incorporated herein by reference.
 *
 ** Useful (if not required) reading:
 *
 *		Texas Instruments, ThunderLAN Programmer's Guide,
 *			TI Literature Number SPWU013A
 *			available in PDF format from www.ti.com
 *		Level One, LXT901 and LXT970 Data Sheets
 *			available in PDF format from www.level1.com
 *		National Semiconductor, DP83840A Data Sheet
 *			available in PDF format from www.national.com
 *		Microchip Technology, 24C01A/02A/04A Data Sheet
 *			available in PDF format from www.microchip.com
 *
28
 ******************************************************************************/
L
Linus Torvalds 已提交
29

30 31
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

32
#include <linux/hardirq.h>
L
Linus Torvalds 已提交
33 34
#include <linux/module.h>
#include <linux/init.h>
35
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
36 37 38
#include <linux/ioport.h>
#include <linux/eisa.h>
#include <linux/pci.h>
39
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/mii.h>

#include "tlan.h"


/* For removing EISA devices */
51
static	struct net_device	*tlan_eisa_devices;
L
Linus Torvalds 已提交
52

53
static	int		tlan_devices_installed;
L
Linus Torvalds 已提交
54 55 56 57 58 59

/* Set speed, duplex and aui settings */
static  int aui[MAX_TLAN_BOARDS];
static  int duplex[MAX_TLAN_BOARDS];
static  int speed[MAX_TLAN_BOARDS];
static  int boards_found;
60 61 62 63
module_param_array(aui, int, NULL, 0);
module_param_array(duplex, int, NULL, 0);
module_param_array(speed, int, NULL, 0);
MODULE_PARM_DESC(aui, "ThunderLAN use AUI port(s) (0-1)");
64 65
MODULE_PARM_DESC(duplex,
		 "ThunderLAN duplex setting(s) (0-default, 1-half, 2-full)");
66
MODULE_PARM_DESC(speed, "ThunderLAN port speed setting(s) (0,10,100)");
L
Linus Torvalds 已提交
67 68 69 70 71 72 73

MODULE_AUTHOR("Maintainer: Samuel Chessman <chessman@tux.org>");
MODULE_DESCRIPTION("Driver for TI ThunderLAN based ethernet PCI adapters");
MODULE_LICENSE("GPL");

/* Turn on debugging. See Documentation/networking/tlan.txt for details */
static  int		debug;
74 75
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "ThunderLAN debug mask");
L
Linus Torvalds 已提交
76

77
static	const char tlan_signature[] = "TLAN";
S
Sakari Ailus 已提交
78
static  const char tlan_banner[] = "ThunderLAN driver v1.17\n";
L
Linus Torvalds 已提交
79 80 81
static  int tlan_have_pci;
static  int tlan_have_eisa;

82 83 84
static const char * const media[] = {
	"10BaseT-HD", "10BaseT-FD", "100baseTx-HD",
	"100BaseTx-FD", "100BaseT4", NULL
L
Linus Torvalds 已提交
85 86 87
};

static struct board {
88 89 90
	const char	*device_label;
	u32		flags;
	u16		addr_ofs;
L
Linus Torvalds 已提交
91 92
} board_info[] = {
	{ "Compaq Netelligent 10 T PCI UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
93 94
	{ "Compaq Netelligent 10/100 TX PCI UTP",
	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
L
Linus Torvalds 已提交
95
	{ "Compaq Integrated NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
S
Stephen Hemminger 已提交
96 97
	{ "Compaq NetFlex-3/P",
	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
L
Linus Torvalds 已提交
98
	{ "Compaq NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
S
Stephen Hemminger 已提交
99 100
	{ "Compaq Netelligent Integrated 10/100 TX UTP",
	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
101 102 103 104
	{ "Compaq Netelligent Dual 10/100 TX PCI UTP",
	  TLAN_ADAPTER_NONE, 0x83 },
	{ "Compaq Netelligent 10/100 TX Embedded UTP",
	  TLAN_ADAPTER_NONE, 0x83 },
L
Linus Torvalds 已提交
105
	{ "Olicom OC-2183/2185", TLAN_ADAPTER_USE_INTERN_10, 0x83 },
106 107 108 109
	{ "Olicom OC-2325", TLAN_ADAPTER_ACTIVITY_LED |
	  TLAN_ADAPTER_UNMANAGED_PHY, 0xf8 },
	{ "Olicom OC-2326", TLAN_ADAPTER_ACTIVITY_LED |
	  TLAN_ADAPTER_USE_INTERN_10, 0xf8 },
L
Linus Torvalds 已提交
110
	{ "Compaq Netelligent 10/100 TX UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
111
	{ "Compaq Netelligent 10 T/2 PCI UTP/coax", TLAN_ADAPTER_NONE, 0x83 },
S
Stephen Hemminger 已提交
112
	{ "Compaq NetFlex-3/E",
113
	  TLAN_ADAPTER_ACTIVITY_LED |	/* EISA card */
S
Stephen Hemminger 已提交
114
	  TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
115 116
	{ "Compaq NetFlex-3/E",
	  TLAN_ADAPTER_ACTIVITY_LED, 0x83 }, /* EISA card */
L
Linus Torvalds 已提交
117 118
};

119
static const struct pci_device_id tlan_pci_tbl[] = {
L
Linus Torvalds 已提交
120
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL10,
121
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
L
Linus Torvalds 已提交
122
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100,
123
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
L
Linus Torvalds 已提交
124
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3I,
125
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
L
Linus Torvalds 已提交
126
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_THUNDER,
127
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
L
Linus Torvalds 已提交
128
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3B,
129
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
L
Linus Torvalds 已提交
130
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100PI,
131
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
L
Linus Torvalds 已提交
132
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100D,
133
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
L
Linus Torvalds 已提交
134
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100I,
135
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
L
Linus Torvalds 已提交
136
	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2183,
137
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
L
Linus Torvalds 已提交
138
	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2325,
139
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
L
Linus Torvalds 已提交
140
	{ PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2326,
141
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
L
Linus Torvalds 已提交
142
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_100_WS_5100,
143
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
L
Linus Torvalds 已提交
144
	{ PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_T2,
145
	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
L
Linus Torvalds 已提交
146 147
	{ 0,}
};
148
MODULE_DEVICE_TABLE(pci, tlan_pci_tbl);
L
Linus Torvalds 已提交
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static void	tlan_eisa_probe(void);
static void	tlan_eisa_cleanup(void);
static int      tlan_init(struct net_device *);
static int	tlan_open(struct net_device *dev);
static netdev_tx_t tlan_start_tx(struct sk_buff *, struct net_device *);
static irqreturn_t tlan_handle_interrupt(int, void *);
static int	tlan_close(struct net_device *);
static struct	net_device_stats *tlan_get_stats(struct net_device *);
static void	tlan_set_multicast_list(struct net_device *);
static int	tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
static int      tlan_probe1(struct pci_dev *pdev, long ioaddr,
			    int irq, int rev, const struct pci_device_id *ent);
static void	tlan_tx_timeout(struct net_device *dev);
static void	tlan_tx_timeout_work(struct work_struct *work);
static int	tlan_init_one(struct pci_dev *pdev,
			      const struct pci_device_id *ent);

static u32	tlan_handle_tx_eof(struct net_device *, u16);
static u32	tlan_handle_stat_overflow(struct net_device *, u16);
static u32	tlan_handle_rx_eof(struct net_device *, u16);
static u32	tlan_handle_dummy(struct net_device *, u16);
static u32	tlan_handle_tx_eoc(struct net_device *, u16);
static u32	tlan_handle_status_check(struct net_device *, u16);
static u32	tlan_handle_rx_eoc(struct net_device *, u16);

175 176
static void	tlan_timer(struct timer_list *t);
static void	tlan_phy_monitor(struct timer_list *t);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

static void	tlan_reset_lists(struct net_device *);
static void	tlan_free_lists(struct net_device *);
static void	tlan_print_dio(u16);
static void	tlan_print_list(struct tlan_list *, char *, int);
static void	tlan_read_and_clear_stats(struct net_device *, int);
static void	tlan_reset_adapter(struct net_device *);
static void	tlan_finish_reset(struct net_device *);
static void	tlan_set_mac(struct net_device *, int areg, char *mac);

static void	tlan_phy_print(struct net_device *);
static void	tlan_phy_detect(struct net_device *);
static void	tlan_phy_power_down(struct net_device *);
static void	tlan_phy_power_up(struct net_device *);
static void	tlan_phy_reset(struct net_device *);
static void	tlan_phy_start_link(struct net_device *);
static void	tlan_phy_finish_auto_neg(struct net_device *);
L
Linus Torvalds 已提交
194 195

/*
196 197 198 199
  static int	tlan_phy_nop(struct net_device *);
  static int	tlan_phy_internal_check(struct net_device *);
  static int	tlan_phy_internal_service(struct net_device *);
  static int	tlan_phy_dp83840a_check(struct net_device *);
L
Linus Torvalds 已提交
200 201
*/

202 203 204 205
static bool	tlan_mii_read_reg(struct net_device *, u16, u16, u16 *);
static void	tlan_mii_send_data(u16, u32, unsigned);
static void	tlan_mii_sync(u16);
static void	tlan_mii_write_reg(struct net_device *, u16, u16, u16);
L
Linus Torvalds 已提交
206

207 208 209 210
static void	tlan_ee_send_start(u16);
static int	tlan_ee_send_byte(u16, u8, int);
static void	tlan_ee_receive_byte(u16, u8 *, int);
static int	tlan_ee_read_byte(struct net_device *, u8, u8 *);
L
Linus Torvalds 已提交
211 212


S
Stephen Hemminger 已提交
213
static inline void
214
tlan_store_skb(struct tlan_list *tag, struct sk_buff *skb)
L
Linus Torvalds 已提交
215 216
{
	unsigned long addr = (unsigned long)skb;
S
Stephen Hemminger 已提交
217 218
	tag->buffer[9].address = addr;
	tag->buffer[8].address = upper_32_bits(addr);
L
Linus Torvalds 已提交
219 220
}

S
Stephen Hemminger 已提交
221
static inline struct sk_buff *
222
tlan_get_skb(const struct tlan_list *tag)
L
Linus Torvalds 已提交
223
{
S
Stephen Hemminger 已提交
224 225
	unsigned long addr;

226
	addr = tag->buffer[9].address;
227
	addr |= ((unsigned long) tag->buffer[8].address << 16) << 16;
L
Linus Torvalds 已提交
228 229 230
	return (struct sk_buff *) addr;
}

231 232
static u32
(*tlan_int_vector[TLAN_INT_NUMBER_OF_INTS])(struct net_device *, u16) = {
233
	NULL,
234 235 236 237 238 239 240
	tlan_handle_tx_eof,
	tlan_handle_stat_overflow,
	tlan_handle_rx_eof,
	tlan_handle_dummy,
	tlan_handle_tx_eoc,
	tlan_handle_status_check,
	tlan_handle_rx_eoc
L
Linus Torvalds 已提交
241 242 243
};

static inline void
244
tlan_set_timer(struct net_device *dev, u32 ticks, u32 type)
L
Linus Torvalds 已提交
245
{
246
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
247
	unsigned long flags = 0;
248

L
Linus Torvalds 已提交
249 250
	if (!in_irq())
		spin_lock_irqsave(&priv->lock, flags);
251 252
	if (priv->timer.function != NULL &&
	    priv->timer_type != TLAN_TIMER_ACTIVITY) {
L
Linus Torvalds 已提交
253 254 255 256
		if (!in_irq())
			spin_unlock_irqrestore(&priv->lock, flags);
		return;
	}
257
	priv->timer.function = tlan_timer;
L
Linus Torvalds 已提交
258 259 260
	if (!in_irq())
		spin_unlock_irqrestore(&priv->lock, flags);

261 262
	priv->timer_set_at = jiffies;
	priv->timer_type = type;
L
Linus Torvalds 已提交
263
	mod_timer(&priv->timer, jiffies + ticks);
264

265
}
L
Linus Torvalds 已提交
266 267 268 269 270


/*****************************************************************************
******************************************************************************

271
ThunderLAN driver primary functions
L
Linus Torvalds 已提交
272

273
these functions are more or less common to all linux network drivers.
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281

******************************************************************************
*****************************************************************************/





282 283 284 285 286 287 288 289 290 291 292 293 294 295
/***************************************************************
 *	tlan_remove_one
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		None
 *
 *	Goes through the TLanDevices list and frees the device
 *	structs and memory associated with each device (lists
 *	and buffers).  It also ureserves the IO port regions
 *	associated with this device.
 *
 **************************************************************/
L
Linus Torvalds 已提交
296 297


B
Bill Pemberton 已提交
298
static void tlan_remove_one(struct pci_dev *pdev)
L
Linus Torvalds 已提交
299
{
300 301
	struct net_device *dev = pci_get_drvdata(pdev);
	struct tlan_priv	*priv = netdev_priv(dev);
302

303
	unregister_netdev(dev);
L
Linus Torvalds 已提交
304

305 306 307 308
	if (priv->dma_storage) {
		pci_free_consistent(priv->pci_dev,
				    priv->dma_size, priv->dma_storage,
				    priv->dma_storage_dma);
L
Linus Torvalds 已提交
309 310 311 312 313
	}

#ifdef CONFIG_PCI
	pci_release_regions(pdev);
#endif
314

315
	free_netdev(dev);
316

D
Devendra Naga 已提交
317
	cancel_work_sync(&priv->tlan_tqueue);
318
}
L
Linus Torvalds 已提交
319

S
Sakari Ailus 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
static void tlan_start(struct net_device *dev)
{
	tlan_reset_lists(dev);
	/* NOTE: It might not be necessary to read the stats before a
	   reset if you don't care what the values are.
	*/
	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
	tlan_reset_adapter(dev);
	netif_wake_queue(dev);
}

static void tlan_stop(struct net_device *dev)
{
	struct tlan_priv *priv = netdev_priv(dev);

O
Ondrej Zary 已提交
335
	del_timer_sync(&priv->media_timer);
S
Sakari Ailus 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	tlan_read_and_clear_stats(dev, TLAN_RECORD);
	outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
	/* Reset and power down phy */
	tlan_reset_adapter(dev);
	if (priv->timer.function != NULL) {
		del_timer_sync(&priv->timer);
		priv->timer.function = NULL;
	}
}

#ifdef CONFIG_PM

static int tlan_suspend(struct pci_dev *pdev, pm_message_t state)
{
	struct net_device *dev = pci_get_drvdata(pdev);

	if (netif_running(dev))
		tlan_stop(dev);

	netif_device_detach(dev);
	pci_save_state(pdev);
	pci_disable_device(pdev);
	pci_wake_from_d3(pdev, false);
	pci_set_power_state(pdev, PCI_D3hot);

	return 0;
}

static int tlan_resume(struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
O
Ondrej Zary 已提交
367
	int rc = pci_enable_device(pdev);
S
Sakari Ailus 已提交
368

O
Ondrej Zary 已提交
369 370
	if (rc)
		return rc;
S
Sakari Ailus 已提交
371
	pci_restore_state(pdev);
372
	pci_enable_wake(pdev, PCI_D0, 0);
S
Sakari Ailus 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	netif_device_attach(dev);

	if (netif_running(dev))
		tlan_start(dev);

	return 0;
}

#else /* CONFIG_PM */

#define tlan_suspend   NULL
#define tlan_resume    NULL

#endif /* CONFIG_PM */


L
Linus Torvalds 已提交
389 390 391 392
static struct pci_driver tlan_driver = {
	.name		= "tlan",
	.id_table	= tlan_pci_tbl,
	.probe		= tlan_init_one,
B
Bill Pemberton 已提交
393
	.remove		= tlan_remove_one,
S
Sakari Ailus 已提交
394 395
	.suspend	= tlan_suspend,
	.resume		= tlan_resume,
L
Linus Torvalds 已提交
396 397 398 399
};

static int __init tlan_probe(void)
{
400
	int rc = -ENODEV;
401

402
	pr_info("%s", tlan_banner);
403

L
Linus Torvalds 已提交
404
	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting PCI Probe....\n");
405

L
Linus Torvalds 已提交
406 407
	/* Use new style PCI probing. Now the kernel will
	   do most of this for us */
408 409 410
	rc = pci_register_driver(&tlan_driver);

	if (rc != 0) {
411
		pr_err("Could not register pci driver\n");
412 413
		goto err_out_pci_free;
	}
L
Linus Torvalds 已提交
414 415

	TLAN_DBG(TLAN_DEBUG_PROBE, "Starting EISA Probe....\n");
416
	tlan_eisa_probe();
417

418 419 420
	pr_info("%d device%s installed, PCI: %d  EISA: %d\n",
		tlan_devices_installed, tlan_devices_installed == 1 ? "" : "s",
		tlan_have_pci, tlan_have_eisa);
L
Linus Torvalds 已提交
421

422
	if (tlan_devices_installed == 0) {
423 424
		rc = -ENODEV;
		goto  err_out_pci_unreg;
L
Linus Torvalds 已提交
425 426
	}
	return 0;
427 428 429 430 431

err_out_pci_unreg:
	pci_unregister_driver(&tlan_driver);
err_out_pci_free:
	return rc;
L
Linus Torvalds 已提交
432
}
433

L
Linus Torvalds 已提交
434

B
Bill Pemberton 已提交
435
static int tlan_init_one(struct pci_dev *pdev,
436
				   const struct pci_device_id *ent)
L
Linus Torvalds 已提交
437
{
438
	return tlan_probe1(pdev, -1, -1, 0, ent);
L
Linus Torvalds 已提交
439 440 441 442
}


/*
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
***************************************************************
*	tlan_probe1
*
*	Returns:
*		0 on success, error code on error
*	Parms:
*		none
*
*	The name is lower case to fit in with all the rest of
*	the netcard_probe names.  This function looks for
*	another TLan based adapter, setting it up with the
*	allocated device struct if one is found.
*	tlan_probe has been ported to the new net API and
*	now allocates its own device structure. This function
*	is also used by modules.
*
**************************************************************/

461 462
static int tlan_probe1(struct pci_dev *pdev, long ioaddr, int irq, int rev,
		       const struct pci_device_id *ent)
L
Linus Torvalds 已提交
463 464 465
{

	struct net_device  *dev;
466
	struct tlan_priv  *priv;
L
Linus Torvalds 已提交
467 468 469
	u16		   device_id;
	int		   reg, rc = -ENODEV;

470
#ifdef CONFIG_PCI
L
Linus Torvalds 已提交
471 472 473 474 475
	if (pdev) {
		rc = pci_enable_device(pdev);
		if (rc)
			return rc;

476
		rc = pci_request_regions(pdev, tlan_signature);
L
Linus Torvalds 已提交
477
		if (rc) {
478
			pr_err("Could not reserve IO regions\n");
L
Linus Torvalds 已提交
479 480 481
			goto err_out;
		}
	}
482
#endif  /*  CONFIG_PCI  */
L
Linus Torvalds 已提交
483

484
	dev = alloc_etherdev(sizeof(struct tlan_priv));
L
Linus Torvalds 已提交
485 486 487 488 489
	if (dev == NULL) {
		rc = -ENOMEM;
		goto err_out_regions;
	}
	SET_NETDEV_DEV(dev, &pdev->dev);
490

L
Linus Torvalds 已提交
491 492
	priv = netdev_priv(dev);

493
	priv->pci_dev = pdev;
D
David Howells 已提交
494
	priv->dev = dev;
495

L
Linus Torvalds 已提交
496 497
	/* Is this a PCI device? */
	if (pdev) {
498
		u32		   pci_io_base = 0;
L
Linus Torvalds 已提交
499 500 501

		priv->adapter = &board_info[ent->driver_data];

502
		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
L
Linus Torvalds 已提交
503
		if (rc) {
504
			pr_err("No suitable PCI mapping available\n");
L
Linus Torvalds 已提交
505 506 507
			goto err_out_free_dev;
		}

508
		for (reg = 0; reg <= 5; reg++) {
L
Linus Torvalds 已提交
509 510
			if (pci_resource_flags(pdev, reg) & IORESOURCE_IO) {
				pci_io_base = pci_resource_start(pdev, reg);
511 512 513
				TLAN_DBG(TLAN_DEBUG_GNRL,
					 "IO mapping is available at %x.\n",
					 pci_io_base);
L
Linus Torvalds 已提交
514 515 516 517
				break;
			}
		}
		if (!pci_io_base) {
518
			pr_err("No IO mappings available\n");
L
Linus Torvalds 已提交
519 520 521
			rc = -EIO;
			goto err_out_free_dev;
		}
522

L
Linus Torvalds 已提交
523 524
		dev->base_addr = pci_io_base;
		dev->irq = pdev->irq;
525
		priv->adapter_rev = pdev->revision;
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533
		pci_set_master(pdev);
		pci_set_drvdata(pdev, dev);

	} else	{     /* EISA card */
		/* This is a hack. We need to know which board structure
		 * is suited for this adapter */
		device_id = inw(ioaddr + EISA_ID2);
		if (device_id == 0x20F1) {
534 535
			priv->adapter = &board_info[13]; /* NetFlex-3/E */
			priv->adapter_rev = 23;		/* TLAN 2.3 */
L
Linus Torvalds 已提交
536 537
		} else {
			priv->adapter = &board_info[14];
538
			priv->adapter_rev = 10;		/* TLAN 1.0 */
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546
		}
		dev->base_addr = ioaddr;
		dev->irq = irq;
	}

	/* Kernel parameters */
	if (dev->mem_start) {
		priv->aui    = dev->mem_start & 0x01;
S
Stephen Hemminger 已提交
547 548 549 550
		priv->duplex = ((dev->mem_start & 0x06) == 0x06) ? 0
			: (dev->mem_start & 0x06) >> 1;
		priv->speed  = ((dev->mem_start & 0x18) == 0x18) ? 0
			: (dev->mem_start & 0x18) >> 3;
551

552
		if (priv->speed == 0x1)
L
Linus Torvalds 已提交
553
			priv->speed = TLAN_SPEED_10;
554
		else if (priv->speed == 0x2)
L
Linus Torvalds 已提交
555
			priv->speed = TLAN_SPEED_100;
556

L
Linus Torvalds 已提交
557 558 559 560 561 562 563
		debug = priv->debug = dev->mem_end;
	} else {
		priv->aui    = aui[boards_found];
		priv->speed  = speed[boards_found];
		priv->duplex = duplex[boards_found];
		priv->debug = debug;
	}
564

L
Linus Torvalds 已提交
565 566
	/* This will be used when we get an adapter error from
	 * within our irq handler */
567
	INIT_WORK(&priv->tlan_tqueue, tlan_tx_timeout_work);
L
Linus Torvalds 已提交
568 569

	spin_lock_init(&priv->lock);
570

571
	rc = tlan_init(dev);
L
Linus Torvalds 已提交
572
	if (rc) {
573
		pr_err("Could not set up device\n");
L
Linus Torvalds 已提交
574 575 576 577 578
		goto err_out_free_dev;
	}

	rc = register_netdev(dev);
	if (rc) {
579
		pr_err("Could not register device\n");
L
Linus Torvalds 已提交
580 581 582
		goto err_out_uninit;
	}

583

584
	tlan_devices_installed++;
L
Linus Torvalds 已提交
585
	boards_found++;
586

L
Linus Torvalds 已提交
587 588 589 590
	/* pdev is NULL if this is an EISA device */
	if (pdev)
		tlan_have_pci++;
	else {
591 592
		priv->next_device = tlan_eisa_devices;
		tlan_eisa_devices = dev;
L
Linus Torvalds 已提交
593 594
		tlan_have_eisa++;
	}
595

596 597 598 599 600
	netdev_info(dev, "irq=%2d, io=%04x, %s, Rev. %d\n",
		    (int)dev->irq,
		    (int)dev->base_addr,
		    priv->adapter->device_label,
		    priv->adapter_rev);
L
Linus Torvalds 已提交
601 602 603
	return 0;

err_out_uninit:
604 605
	pci_free_consistent(priv->pci_dev, priv->dma_size, priv->dma_storage,
			    priv->dma_storage_dma);
L
Linus Torvalds 已提交
606 607 608 609 610 611 612
err_out_free_dev:
	free_netdev(dev);
err_out_regions:
#ifdef CONFIG_PCI
	if (pdev)
		pci_release_regions(pdev);
err_out:
613
#endif
L
Linus Torvalds 已提交
614 615 616 617 618 619
	if (pdev)
		pci_disable_device(pdev);
	return rc;
}


620
static void tlan_eisa_cleanup(void)
L
Linus Torvalds 已提交
621 622
{
	struct net_device *dev;
623
	struct tlan_priv *priv;
624

625 626
	while (tlan_have_eisa) {
		dev = tlan_eisa_devices;
L
Linus Torvalds 已提交
627
		priv = netdev_priv(dev);
628 629 630 631
		if (priv->dma_storage) {
			pci_free_consistent(priv->pci_dev, priv->dma_size,
					    priv->dma_storage,
					    priv->dma_storage_dma);
L
Linus Torvalds 已提交
632
		}
633 634 635 636
		release_region(dev->base_addr, 0x10);
		unregister_netdev(dev);
		tlan_eisa_devices = priv->next_device;
		free_netdev(dev);
L
Linus Torvalds 已提交
637 638 639
		tlan_have_eisa--;
	}
}
640 641


L
Linus Torvalds 已提交
642 643 644 645 646
static void __exit tlan_exit(void)
{
	pci_unregister_driver(&tlan_driver);

	if (tlan_have_eisa)
647
		tlan_eisa_cleanup();
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657

}


/* Module loading/unloading */
module_init(tlan_probe);
module_exit(tlan_exit);



658 659 660 661 662 663 664 665 666 667 668 669
/**************************************************************
 *	tlan_eisa_probe
 *
 *	Returns: 0 on success, 1 otherwise
 *
 *	Parms:	 None
 *
 *
 *	This functions probes for EISA devices and calls
 *	TLan_probe1 when one is found.
 *
 *************************************************************/
L
Linus Torvalds 已提交
670

671
static void  __init tlan_eisa_probe(void)
L
Linus Torvalds 已提交
672
{
673 674 675
	long	ioaddr;
	int	rc = -ENODEV;
	int	irq;
L
Linus Torvalds 已提交
676 677
	u16	device_id;

678
	if (!EISA_bus) {
L
Linus Torvalds 已提交
679 680 681
		TLAN_DBG(TLAN_DEBUG_PROBE, "No EISA bus present\n");
		return;
	}
682

L
Linus Torvalds 已提交
683 684
	/* Loop through all slots of the EISA bus */
	for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
685

686 687 688 689
		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
			 (int) ioaddr + 0xc80, inw(ioaddr + EISA_ID));
		TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
			 (int) ioaddr + 0xc82, inw(ioaddr + EISA_ID2));
L
Linus Torvalds 已提交
690 691


692 693 694 695
		TLAN_DBG(TLAN_DEBUG_PROBE,
			 "Probing for EISA adapter at IO: 0x%4x : ",
			 (int) ioaddr);
		if (request_region(ioaddr, 0x10, tlan_signature) == NULL)
L
Linus Torvalds 已提交
696 697
			goto out;

698
		if (inw(ioaddr + EISA_ID) != 0x110E) {
L
Linus Torvalds 已提交
699 700 701
			release_region(ioaddr, 0x10);
			goto out;
		}
702

L
Linus Torvalds 已提交
703
		device_id = inw(ioaddr + EISA_ID2);
704
		if (device_id !=  0x20F1 && device_id != 0x40F1) {
705
			release_region(ioaddr, 0x10);
L
Linus Torvalds 已提交
706 707
			goto out;
		}
708

709 710 711
		/* check if adapter is enabled */
		if (inb(ioaddr + EISA_CR) != 0x1) {
			release_region(ioaddr, 0x10);
L
Linus Torvalds 已提交
712 713
			goto out2;
		}
714 715

		if (debug == 0x10)
716
			pr_info("Found one\n");
L
Linus Torvalds 已提交
717 718 719


		/* Get irq from board */
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
		switch (inb(ioaddr + 0xcc0)) {
		case(0x10):
			irq = 5;
			break;
		case(0x20):
			irq = 9;
			break;
		case(0x40):
			irq = 10;
			break;
		case(0x80):
			irq = 11;
			break;
		default:
			goto out;
735 736 737
		}


L
Linus Torvalds 已提交
738
		/* Setup the newly found eisa adapter */
739 740
		rc = tlan_probe1(NULL, ioaddr, irq,
				 12, NULL);
L
Linus Torvalds 已提交
741
		continue;
742

743 744
out:
		if (debug == 0x10)
745
			pr_info("None found\n");
746
		continue;
L
Linus Torvalds 已提交
747

748 749
out2:
		if (debug == 0x10)
750
			pr_info("Card found but it is not enabled, skipping\n");
751
		continue;
752

L
Linus Torvalds 已提交
753 754
	}

755
}
L
Linus Torvalds 已提交
756 757

#ifdef CONFIG_NET_POLL_CONTROLLER
758
static void tlan_poll(struct net_device *dev)
L
Linus Torvalds 已提交
759 760
{
	disable_irq(dev->irq);
761
	tlan_handle_interrupt(dev->irq, dev);
L
Linus Torvalds 已提交
762 763 764 765
	enable_irq(dev->irq);
}
#endif

766 767 768 769 770 771
static const struct net_device_ops tlan_netdev_ops = {
	.ndo_open		= tlan_open,
	.ndo_stop		= tlan_close,
	.ndo_start_xmit		= tlan_start_tx,
	.ndo_tx_timeout		= tlan_tx_timeout,
	.ndo_get_stats		= tlan_get_stats,
772
	.ndo_set_rx_mode	= tlan_set_multicast_list,
773 774
	.ndo_do_ioctl		= tlan_ioctl,
	.ndo_set_mac_address	= eth_mac_addr,
775 776
	.ndo_validate_addr	= eth_validate_addr,
#ifdef CONFIG_NET_POLL_CONTROLLER
777
	.ndo_poll_controller	 = tlan_poll,
778 779
#endif
};
780

O
Ondrej Zary 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
static void tlan_get_drvinfo(struct net_device *dev,
			     struct ethtool_drvinfo *info)
{
	struct tlan_priv *priv = netdev_priv(dev);

	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
	if (priv->pci_dev)
		strlcpy(info->bus_info, pci_name(priv->pci_dev),
			sizeof(info->bus_info));
	else
		strlcpy(info->bus_info, "EISA",	sizeof(info->bus_info));
}

static int tlan_get_eeprom_len(struct net_device *dev)
{
	return TLAN_EEPROM_SIZE;
}
L
Linus Torvalds 已提交
798

O
Ondrej Zary 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
static int tlan_get_eeprom(struct net_device *dev,
			   struct ethtool_eeprom *eeprom, u8 *data)
{
	int i;

	for (i = 0; i < TLAN_EEPROM_SIZE; i++)
		if (tlan_ee_read_byte(dev, i, &data[i]))
			return -EIO;

	return 0;
}

static const struct ethtool_ops tlan_ethtool_ops = {
	.get_drvinfo	= tlan_get_drvinfo,
	.get_link	= ethtool_op_get_link,
	.get_eeprom_len	= tlan_get_eeprom_len,
	.get_eeprom	= tlan_get_eeprom,
};
L
Linus Torvalds 已提交
817

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
/***************************************************************
 *	tlan_init
 *
 *	Returns:
 *		0 on success, error code otherwise.
 *	Parms:
 *		dev	The structure of the device to be
 *			init'ed.
 *
 *	This function completes the initialization of the
 *	device structure and driver.  It reserves the IO
 *	addresses, allocates memory for the lists and bounce
 *	buffers, retrieves the MAC address from the eeprom
 *	and assignes the device's methods.
 *
 **************************************************************/

static int tlan_init(struct net_device *dev)
L
Linus Torvalds 已提交
836 837
{
	int		dma_size;
838
	int		err;
L
Linus Torvalds 已提交
839
	int		i;
840
	struct tlan_priv	*priv;
L
Linus Torvalds 已提交
841 842

	priv = netdev_priv(dev);
843

844 845 846 847 848 849 850 851
	dma_size = (TLAN_NUM_RX_LISTS + TLAN_NUM_TX_LISTS)
		* (sizeof(struct tlan_list));
	priv->dma_storage = pci_alloc_consistent(priv->pci_dev,
						 dma_size,
						 &priv->dma_storage_dma);
	priv->dma_size = dma_size;

	if (priv->dma_storage == NULL) {
852
		pr_err("Could not allocate lists and buffers for %s\n",
853
		       dev->name);
L
Linus Torvalds 已提交
854 855
		return -ENOMEM;
	}
856 857 858 859 860 861 862
	memset(priv->dma_storage, 0, dma_size);
	priv->rx_list = (struct tlan_list *)
		ALIGN((unsigned long)priv->dma_storage, 8);
	priv->rx_list_dma = ALIGN(priv->dma_storage_dma, 8);
	priv->tx_list = priv->rx_list + TLAN_NUM_RX_LISTS;
	priv->tx_list_dma =
		priv->rx_list_dma + sizeof(struct tlan_list)*TLAN_NUM_RX_LISTS;
S
Stephen Hemminger 已提交
863

L
Linus Torvalds 已提交
864
	err = 0;
865
	for (i = 0; i < ETH_ALEN; i++)
866 867 868 869
		err |= tlan_ee_read_byte(dev,
					 (u8) priv->adapter->addr_ofs + i,
					 (u8 *) &dev->dev_addr[i]);
	if (err) {
870 871
		pr_err("%s: Error reading MAC from eeprom: %d\n",
		       dev->name, err);
L
Linus Torvalds 已提交
872
	}
873 874 875 876 877 878 879 880
	/* Olicom OC-2325/OC-2326 have the address byte-swapped */
	if (priv->adapter->addr_ofs == 0xf8) {
		for (i = 0; i < ETH_ALEN; i += 2) {
			char tmp = dev->dev_addr[i];
			dev->dev_addr[i] = dev->dev_addr[i + 1];
			dev->dev_addr[i + 1] = tmp;
		}
	}
L
Linus Torvalds 已提交
881 882 883 884

	netif_carrier_off(dev);

	/* Device methods */
885
	dev->netdev_ops = &tlan_netdev_ops;
O
Ondrej Zary 已提交
886
	dev->ethtool_ops = &tlan_ethtool_ops;
L
Linus Torvalds 已提交
887 888 889 890
	dev->watchdog_timeo = TX_TIMEOUT;

	return 0;

891
}
L
Linus Torvalds 已提交
892 893 894 895




896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
/***************************************************************
 *	tlan_open
 *
 *	Returns:
 *		0 on success, error code otherwise.
 *	Parms:
 *		dev	Structure of device to be opened.
 *
 *	This routine puts the driver and TLAN adapter in a
 *	state where it is ready to send and receive packets.
 *	It allocates the IRQ, resets and brings the adapter
 *	out of reset, and allows interrupts.  It also delays
 *	the startup for autonegotiation or sends a Rx GO
 *	command to the adapter, as appropriate.
 *
 **************************************************************/
L
Linus Torvalds 已提交
912

913
static int tlan_open(struct net_device *dev)
L
Linus Torvalds 已提交
914
{
915
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
916
	int		err;
917

918 919 920
	priv->tlan_rev = tlan_dio_read8(dev->base_addr, TLAN_DEF_REVISION);
	err = request_irq(dev->irq, tlan_handle_interrupt, IRQF_SHARED,
			  dev->name, dev);
921

922
	if (err) {
923 924
		netdev_err(dev, "Cannot open because IRQ %d is already in use\n",
			   dev->irq);
L
Linus Torvalds 已提交
925 926
		return err;
	}
927

928 929
	timer_setup(&priv->timer, NULL, 0);
	timer_setup(&priv->media_timer, tlan_phy_monitor, 0);
930

S
Sakari Ailus 已提交
931
	tlan_start(dev);
L
Linus Torvalds 已提交
932

933 934
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Opened.  TLAN Chip Rev: %x\n",
		 dev->name, priv->tlan_rev);
L
Linus Torvalds 已提交
935 936 937

	return 0;

938
}
L
Linus Torvalds 已提交
939 940 941



942 943 944 945 946 947 948 949 950 951 952 953 954 955
/**************************************************************
 *	tlan_ioctl
 *
 *	Returns:
 *		0 on success, error code otherwise
 *	Params:
 *		dev	structure of device to receive ioctl.
 *
 *		rq	ifreq structure to hold userspace data.
 *
 *		cmd	ioctl command.
 *
 *
 *************************************************************/
L
Linus Torvalds 已提交
956

957
static int tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
L
Linus Torvalds 已提交
958
{
959
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
960
	struct mii_ioctl_data *data = if_mii(rq);
961
	u32 phy   = priv->phy[priv->phy_num];
962

963
	if (!priv->phy_online)
L
Linus Torvalds 已提交
964 965
		return -EAGAIN;

966 967 968
	switch (cmd) {
	case SIOCGMIIPHY:		/* get address of MII PHY in use. */
		data->phy_id = phy;
L
Linus Torvalds 已提交
969 970


971 972 973 974
	case SIOCGMIIREG:		/* read MII PHY register. */
		tlan_mii_read_reg(dev, data->phy_id & 0x1f,
				  data->reg_num & 0x1f, &data->val_out);
		return 0;
975

L
Linus Torvalds 已提交
976

977 978 979 980 981 982
	case SIOCSMIIREG:		/* write MII PHY register. */
		tlan_mii_write_reg(dev, data->phy_id & 0x1f,
				   data->reg_num & 0x1f, data->val_in);
		return 0;
	default:
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
983
	}
984
}
L
Linus Torvalds 已提交
985 986


987 988 989 990 991 992 993 994 995 996
/***************************************************************
 *	tlan_tx_timeout
 *
 *	Returns: nothing
 *
 *	Params:
 *		dev	structure of device which timed out
 *			during transmit.
 *
 **************************************************************/
L
Linus Torvalds 已提交
997

998
static void tlan_tx_timeout(struct net_device *dev)
L
Linus Torvalds 已提交
999
{
1000

1001
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Transmit timed out.\n", dev->name);
1002

L
Linus Torvalds 已提交
1003
	/* Ok so we timed out, lets see what we can do about it...*/
1004 1005 1006 1007
	tlan_free_lists(dev);
	tlan_reset_lists(dev);
	tlan_read_and_clear_stats(dev, TLAN_IGNORE);
	tlan_reset_adapter(dev);
1008
	netif_trans_update(dev); /* prevent tx timeout */
1009
	netif_wake_queue(dev);
L
Linus Torvalds 已提交
1010 1011

}
1012

L
Linus Torvalds 已提交
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022
/***************************************************************
 *	tlan_tx_timeout_work
 *
 *	Returns: nothing
 *
 *	Params:
 *		work	work item of device which timed out
 *
 **************************************************************/
D
David Howells 已提交
1023

1024
static void tlan_tx_timeout_work(struct work_struct *work)
D
David Howells 已提交
1025
{
1026 1027
	struct tlan_priv	*priv =
		container_of(work, struct tlan_priv, tlan_tqueue);
D
David Howells 已提交
1028

1029
	tlan_tx_timeout(priv->dev);
D
David Howells 已提交
1030 1031 1032
}


L
Linus Torvalds 已提交
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
/***************************************************************
 *	tlan_start_tx
 *
 *	Returns:
 *		0 on success, non-zero on failure.
 *	Parms:
 *		skb	A pointer to the sk_buff containing the
 *			frame to be sent.
 *		dev	The device to send the data on.
 *
 *	This function adds a frame to the Tx list to be sent
 *	ASAP.  First it	verifies that the adapter is ready and
 *	there is room in the queue.  Then it sets up the next
 *	available list, copies the frame to the	corresponding
 *	buffer.  If the adapter Tx channel is idle, it gives
 *	the adapter a Tx Go command on the list, otherwise it
 *	sets the forward address of the previous list to point
 *	to this one.  Then it frees the sk_buff.
 *
 **************************************************************/

static netdev_tx_t tlan_start_tx(struct sk_buff *skb, struct net_device *dev)
L
Linus Torvalds 已提交
1056
{
1057
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1058
	dma_addr_t	tail_list_phys;
1059
	struct tlan_list	*tail_list;
L
Linus Torvalds 已提交
1060
	unsigned long	flags;
1061
	unsigned int    txlen;
L
Linus Torvalds 已提交
1062

1063 1064 1065
	if (!priv->phy_online) {
		TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s PHY is not ready\n",
			 dev->name);
L
Linus Torvalds 已提交
1066
		dev_kfree_skb_any(skb);
1067
		return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1068 1069
	}

1070
	if (skb_padto(skb, TLAN_MIN_FRAME_SIZE))
1071
		return NETDEV_TX_OK;
1072
	txlen = max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE);
1073

1074 1075 1076
	tail_list = priv->tx_list + priv->tx_tail;
	tail_list_phys =
		priv->tx_list_dma + sizeof(struct tlan_list)*priv->tx_tail;
1077

1078 1079 1080 1081
	if (tail_list->c_stat != TLAN_CSTAT_UNUSED) {
		TLAN_DBG(TLAN_DEBUG_TX,
			 "TRANSMIT:  %s is busy (Head=%d Tail=%d)\n",
			 dev->name, priv->tx_head, priv->tx_tail);
L
Linus Torvalds 已提交
1082
		netif_stop_queue(dev);
1083
		priv->tx_busy_count++;
1084
		return NETDEV_TX_BUSY;
L
Linus Torvalds 已提交
1085 1086 1087 1088
	}

	tail_list->forward = 0;

1089
	tail_list->buffer[0].address = pci_map_single(priv->pci_dev,
1090 1091
						      skb->data, txlen,
						      PCI_DMA_TODEVICE);
1092
	tlan_store_skb(tail_list, skb);
L
Linus Torvalds 已提交
1093

1094
	tail_list->frame_size = (u16) txlen;
1095
	tail_list->buffer[0].count = TLAN_LAST_BUFFER | (u32) txlen;
1096 1097
	tail_list->buffer[1].count = 0;
	tail_list->buffer[1].address = 0;
L
Linus Torvalds 已提交
1098 1099

	spin_lock_irqsave(&priv->lock, flags);
1100 1101 1102 1103 1104 1105 1106 1107
	tail_list->c_stat = TLAN_CSTAT_READY;
	if (!priv->tx_in_progress) {
		priv->tx_in_progress = 1;
		TLAN_DBG(TLAN_DEBUG_TX,
			 "TRANSMIT:  Starting TX on buffer %d\n",
			 priv->tx_tail);
		outl(tail_list_phys, dev->base_addr + TLAN_CH_PARM);
		outl(TLAN_HC_GO, dev->base_addr + TLAN_HOST_CMD);
L
Linus Torvalds 已提交
1108
	} else {
1109 1110 1111 1112 1113
		TLAN_DBG(TLAN_DEBUG_TX,
			 "TRANSMIT:  Adding buffer %d to TX channel\n",
			 priv->tx_tail);
		if (priv->tx_tail == 0) {
			(priv->tx_list + (TLAN_NUM_TX_LISTS - 1))->forward
S
Stephen Hemminger 已提交
1114
				= tail_list_phys;
L
Linus Torvalds 已提交
1115
		} else {
1116
			(priv->tx_list + (priv->tx_tail - 1))->forward
S
Stephen Hemminger 已提交
1117
				= tail_list_phys;
L
Linus Torvalds 已提交
1118 1119 1120 1121
		}
	}
	spin_unlock_irqrestore(&priv->lock, flags);

1122
	CIRC_INC(priv->tx_tail, TLAN_NUM_TX_LISTS);
L
Linus Torvalds 已提交
1123

1124
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1125

1126
}
L
Linus Torvalds 已提交
1127 1128 1129 1130




1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
/***************************************************************
 *	tlan_handle_interrupt
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		irq	The line on which the interrupt
 *			occurred.
 *		dev_id	A pointer to the device assigned to
 *			this irq line.
 *
 *	This function handles an interrupt generated by its
 *	assigned TLAN adapter.  The function deactivates
 *	interrupts on its adapter, records the type of
 *	interrupt, executes the appropriate subhandler, and
 *	acknowdges the interrupt to the adapter (thus
 *	re-enabling adapter interrupts.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1150

1151
static irqreturn_t tlan_handle_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1152
{
1153
	struct net_device	*dev = dev_id;
1154
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1155
	u16		host_int;
1156
	u16		type;
L
Linus Torvalds 已提交
1157 1158 1159

	spin_lock(&priv->lock);

1160 1161 1162
	host_int = inw(dev->base_addr + TLAN_HOST_INT);
	type = (host_int & TLAN_HI_IT_MASK) >> 2;
	if (type) {
1163 1164
		u32	ack;
		u32	host_cmd;
L
Linus Torvalds 已提交
1165

1166 1167
		outw(host_int, dev->base_addr + TLAN_HOST_INT);
		ack = tlan_int_vector[type](dev, host_int);
L
Linus Torvalds 已提交
1168

1169 1170 1171
		if (ack) {
			host_cmd = TLAN_HC_ACK | ack | (type << 18);
			outl(host_cmd, dev->base_addr + TLAN_HOST_CMD);
1172
		}
L
Linus Torvalds 已提交
1173 1174 1175 1176
	}

	spin_unlock(&priv->lock);

1177
	return IRQ_RETVAL(type);
1178
}
L
Linus Torvalds 已提交
1179 1180 1181 1182




1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
/***************************************************************
 *	tlan_close
 *
 *	Returns:
 *		An error code.
 *	Parms:
 *		dev	The device structure of the device to
 *			close.
 *
 *	This function shuts down the adapter.  It records any
 *	stats, puts the adapter into reset state, deactivates
 *	its time as needed, and	frees the irq it is using.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1197

1198
static int tlan_close(struct net_device *dev)
L
Linus Torvalds 已提交
1199
{
S
Sakari Ailus 已提交
1200
	tlan_stop(dev);
1201

1202 1203 1204
	free_irq(dev->irq, dev);
	tlan_free_lists(dev);
	TLAN_DBG(TLAN_DEBUG_GNRL, "Device %s closed.\n", dev->name);
L
Linus Torvalds 已提交
1205 1206 1207

	return 0;

1208
}
L
Linus Torvalds 已提交
1209 1210 1211 1212




1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
/***************************************************************
 *	tlan_get_stats
 *
 *	Returns:
 *		A pointer to the device's statistics structure.
 *	Parms:
 *		dev	The device structure to return the
 *			stats for.
 *
 *	This function updates the devices statistics by reading
 *	the TLAN chip's onboard registers.  Then it returns the
 *	address of the statistics structure.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1227

1228
static struct net_device_stats *tlan_get_stats(struct net_device *dev)
L
Linus Torvalds 已提交
1229
{
1230
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1231 1232 1233
	int i;

	/* Should only read stats if open ? */
1234
	tlan_read_and_clear_stats(dev, TLAN_RECORD);
L
Linus Torvalds 已提交
1235

1236 1237 1238 1239 1240 1241 1242
	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  %s EOC count = %d\n", dev->name,
		 priv->rx_eoc_count);
	TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT:  %s Busy count = %d\n", dev->name,
		 priv->tx_busy_count);
	if (debug & TLAN_DEBUG_GNRL) {
		tlan_print_dio(dev->base_addr);
		tlan_phy_print(dev);
L
Linus Torvalds 已提交
1243
	}
1244 1245 1246 1247 1248
	if (debug & TLAN_DEBUG_LIST) {
		for (i = 0; i < TLAN_NUM_RX_LISTS; i++)
			tlan_print_list(priv->rx_list + i, "RX", i);
		for (i = 0; i < TLAN_NUM_TX_LISTS; i++)
			tlan_print_list(priv->tx_list + i, "TX", i);
L
Linus Torvalds 已提交
1249
	}
1250

S
Stephen Hemminger 已提交
1251
	return &dev->stats;
L
Linus Torvalds 已提交
1252

1253
}
L
Linus Torvalds 已提交
1254 1255 1256 1257




1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
/***************************************************************
 *	tlan_set_multicast_list
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	The device structure to set the
 *			multicast list for.
 *
 *	This function sets the TLAN adaptor to various receive
 *	modes.  If the IFF_PROMISC flag is set, promiscuous
 *	mode is acitviated.  Otherwise,	promiscuous mode is
 *	turned off.  If the IFF_ALLMULTI flag is set, then
 *	the hash table is set to receive all group addresses.
 *	Otherwise, the first three multicast addresses are
 *	stored in AREG_1-3, and the rest are selected via the
 *	hash table, as necessary.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1277

1278
static void tlan_set_multicast_list(struct net_device *dev)
1279
{
1280
	struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
1281 1282 1283 1284 1285 1286
	u32			hash1 = 0;
	u32			hash2 = 0;
	int			i;
	u32			offset;
	u8			tmp;

1287 1288 1289 1290
	if (dev->flags & IFF_PROMISC) {
		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
		tlan_dio_write8(dev->base_addr,
				TLAN_NET_CMD, tmp | TLAN_NET_CMD_CAF);
L
Linus Torvalds 已提交
1291
	} else {
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
		tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
		tlan_dio_write8(dev->base_addr,
				TLAN_NET_CMD, tmp & ~TLAN_NET_CMD_CAF);
		if (dev->flags & IFF_ALLMULTI) {
			for (i = 0; i < 3; i++)
				tlan_set_mac(dev, i + 1, NULL);
			tlan_dio_write32(dev->base_addr, TLAN_HASH_1,
					 0xffffffff);
			tlan_dio_write32(dev->base_addr, TLAN_HASH_2,
					 0xffffffff);
L
Linus Torvalds 已提交
1302
		} else {
1303
			i = 0;
1304
			netdev_for_each_mc_addr(ha, dev) {
1305 1306
				if (i < 3) {
					tlan_set_mac(dev, i + 1,
1307
						     (char *) &ha->addr);
L
Linus Torvalds 已提交
1308
				} else {
1309 1310 1311 1312
					offset =
						tlan_hash_func((u8 *)&ha->addr);
					if (offset < 32)
						hash1 |= (1 << offset);
L
Linus Torvalds 已提交
1313
					else
1314
						hash2 |= (1 << (offset - 32));
L
Linus Torvalds 已提交
1315
				}
1316
				i++;
L
Linus Torvalds 已提交
1317
			}
1318 1319 1320 1321
			for ( ; i < 3; i++)
				tlan_set_mac(dev, i + 1, NULL);
			tlan_dio_write32(dev->base_addr, TLAN_HASH_1, hash1);
			tlan_dio_write32(dev->base_addr, TLAN_HASH_2, hash2);
L
Linus Torvalds 已提交
1322 1323 1324
		}
	}

1325
}
L
Linus Torvalds 已提交
1326 1327 1328 1329 1330 1331



/*****************************************************************************
******************************************************************************

1332
ThunderLAN driver interrupt vectors and table
L
Linus Torvalds 已提交
1333

1334 1335 1336
please see chap. 4, "Interrupt Handling" of the "ThunderLAN
Programmer's Guide" for more informations on handling interrupts
generated by TLAN based adapters.
L
Linus Torvalds 已提交
1337 1338 1339 1340 1341 1342 1343

******************************************************************************
*****************************************************************************/




1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
/***************************************************************
 *	tlan_handle_tx_eof
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This function handles Tx EOF interrupts which are raised
 *	by the adapter when it has completed sending the
 *	contents of a buffer.  If detemines which list/buffer
 *	was completed and resets it.  If the buffer was the last
 *	in the channel (EOC), then the function checks to see if
 *	another buffer is ready to send, and if so, sends a Tx
 *	Go command.  Finally, the driver activates/continues the
 *	activity LED.
 *
 **************************************************************/

static u32 tlan_handle_tx_eof(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1367
{
1368
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1369
	int		eoc = 0;
1370
	struct tlan_list	*head_list;
L
Linus Torvalds 已提交
1371 1372
	dma_addr_t	head_list_phys;
	u32		ack = 0;
1373
	u16		tmp_c_stat;
1374

1375 1376 1377 1378
	TLAN_DBG(TLAN_DEBUG_TX,
		 "TRANSMIT:  Handling TX EOF (Head=%d Tail=%d)\n",
		 priv->tx_head, priv->tx_tail);
	head_list = priv->tx_list + priv->tx_head;
L
Linus Torvalds 已提交
1379

1380 1381 1382
	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
	       && (ack < 255)) {
		struct sk_buff *skb = tlan_get_skb(head_list);
1383

L
Linus Torvalds 已提交
1384
		ack++;
1385
		pci_unmap_single(priv->pci_dev, head_list->buffer[0].address,
1386 1387 1388 1389 1390 1391
				 max(skb->len,
				     (unsigned int)TLAN_MIN_FRAME_SIZE),
				 PCI_DMA_TODEVICE);
		dev_kfree_skb_any(skb);
		head_list->buffer[8].address = 0;
		head_list->buffer[9].address = 0;
1392

1393
		if (tmp_c_stat & TLAN_CSTAT_EOC)
L
Linus Torvalds 已提交
1394
			eoc = 1;
1395

1396
		dev->stats.tx_bytes += head_list->frame_size;
L
Linus Torvalds 已提交
1397

1398
		head_list->c_stat = TLAN_CSTAT_UNUSED;
1399
		netif_start_queue(dev);
1400 1401
		CIRC_INC(priv->tx_head, TLAN_NUM_TX_LISTS);
		head_list = priv->tx_list + priv->tx_head;
L
Linus Torvalds 已提交
1402 1403 1404
	}

	if (!ack)
1405 1406
		netdev_info(dev,
			    "Received interrupt for uncompleted TX frame\n");
1407 1408 1409 1410 1411 1412 1413 1414

	if (eoc) {
		TLAN_DBG(TLAN_DEBUG_TX,
			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d)\n",
			 priv->tx_head, priv->tx_tail);
		head_list = priv->tx_list + priv->tx_head;
		head_list_phys = priv->tx_list_dma
			+ sizeof(struct tlan_list)*priv->tx_head;
1415 1416
		if ((head_list->c_stat & TLAN_CSTAT_READY)
		    == TLAN_CSTAT_READY) {
1417
			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
L
Linus Torvalds 已提交
1418 1419
			ack |= TLAN_HC_GO;
		} else {
1420
			priv->tx_in_progress = 0;
L
Linus Torvalds 已提交
1421 1422
		}
	}
1423

1424 1425 1426 1427
	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
		tlan_dio_write8(dev->base_addr,
				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
		if (priv->timer.function == NULL) {
1428
			priv->timer.function = tlan_timer;
1429 1430 1431 1432 1433 1434
			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
			priv->timer_set_at = jiffies;
			priv->timer_type = TLAN_TIMER_ACTIVITY;
			add_timer(&priv->timer);
		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
			priv->timer_set_at = jiffies;
L
Linus Torvalds 已提交
1435 1436 1437 1438 1439
		}
	}

	return ack;

1440
}
L
Linus Torvalds 已提交
1441 1442 1443 1444




1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
/***************************************************************
 *	TLan_HandleStatOverflow
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This function handles the Statistics Overflow interrupt
 *	which means that one or more of the TLAN statistics
 *	registers has reached 1/2 capacity and needs to be read.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1461

1462
static u32 tlan_handle_stat_overflow(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1463
{
1464
	tlan_read_and_clear_stats(dev, TLAN_RECORD);
L
Linus Torvalds 已提交
1465 1466 1467

	return 1;

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
}




/***************************************************************
 *	TLan_HandleRxEOF
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This function handles the Rx EOF interrupt which
 *	indicates a frame has been received by the adapter from
 *	the net and the frame has been transferred to memory.
 *	The function determines the bounce buffer the frame has
 *	been loaded into, creates a new sk_buff big enough to
 *	hold the frame, and sends it to protocol stack.  It
 *	then resets the used buffer and appends it to the end
 *	of the list.  If the frame was the last in the Rx
 *	channel (EOC), the function restarts the receive channel
 *	by sending an Rx Go command to the adapter.  Then it
 *	activates/continues the activity LED.
 *
 **************************************************************/

static u32 tlan_handle_rx_eof(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1499
{
1500
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1501 1502
	u32		ack = 0;
	int		eoc = 0;
1503
	struct tlan_list	*head_list;
L
Linus Torvalds 已提交
1504
	struct sk_buff	*skb;
1505 1506
	struct tlan_list	*tail_list;
	u16		tmp_c_stat;
L
Linus Torvalds 已提交
1507 1508
	dma_addr_t	head_list_phys;

1509 1510 1511 1512 1513
	TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE:  handling RX EOF (Head=%d Tail=%d)\n",
		 priv->rx_head, priv->rx_tail);
	head_list = priv->rx_list + priv->rx_head;
	head_list_phys =
		priv->rx_list_dma + sizeof(struct tlan_list)*priv->rx_head;
1514

1515 1516 1517 1518
	while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
	       && (ack < 255)) {
		dma_addr_t frame_dma = head_list->buffer[0].address;
		u32 frame_size = head_list->frame_size;
1519 1520
		struct sk_buff *new_skb;

L
Linus Torvalds 已提交
1521
		ack++;
1522
		if (tmp_c_stat & TLAN_CSTAT_EOC)
L
Linus Torvalds 已提交
1523
			eoc = 1;
1524

1525 1526
		new_skb = netdev_alloc_skb_ip_align(dev,
						    TLAN_MAX_FRAME_SIZE + 5);
1527
		if (!new_skb)
1528
			goto drop_and_reuse;
1529

1530 1531
		skb = tlan_get_skb(head_list);
		pci_unmap_single(priv->pci_dev, frame_dma,
1532
				 TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
1533
		skb_put(skb, frame_size);
L
Linus Torvalds 已提交
1534

1535
		dev->stats.rx_bytes += frame_size;
L
Linus Torvalds 已提交
1536

1537 1538
		skb->protocol = eth_type_trans(skb, dev);
		netif_rx(skb);
1539

1540 1541 1542
		head_list->buffer[0].address =
			pci_map_single(priv->pci_dev, new_skb->data,
				       TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
S
Stephen Hemminger 已提交
1543

1544
		tlan_store_skb(head_list, new_skb);
S
Stephen Hemminger 已提交
1545
drop_and_reuse:
L
Linus Torvalds 已提交
1546
		head_list->forward = 0;
1547 1548
		head_list->c_stat = 0;
		tail_list = priv->rx_list + priv->rx_tail;
L
Linus Torvalds 已提交
1549 1550
		tail_list->forward = head_list_phys;

1551 1552 1553 1554 1555
		CIRC_INC(priv->rx_head, TLAN_NUM_RX_LISTS);
		CIRC_INC(priv->rx_tail, TLAN_NUM_RX_LISTS);
		head_list = priv->rx_list + priv->rx_head;
		head_list_phys = priv->rx_list_dma
			+ sizeof(struct tlan_list)*priv->rx_head;
L
Linus Torvalds 已提交
1556 1557 1558
	}

	if (!ack)
1559 1560
		netdev_info(dev,
			    "Received interrupt for uncompleted RX frame\n");
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570


	if (eoc) {
		TLAN_DBG(TLAN_DEBUG_RX,
			 "RECEIVE:  handling RX EOC (Head=%d Tail=%d)\n",
			 priv->rx_head, priv->rx_tail);
		head_list = priv->rx_list + priv->rx_head;
		head_list_phys = priv->rx_list_dma
			+ sizeof(struct tlan_list)*priv->rx_head;
		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
L
Linus Torvalds 已提交
1571
		ack |= TLAN_HC_GO | TLAN_HC_RT;
1572
		priv->rx_eoc_count++;
L
Linus Torvalds 已提交
1573 1574
	}

1575 1576 1577 1578
	if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
		tlan_dio_write8(dev->base_addr,
				TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
		if (priv->timer.function == NULL)  {
1579
			priv->timer.function = tlan_timer;
L
Linus Torvalds 已提交
1580
			priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
1581 1582
			priv->timer_set_at = jiffies;
			priv->timer_type = TLAN_TIMER_ACTIVITY;
L
Linus Torvalds 已提交
1583
			add_timer(&priv->timer);
1584 1585
		} else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
			priv->timer_set_at = jiffies;
L
Linus Torvalds 已提交
1586 1587 1588 1589 1590
		}
	}

	return ack;

1591
}
L
Linus Torvalds 已提交
1592 1593 1594 1595




1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
/***************************************************************
 *	tlan_handle_dummy
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This function handles the Dummy interrupt, which is
 *	raised whenever a test interrupt is generated by setting
 *	the Req_Int bit of HOST_CMD to 1.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1612

1613
static u32 tlan_handle_dummy(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1614
{
1615
	netdev_info(dev, "Test interrupt\n");
L
Linus Torvalds 已提交
1616 1617
	return 1;

1618
}
L
Linus Torvalds 已提交
1619 1620 1621 1622




1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
/***************************************************************
 *	tlan_handle_tx_eoc
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This driver is structured to determine EOC occurrences by
 *	reading the CSTAT member of the list structure.  Tx EOC
 *	interrupts are disabled via the DIO INTDIS register.
 *	However, TLAN chips before revision 3.0 didn't have this
 *	functionality, so process EOC events if this is the
 *	case.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1642

1643
static u32 tlan_handle_tx_eoc(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1644
{
1645 1646
	struct tlan_priv	*priv = netdev_priv(dev);
	struct tlan_list		*head_list;
L
Linus Torvalds 已提交
1647 1648
	dma_addr_t		head_list_phys;
	u32			ack = 1;
1649

1650 1651 1652 1653 1654 1655 1656
	if (priv->tlan_rev < 0x30) {
		TLAN_DBG(TLAN_DEBUG_TX,
			 "TRANSMIT:  handling TX EOC (Head=%d Tail=%d) -- IRQ\n",
			 priv->tx_head, priv->tx_tail);
		head_list = priv->tx_list + priv->tx_head;
		head_list_phys = priv->tx_list_dma
			+ sizeof(struct tlan_list)*priv->tx_head;
1657 1658
		if ((head_list->c_stat & TLAN_CSTAT_READY)
		    == TLAN_CSTAT_READY) {
L
Linus Torvalds 已提交
1659
			netif_stop_queue(dev);
1660
			outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
L
Linus Torvalds 已提交
1661 1662
			ack |= TLAN_HC_GO;
		} else {
1663
			priv->tx_in_progress = 0;
L
Linus Torvalds 已提交
1664 1665 1666 1667 1668
		}
	}

	return ack;

1669
}
L
Linus Torvalds 已提交
1670 1671 1672 1673




1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
/***************************************************************
 *	tlan_handle_status_check
 *
 *	Returns:
 *		0 if Adapter check, 1 if Network Status check.
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This function handles Adapter Check/Network Status
 *	interrupts generated by the adapter.  It checks the
 *	vector in the HOST_INT register to determine if it is
 *	an Adapter Check interrupt.  If so, it resets the
 *	adapter.  Otherwise it clears the status registers
 *	and services the PHY.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1693

1694
static u32 tlan_handle_status_check(struct net_device *dev, u16 host_int)
1695
{
1696
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1697 1698 1699 1700 1701 1702
	u32		ack;
	u32		error;
	u8		net_sts;
	u32		phy;
	u16		tlphy_ctl;
	u16		tlphy_sts;
1703

L
Linus Torvalds 已提交
1704
	ack = 1;
1705 1706 1707
	if (host_int & TLAN_HI_IV_MASK) {
		netif_stop_queue(dev);
		error = inl(dev->base_addr + TLAN_CH_PARM);
1708
		netdev_info(dev, "Adaptor Error = 0x%x\n", error);
1709 1710
		tlan_read_and_clear_stats(dev, TLAN_RECORD);
		outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
L
Linus Torvalds 已提交
1711 1712 1713 1714 1715 1716

		schedule_work(&priv->tlan_tqueue);

		netif_wake_queue(dev);
		ack = 0;
	} else {
1717 1718 1719 1720 1721 1722 1723 1724
		TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Status Check\n", dev->name);
		phy = priv->phy[priv->phy_num];

		net_sts = tlan_dio_read8(dev->base_addr, TLAN_NET_STS);
		if (net_sts) {
			tlan_dio_write8(dev->base_addr, TLAN_NET_STS, net_sts);
			TLAN_DBG(TLAN_DEBUG_GNRL, "%s:    Net_Sts = %x\n",
				 dev->name, (unsigned) net_sts);
L
Linus Torvalds 已提交
1725
		}
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
		if ((net_sts & TLAN_NET_STS_MIRQ) &&  (priv->phy_num == 0)) {
			tlan_mii_read_reg(dev, phy, TLAN_TLPHY_STS, &tlphy_sts);
			tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
			if (!(tlphy_sts & TLAN_TS_POLOK) &&
			    !(tlphy_ctl & TLAN_TC_SWAPOL)) {
				tlphy_ctl |= TLAN_TC_SWAPOL;
				tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
						   tlphy_ctl);
			} else if ((tlphy_sts & TLAN_TS_POLOK) &&
				   (tlphy_ctl & TLAN_TC_SWAPOL)) {
				tlphy_ctl &= ~TLAN_TC_SWAPOL;
				tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
						   tlphy_ctl);
L
Linus Torvalds 已提交
1739
			}
1740 1741 1742

			if (debug)
				tlan_phy_print(dev);
L
Linus Torvalds 已提交
1743 1744 1745 1746 1747
		}
	}

	return ack;

1748
}
L
Linus Torvalds 已提交
1749 1750 1751 1752




1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
/***************************************************************
 *	tlan_handle_rx_eoc
 *
 *	Returns:
 *		1
 *	Parms:
 *		dev		Device assigned the IRQ that was
 *				raised.
 *		host_int	The contents of the HOST_INT
 *				port.
 *
 *	This driver is structured to determine EOC occurrences by
 *	reading the CSTAT member of the list structure.  Rx EOC
 *	interrupts are disabled via the DIO INTDIS register.
 *	However, TLAN chips before revision 3.0 didn't have this
 *	CSTAT member or a INTDIS register, so if this chip is
 *	pre-3.0, process EOC interrupts normally.
 *
 **************************************************************/
L
Linus Torvalds 已提交
1772

1773
static u32 tlan_handle_rx_eoc(struct net_device *dev, u16 host_int)
L
Linus Torvalds 已提交
1774
{
1775
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1776 1777 1778
	dma_addr_t	head_list_phys;
	u32		ack = 1;

1779 1780 1781 1782 1783 1784 1785
	if (priv->tlan_rev < 0x30) {
		TLAN_DBG(TLAN_DEBUG_RX,
			 "RECEIVE:  Handling RX EOC (head=%d tail=%d) -- IRQ\n",
			 priv->rx_head, priv->rx_tail);
		head_list_phys = priv->rx_list_dma
			+ sizeof(struct tlan_list)*priv->rx_head;
		outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
L
Linus Torvalds 已提交
1786
		ack |= TLAN_HC_GO | TLAN_HC_RT;
1787
		priv->rx_eoc_count++;
L
Linus Torvalds 已提交
1788 1789 1790 1791
	}

	return ack;

1792
}
L
Linus Torvalds 已提交
1793 1794 1795 1796 1797 1798 1799




/*****************************************************************************
******************************************************************************

1800
ThunderLAN driver timer function
L
Linus Torvalds 已提交
1801 1802 1803 1804 1805

******************************************************************************
*****************************************************************************/


1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
/***************************************************************
 *	tlan_timer
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		data	A value given to add timer when
 *			add_timer was called.
 *
 *	This function handles timed functionality for the
 *	TLAN driver.  The two current timer uses are for
 *	delaying for autonegotionation and driving the ACT LED.
 *	-	Autonegotiation requires being allowed about
 *		2 1/2 seconds before attempting to transmit a
 *		packet.  It would be a very bad thing to hang
 *		the kernel this long, so the driver doesn't
 *		allow transmission 'til after this time, for
 *		certain PHYs.  It would be much nicer if all
 *		PHYs were interrupt-capable like the internal
 *		PHY.
 *	-	The ACT LED, which shows adapter activity, is
 *		driven by the driver, and so must be left on
 *		for a short period to power up the LED so it
 *		can be seen.  This delay can be changed by
 *		changing the TLAN_TIMER_ACT_DELAY in tlan.h,
 *		if desired.  100 ms  produces a slightly
 *		sluggish response.
 *
 **************************************************************/

1836
static void tlan_timer(struct timer_list *t)
L
Linus Torvalds 已提交
1837
{
1838 1839
	struct tlan_priv	*priv = from_timer(priv, t, timer);
	struct net_device	*dev = priv->dev;
L
Linus Torvalds 已提交
1840 1841 1842 1843 1844
	u32		elapsed;
	unsigned long	flags = 0;

	priv->timer.function = NULL;

1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
	switch (priv->timer_type) {
	case TLAN_TIMER_PHY_PDOWN:
		tlan_phy_power_down(dev);
		break;
	case TLAN_TIMER_PHY_PUP:
		tlan_phy_power_up(dev);
		break;
	case TLAN_TIMER_PHY_RESET:
		tlan_phy_reset(dev);
		break;
	case TLAN_TIMER_PHY_START_LINK:
		tlan_phy_start_link(dev);
		break;
	case TLAN_TIMER_PHY_FINISH_AN:
		tlan_phy_finish_auto_neg(dev);
		break;
	case TLAN_TIMER_FINISH_RESET:
		tlan_finish_reset(dev);
		break;
	case TLAN_TIMER_ACTIVITY:
		spin_lock_irqsave(&priv->lock, flags);
		if (priv->timer.function == NULL) {
			elapsed = jiffies - priv->timer_set_at;
			if (elapsed >= TLAN_TIMER_ACT_DELAY) {
				tlan_dio_write8(dev->base_addr,
						TLAN_LED_REG, TLAN_LED_LINK);
			} else  {
				priv->timer.expires = priv->timer_set_at
					+ TLAN_TIMER_ACT_DELAY;
				spin_unlock_irqrestore(&priv->lock, flags);
				add_timer(&priv->timer);
				break;
L
Linus Torvalds 已提交
1877
			}
1878 1879 1880 1881 1882
		}
		spin_unlock_irqrestore(&priv->lock, flags);
		break;
	default:
		break;
L
Linus Torvalds 已提交
1883 1884
	}

1885
}
L
Linus Torvalds 已提交
1886 1887 1888 1889 1890


/*****************************************************************************
******************************************************************************

1891
ThunderLAN driver adapter related routines
L
Linus Torvalds 已提交
1892 1893 1894 1895 1896

******************************************************************************
*****************************************************************************/


1897 1898 1899 1900 1901 1902 1903
/***************************************************************
 *	tlan_reset_lists
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	The device structure with the list
1904
 *			structures to be reset.
1905 1906 1907 1908 1909 1910 1911
 *
 *	This routine sets the variables associated with managing
 *	the TLAN lists to their initial values.
 *
 **************************************************************/

static void tlan_reset_lists(struct net_device *dev)
L
Linus Torvalds 已提交
1912
{
1913
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1914
	int		i;
1915
	struct tlan_list	*list;
L
Linus Torvalds 已提交
1916 1917 1918
	dma_addr_t	list_phys;
	struct sk_buff	*skb;

1919 1920 1921 1922 1923
	priv->tx_head = 0;
	priv->tx_tail = 0;
	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
		list = priv->tx_list + i;
		list->c_stat = TLAN_CSTAT_UNUSED;
1924
		list->buffer[0].address = 0;
L
Linus Torvalds 已提交
1925 1926 1927 1928 1929 1930
		list->buffer[2].count = 0;
		list->buffer[2].address = 0;
		list->buffer[8].address = 0;
		list->buffer[9].address = 0;
	}

1931 1932 1933 1934 1935 1936 1937
	priv->rx_head = 0;
	priv->rx_tail = TLAN_NUM_RX_LISTS - 1;
	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
		list = priv->rx_list + i;
		list_phys = priv->rx_list_dma + sizeof(struct tlan_list)*i;
		list->c_stat = TLAN_CSTAT_READY;
		list->frame_size = TLAN_MAX_FRAME_SIZE;
L
Linus Torvalds 已提交
1938
		list->buffer[0].count = TLAN_MAX_FRAME_SIZE | TLAN_LAST_BUFFER;
1939
		skb = netdev_alloc_skb_ip_align(dev, TLAN_MAX_FRAME_SIZE + 5);
1940
		if (!skb)
1941 1942
			break;

1943
		list->buffer[0].address = pci_map_single(priv->pci_dev,
1944 1945 1946
							 skb->data,
							 TLAN_MAX_FRAME_SIZE,
							 PCI_DMA_FROMDEVICE);
1947
		tlan_store_skb(list, skb);
L
Linus Torvalds 已提交
1948 1949
		list->buffer[1].count = 0;
		list->buffer[1].address = 0;
1950
		list->forward = list_phys + sizeof(struct tlan_list);
1951 1952 1953 1954
	}

	/* in case ran out of memory early, clear bits */
	while (i < TLAN_NUM_RX_LISTS) {
1955
		tlan_store_skb(priv->rx_list + i, NULL);
1956
		++i;
L
Linus Torvalds 已提交
1957
	}
1958
	list->forward = 0;
L
Linus Torvalds 已提交
1959

1960
}
L
Linus Torvalds 已提交
1961 1962


1963
static void tlan_free_lists(struct net_device *dev)
L
Linus Torvalds 已提交
1964
{
1965
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
1966
	int		i;
1967
	struct tlan_list	*list;
L
Linus Torvalds 已提交
1968 1969
	struct sk_buff	*skb;

1970 1971 1972 1973
	for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
		list = priv->tx_list + i;
		skb = tlan_get_skb(list);
		if (skb) {
1974
			pci_unmap_single(
1975
				priv->pci_dev,
1976 1977 1978 1979
				list->buffer[0].address,
				max(skb->len,
				    (unsigned int)TLAN_MIN_FRAME_SIZE),
				PCI_DMA_TODEVICE);
1980
			dev_kfree_skb_any(skb);
1981 1982
			list->buffer[8].address = 0;
			list->buffer[9].address = 0;
L
Linus Torvalds 已提交
1983
		}
1984
	}
L
Linus Torvalds 已提交
1985

1986 1987 1988 1989 1990
	for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
		list = priv->rx_list + i;
		skb = tlan_get_skb(list);
		if (skb) {
			pci_unmap_single(priv->pci_dev,
1991 1992 1993
					 list->buffer[0].address,
					 TLAN_MAX_FRAME_SIZE,
					 PCI_DMA_FROMDEVICE);
1994
			dev_kfree_skb_any(skb);
1995 1996
			list->buffer[8].address = 0;
			list->buffer[9].address = 0;
L
Linus Torvalds 已提交
1997 1998
		}
	}
1999
}
L
Linus Torvalds 已提交
2000 2001 2002 2003




2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
/***************************************************************
 *	tlan_print_dio
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		io_base		Base IO port of the device of
 *				which to print DIO registers.
 *
 *	This function prints out all the internal (DIO)
 *	registers of a TLAN chip.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2017

2018
static void tlan_print_dio(u16 io_base)
L
Linus Torvalds 已提交
2019 2020 2021 2022
{
	u32 data0, data1;
	int	i;

2023 2024 2025
	pr_info("Contents of internal registers for io base 0x%04hx\n",
		io_base);
	pr_info("Off.  +0        +4\n");
2026 2027 2028
	for (i = 0; i < 0x4C; i += 8) {
		data0 = tlan_dio_read32(io_base, i);
		data1 = tlan_dio_read32(io_base, i + 0x4);
2029
		pr_info("0x%02x  0x%08x 0x%08x\n", i, data0, data1);
L
Linus Torvalds 已提交
2030 2031
	}

2032
}
L
Linus Torvalds 已提交
2033 2034 2035 2036




2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052
/***************************************************************
 *	TLan_PrintList
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		list	A pointer to the struct tlan_list structure to
 *			be printed.
 *		type	A string to designate type of list,
 *			"Rx" or "Tx".
 *		num	The index of the list.
 *
 *	This function prints out the contents of the list
 *	pointed to by the list parameter.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2053

2054
static void tlan_print_list(struct tlan_list *list, char *type, int num)
L
Linus Torvalds 已提交
2055 2056 2057
{
	int i;

2058 2059 2060 2061
	pr_info("%s List %d at %p\n", type, num, list);
	pr_info("   Forward    = 0x%08x\n",  list->forward);
	pr_info("   CSTAT      = 0x%04hx\n", list->c_stat);
	pr_info("   Frame Size = 0x%04hx\n", list->frame_size);
2062 2063
	/* for (i = 0; i < 10; i++) { */
	for (i = 0; i < 2; i++) {
2064 2065
		pr_info("   Buffer[%d].count, addr = 0x%08x, 0x%08x\n",
			i, list->buffer[i].count, list->buffer[i].address);
L
Linus Torvalds 已提交
2066 2067
	}

2068
}
L
Linus Torvalds 已提交
2069 2070 2071 2072




2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
/***************************************************************
 *	tlan_read_and_clear_stats
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	Pointer to device structure of adapter
 *			to which to read stats.
 *		record	Flag indicating whether to add
 *
 *	This functions reads all the internal status registers
 *	of the TLAN chip, which clears them as a side effect.
 *	It then either adds the values to the device's status
 *	struct, or discards them, depending on whether record
 *	is TLAN_RECORD (!=0)  or TLAN_IGNORE (==0).
 *
 **************************************************************/
L
Linus Torvalds 已提交
2090

2091
static void tlan_read_and_clear_stats(struct net_device *dev, int record)
L
Linus Torvalds 已提交
2092 2093 2094 2095 2096 2097 2098
{
	u32		tx_good, tx_under;
	u32		rx_good, rx_over;
	u32		def_tx, crc, code;
	u32		multi_col, single_col;
	u32		excess_col, late_col, loss;

2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	outw(TLAN_GOOD_TX_FRMS, dev->base_addr + TLAN_DIO_ADR);
	tx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
	tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
	tx_under = inb(dev->base_addr + TLAN_DIO_DATA + 3);

	outw(TLAN_GOOD_RX_FRMS, dev->base_addr + TLAN_DIO_ADR);
	rx_good  = inb(dev->base_addr + TLAN_DIO_DATA);
	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
	rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
	rx_over  = inb(dev->base_addr + TLAN_DIO_DATA + 3);

	outw(TLAN_DEFERRED_TX, dev->base_addr + TLAN_DIO_ADR);
	def_tx  = inb(dev->base_addr + TLAN_DIO_DATA);
	def_tx += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
	crc     = inb(dev->base_addr + TLAN_DIO_DATA + 2);
	code    = inb(dev->base_addr + TLAN_DIO_DATA + 3);

	outw(TLAN_MULTICOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
	multi_col   = inb(dev->base_addr + TLAN_DIO_DATA);
	multi_col  += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
	single_col  = inb(dev->base_addr + TLAN_DIO_DATA + 2);
	single_col += inb(dev->base_addr + TLAN_DIO_DATA + 3) << 8;

	outw(TLAN_EXCESSCOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
	excess_col = inb(dev->base_addr + TLAN_DIO_DATA);
	late_col   = inb(dev->base_addr + TLAN_DIO_DATA + 1);
	loss       = inb(dev->base_addr + TLAN_DIO_DATA + 2);

	if (record) {
S
Stephen Hemminger 已提交
2129 2130 2131 2132
		dev->stats.rx_packets += rx_good;
		dev->stats.rx_errors  += rx_over + crc + code;
		dev->stats.tx_packets += tx_good;
		dev->stats.tx_errors  += tx_under + loss;
2133 2134
		dev->stats.collisions += multi_col
			+ single_col + excess_col + late_col;
S
Stephen Hemminger 已提交
2135 2136 2137 2138 2139 2140 2141

		dev->stats.rx_over_errors    += rx_over;
		dev->stats.rx_crc_errors     += crc;
		dev->stats.rx_frame_errors   += code;

		dev->stats.tx_aborted_errors += tx_under;
		dev->stats.tx_carrier_errors += loss;
L
Linus Torvalds 已提交
2142
	}
2143

2144
}
L
Linus Torvalds 已提交
2145 2146 2147 2148




2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
/***************************************************************
 *	TLan_Reset
 *
 *	Returns:
 *		0
 *	Parms:
 *		dev	Pointer to device structure of adapter
 *			to be reset.
 *
 *	This function resets the adapter and it's physical
 *	device.  See Chap. 3, pp. 9-10 of the "ThunderLAN
 *	Programmer's Guide" for details.  The routine tries to
 *	implement what is detailed there, though adjustments
 *	have been made.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2165

2166
static void
2167
tlan_reset_adapter(struct net_device *dev)
L
Linus Torvalds 已提交
2168
{
2169
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2170 2171 2172 2173 2174
	int		i;
	u32		addr;
	u32		data;
	u8		data8;

2175 2176
	priv->tlan_full_duplex = false;
	priv->phy_online = 0;
L
Linus Torvalds 已提交
2177 2178 2179 2180 2181 2182 2183
	netif_carrier_off(dev);

/*  1.	Assert reset bit. */

	data = inl(dev->base_addr + TLAN_HOST_CMD);
	data |= TLAN_HC_AD_RST;
	outl(data, dev->base_addr + TLAN_HOST_CMD);
2184

L
Linus Torvalds 已提交
2185 2186
	udelay(1000);

2187
/*  2.	Turn off interrupts. (Probably isn't necessary) */
L
Linus Torvalds 已提交
2188 2189 2190 2191 2192 2193 2194

	data = inl(dev->base_addr + TLAN_HOST_CMD);
	data |= TLAN_HC_INT_OFF;
	outl(data, dev->base_addr + TLAN_HOST_CMD);

/*  3.	Clear AREGs and HASHs. */

2195 2196
	for (i = TLAN_AREG_0; i <= TLAN_HASH_2; i += 4)
		tlan_dio_write32(dev->base_addr, (u16) i, 0);
L
Linus Torvalds 已提交
2197 2198 2199 2200

/*  4.	Setup NetConfig register. */

	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN | TLAN_NET_CFG_PHY_EN;
2201
	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
L
Linus Torvalds 已提交
2202 2203 2204

/*  5.	Load Ld_Tmr and Ld_Thr in HOST_CMD. */

2205 2206
	outl(TLAN_HC_LD_TMR | 0x3f, dev->base_addr + TLAN_HOST_CMD);
	outl(TLAN_HC_LD_THR | 0x9, dev->base_addr + TLAN_HOST_CMD);
L
Linus Torvalds 已提交
2207 2208 2209

/*  6.	Unreset the MII by setting NMRST (in NetSio) to 1. */

2210
	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
2211
	addr = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2212
	tlan_set_bit(TLAN_NET_SIO_NMRST, addr);
L
Linus Torvalds 已提交
2213 2214 2215

/*  7.	Setup the remaining registers. */

2216
	if (priv->tlan_rev >= 0x30) {
L
Linus Torvalds 已提交
2217
		data8 = TLAN_ID_TX_EOC | TLAN_ID_RX_EOC;
2218
		tlan_dio_write8(dev->base_addr, TLAN_INT_DIS, data8);
L
Linus Torvalds 已提交
2219
	}
2220
	tlan_phy_detect(dev);
L
Linus Torvalds 已提交
2221
	data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN;
2222

2223
	if (priv->adapter->flags & TLAN_ADAPTER_BIT_RATE_PHY) {
L
Linus Torvalds 已提交
2224
		data |= TLAN_NET_CFG_BIT;
2225 2226 2227 2228 2229
		if (priv->aui == 1) {
			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x0a);
		} else if (priv->duplex == TLAN_DUPLEX_FULL) {
			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x00);
			priv->tlan_full_duplex = true;
L
Linus Torvalds 已提交
2230
		} else {
2231
			tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x08);
L
Linus Torvalds 已提交
2232 2233 2234
		}
	}

2235 2236 2237
	/* don't power down internal PHY if we're going to use it */
	if (priv->phy_num == 0 ||
	   (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10))
L
Linus Torvalds 已提交
2238
		data |= TLAN_NET_CFG_PHY_EN;
2239
	tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
L
Linus Torvalds 已提交
2240

2241 2242 2243 2244
	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY)
		tlan_finish_reset(dev);
	else
		tlan_phy_power_down(dev);
L
Linus Torvalds 已提交
2245

2246
}
L
Linus Torvalds 已提交
2247 2248 2249 2250




2251
static void
2252
tlan_finish_reset(struct net_device *dev)
L
Linus Torvalds 已提交
2253
{
2254
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2255 2256 2257 2258 2259 2260
	u8		data;
	u32		phy;
	u8		sio;
	u16		status;
	u16		partner;
	u16		tlphy_ctl;
2261
	u16		tlphy_par;
L
Linus Torvalds 已提交
2262
	u16		tlphy_id1, tlphy_id2;
2263
	int		i;
L
Linus Torvalds 已提交
2264

2265
	phy = priv->phy[priv->phy_num];
L
Linus Torvalds 已提交
2266 2267

	data = TLAN_NET_CMD_NRESET | TLAN_NET_CMD_NWRAP;
2268
	if (priv->tlan_full_duplex)
L
Linus Torvalds 已提交
2269
		data |= TLAN_NET_CMD_DUPLEX;
2270
	tlan_dio_write8(dev->base_addr, TLAN_NET_CMD, data);
2271
	data = TLAN_NET_MASK_MASK4 | TLAN_NET_MASK_MASK5;
2272
	if (priv->phy_num == 0)
2273
		data |= TLAN_NET_MASK_MASK7;
2274 2275 2276 2277
	tlan_dio_write8(dev->base_addr, TLAN_NET_MASK, data);
	tlan_dio_write16(dev->base_addr, TLAN_MAX_RX, ((1536)+7)&~7);
	tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &tlphy_id1);
	tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &tlphy_id2);
2278

2279 2280
	if ((priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) ||
	    (priv->aui)) {
L
Linus Torvalds 已提交
2281
		status = MII_GS_LINK;
2282
		netdev_info(dev, "Link forced\n");
L
Linus Torvalds 已提交
2283
	} else {
2284 2285 2286
		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
		udelay(1000);
		tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
O
Ondrej Zary 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
		if (status & MII_GS_LINK) {
			/* We only support link info on Nat.Sem. PHY's */
			if ((tlphy_id1 == NAT_SEM_ID1) &&
			    (tlphy_id2 == NAT_SEM_ID2)) {
				tlan_mii_read_reg(dev, phy, MII_AN_LPA,
					&partner);
				tlan_mii_read_reg(dev, phy, TLAN_TLPHY_PAR,
					&tlphy_par);

				netdev_info(dev,
					"Link active, %s %uMbps %s-Duplex\n",
					!(tlphy_par & TLAN_PHY_AN_EN_STAT)
					? "forced" : "Autonegotiation enabled,",
					tlphy_par & TLAN_PHY_SPEED_100
					? 100 : 10,
					tlphy_par & TLAN_PHY_DUPLEX_FULL
					? "Full" : "Half");

				if (tlphy_par & TLAN_PHY_AN_EN_STAT) {
					netdev_info(dev, "Partner capability:");
					for (i = 5; i < 10; i++)
						if (partner & (1 << i))
							pr_cont(" %s",
								media[i-5]);
					pr_cont("\n");
				}
			} else
				netdev_info(dev, "Link active\n");
			/* Enabling link beat monitoring */
			priv->media_timer.expires = jiffies + HZ;
			add_timer(&priv->media_timer);
L
Linus Torvalds 已提交
2318 2319 2320
		}
	}

2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338
	if (priv->phy_num == 0) {
		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
		tlphy_ctl |= TLAN_TC_INTEN;
		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tlphy_ctl);
		sio = tlan_dio_read8(dev->base_addr, TLAN_NET_SIO);
		sio |= TLAN_NET_SIO_MINTEN;
		tlan_dio_write8(dev->base_addr, TLAN_NET_SIO, sio);
	}

	if (status & MII_GS_LINK) {
		tlan_set_mac(dev, 0, dev->dev_addr);
		priv->phy_online = 1;
		outb((TLAN_HC_INT_ON >> 8), dev->base_addr + TLAN_HOST_CMD + 1);
		if (debug >= 1 && debug != TLAN_DEBUG_PROBE)
			outb((TLAN_HC_REQ_INT >> 8),
			     dev->base_addr + TLAN_HOST_CMD + 1);
		outl(priv->rx_list_dma, dev->base_addr + TLAN_CH_PARM);
		outl(TLAN_HC_GO | TLAN_HC_RT, dev->base_addr + TLAN_HOST_CMD);
O
Ondrej Zary 已提交
2339
		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
L
Linus Torvalds 已提交
2340 2341
		netif_carrier_on(dev);
	} else {
2342
		netdev_info(dev, "Link inactive, will retry in 10 secs...\n");
2343
		tlan_set_timer(dev, (10*HZ), TLAN_TIMER_FINISH_RESET);
L
Linus Torvalds 已提交
2344 2345
		return;
	}
2346
	tlan_set_multicast_list(dev);
L
Linus Torvalds 已提交
2347

2348
}
L
Linus Torvalds 已提交
2349 2350 2351 2352




2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
/***************************************************************
 *	tlan_set_mac
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	Pointer to device structure of adapter
 *			on which to change the AREG.
 *		areg	The AREG to set the address in (0 - 3).
 *		mac	A pointer to an array of chars.  Each
 *			element stores one byte of the address.
 *			IE, it isn't in ascii.
 *
 *	This function transfers a MAC address to one of the
 *	TLAN AREGs (address registers).  The TLAN chip locks
 *	the register on writing to offset 0 and unlocks the
 *	register after writing to offset 5.  If NULL is passed
 *	in mac, then the AREG is filled with 0's.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2373

2374
static void tlan_set_mac(struct net_device *dev, int areg, char *mac)
L
Linus Torvalds 已提交
2375 2376
{
	int i;
2377

L
Linus Torvalds 已提交
2378 2379
	areg *= 6;

2380 2381 2382 2383
	if (mac != NULL) {
		for (i = 0; i < 6; i++)
			tlan_dio_write8(dev->base_addr,
					TLAN_AREG_0 + areg + i, mac[i]);
L
Linus Torvalds 已提交
2384
	} else {
2385 2386 2387
		for (i = 0; i < 6; i++)
			tlan_dio_write8(dev->base_addr,
					TLAN_AREG_0 + areg + i, 0);
L
Linus Torvalds 已提交
2388 2389
	}

2390
}
L
Linus Torvalds 已提交
2391 2392 2393 2394 2395 2396 2397




/*****************************************************************************
******************************************************************************

2398
ThunderLAN driver PHY layer routines
L
Linus Torvalds 已提交
2399 2400 2401 2402 2403 2404

******************************************************************************
*****************************************************************************/



2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416
/*********************************************************************
 *	tlan_phy_print
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	A pointer to the device structure of the
 *			TLAN device having the PHYs to be detailed.
 *
 *	This function prints the registers a PHY (aka transceiver).
 *
 ********************************************************************/
L
Linus Torvalds 已提交
2417

2418
static void tlan_phy_print(struct net_device *dev)
L
Linus Torvalds 已提交
2419
{
2420
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2421 2422
	u16 i, data0, data1, data2, data3, phy;

2423 2424 2425
	phy = priv->phy[priv->phy_num];

	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
2426
		netdev_info(dev, "Unmanaged PHY\n");
2427
	} else if (phy <= TLAN_PHY_MAX_ADDR) {
2428 2429
		netdev_info(dev, "PHY 0x%02x\n", phy);
		pr_info("   Off.  +0     +1     +2     +3\n");
2430 2431 2432 2433 2434
		for (i = 0; i < 0x20; i += 4) {
			tlan_mii_read_reg(dev, phy, i, &data0);
			tlan_mii_read_reg(dev, phy, i + 1, &data1);
			tlan_mii_read_reg(dev, phy, i + 2, &data2);
			tlan_mii_read_reg(dev, phy, i + 3, &data3);
2435 2436
			pr_info("   0x%02x 0x%04hx 0x%04hx 0x%04hx 0x%04hx\n",
				i, data0, data1, data2, data3);
L
Linus Torvalds 已提交
2437 2438
		}
	} else {
2439
		netdev_info(dev, "Invalid PHY\n");
L
Linus Torvalds 已提交
2440 2441
	}

2442
}
L
Linus Torvalds 已提交
2443 2444 2445 2446




2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462
/*********************************************************************
 *	tlan_phy_detect
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev	A pointer to the device structure of the adapter
 *			for which the PHY needs determined.
 *
 *	So far I've found that adapters which have external PHYs
 *	may also use the internal PHY for part of the functionality.
 *	(eg, AUI/Thinnet).  This function finds out if this TLAN
 *	chip has an internal PHY, and then finds the first external
 *	PHY (starting from address 0) if it exists).
 *
 ********************************************************************/
L
Linus Torvalds 已提交
2463

2464
static void tlan_phy_detect(struct net_device *dev)
L
Linus Torvalds 已提交
2465
{
2466
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2467 2468 2469 2470 2471
	u16		control;
	u16		hi;
	u16		lo;
	u32		phy;

2472 2473
	if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
		priv->phy_num = 0xffff;
L
Linus Torvalds 已提交
2474 2475 2476
		return;
	}

2477
	tlan_mii_read_reg(dev, TLAN_PHY_MAX_ADDR, MII_GEN_ID_HI, &hi);
2478

2479
	if (hi != 0xffff)
L
Linus Torvalds 已提交
2480
		priv->phy[0] = TLAN_PHY_MAX_ADDR;
2481
	else
L
Linus Torvalds 已提交
2482 2483 2484
		priv->phy[0] = TLAN_PHY_NONE;

	priv->phy[1] = TLAN_PHY_NONE;
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495
	for (phy = 0; phy <= TLAN_PHY_MAX_ADDR; phy++) {
		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &control);
		tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &hi);
		tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &lo);
		if ((control != 0xffff) ||
		    (hi != 0xffff) || (lo != 0xffff)) {
			TLAN_DBG(TLAN_DEBUG_GNRL,
				 "PHY found at %02x %04x %04x %04x\n",
				 phy, control, hi, lo);
			if ((priv->phy[1] == TLAN_PHY_NONE) &&
			    (phy != TLAN_PHY_MAX_ADDR)) {
L
Linus Torvalds 已提交
2496 2497 2498 2499 2500
				priv->phy[1] = phy;
			}
		}
	}

2501 2502 2503 2504 2505
	if (priv->phy[1] != TLAN_PHY_NONE)
		priv->phy_num = 1;
	else if (priv->phy[0] != TLAN_PHY_NONE)
		priv->phy_num = 0;
	else
2506
		netdev_info(dev, "Cannot initialize device, no PHY was found!\n");
L
Linus Torvalds 已提交
2507

2508
}
L
Linus Torvalds 已提交
2509 2510 2511 2512




2513
static void tlan_phy_power_down(struct net_device *dev)
L
Linus Torvalds 已提交
2514
{
2515
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2516 2517
	u16		value;

2518
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering down PHY(s).\n", dev->name);
L
Linus Torvalds 已提交
2519
	value = MII_GC_PDOWN | MII_GC_LOOPBK | MII_GC_ISOLATE;
2520 2521
	tlan_mii_sync(dev->base_addr);
	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
2522 2523 2524 2525
	if ((priv->phy_num == 0) && (priv->phy[1] != TLAN_PHY_NONE)) {
		/* if using internal PHY, the external PHY must be powered on */
		if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10)
			value = MII_GC_ISOLATE; /* just isolate it from MII */
2526 2527
		tlan_mii_sync(dev->base_addr);
		tlan_mii_write_reg(dev, priv->phy[1], MII_GEN_CTL, value);
L
Linus Torvalds 已提交
2528 2529 2530 2531 2532 2533
	}

	/* Wait for 50 ms and powerup
	 * This is abitrary.  It is intended to make sure the
	 * transceiver settles.
	 */
2534
	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_PUP);
L
Linus Torvalds 已提交
2535

2536
}
L
Linus Torvalds 已提交
2537 2538 2539 2540




2541
static void tlan_phy_power_up(struct net_device *dev)
L
Linus Torvalds 已提交
2542
{
2543
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2544 2545
	u16		value;

2546 2547
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering up PHY.\n", dev->name);
	tlan_mii_sync(dev->base_addr);
L
Linus Torvalds 已提交
2548
	value = MII_GC_LOOPBK;
2549 2550
	tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
	tlan_mii_sync(dev->base_addr);
L
Linus Torvalds 已提交
2551 2552 2553 2554
	/* Wait for 500 ms and reset the
	 * transceiver.  The TLAN docs say both 50 ms and
	 * 500 ms, so do the longer, just in case.
	 */
2555
	tlan_set_timer(dev, msecs_to_jiffies(500), TLAN_TIMER_PHY_RESET);
L
Linus Torvalds 已提交
2556

2557
}
L
Linus Torvalds 已提交
2558 2559 2560 2561




2562
static void tlan_phy_reset(struct net_device *dev)
L
Linus Torvalds 已提交
2563
{
2564
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2565 2566
	u16		phy;
	u16		value;
O
Ondrej Zary 已提交
2567
	unsigned long timeout = jiffies + HZ;
L
Linus Torvalds 已提交
2568

2569
	phy = priv->phy[priv->phy_num];
L
Linus Torvalds 已提交
2570

M
Masanari Iida 已提交
2571
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Resetting PHY.\n", dev->name);
2572
	tlan_mii_sync(dev->base_addr);
L
Linus Torvalds 已提交
2573
	value = MII_GC_LOOPBK | MII_GC_RESET;
2574
	tlan_mii_write_reg(dev, phy, MII_GEN_CTL, value);
O
Ondrej Zary 已提交
2575
	do {
2576
		tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &value);
O
Ondrej Zary 已提交
2577 2578 2579 2580 2581
		if (time_after(jiffies, timeout)) {
			netdev_err(dev, "PHY reset timeout\n");
			return;
		}
	} while (value & MII_GC_RESET);
L
Linus Torvalds 已提交
2582 2583 2584 2585 2586

	/* Wait for 500 ms and initialize.
	 * I don't remember why I wait this long.
	 * I've changed this to 50ms, as it seems long enough.
	 */
2587
	tlan_set_timer(dev, msecs_to_jiffies(50), TLAN_TIMER_PHY_START_LINK);
L
Linus Torvalds 已提交
2588

2589
}
L
Linus Torvalds 已提交
2590 2591 2592 2593




2594
static void tlan_phy_start_link(struct net_device *dev)
L
Linus Torvalds 已提交
2595
{
2596
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602 2603
	u16		ability;
	u16		control;
	u16		data;
	u16		phy;
	u16		status;
	u16		tctl;

2604 2605 2606 2607
	phy = priv->phy[priv->phy_num];
	TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Trying to activate link.\n", dev->name);
	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &ability);
L
Linus Torvalds 已提交
2608

2609 2610
	if ((status & MII_GS_AUTONEG) &&
	    (!priv->aui)) {
L
Linus Torvalds 已提交
2611
		ability = status >> 11;
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
		if (priv->speed  == TLAN_SPEED_10 &&
		    priv->duplex == TLAN_DUPLEX_HALF) {
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0000);
		} else if (priv->speed == TLAN_SPEED_10 &&
			   priv->duplex == TLAN_DUPLEX_FULL) {
			priv->tlan_full_duplex = true;
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0100);
		} else if (priv->speed == TLAN_SPEED_100 &&
			   priv->duplex == TLAN_DUPLEX_HALF) {
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2000);
		} else if (priv->speed == TLAN_SPEED_100 &&
			   priv->duplex == TLAN_DUPLEX_FULL) {
			priv->tlan_full_duplex = true;
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2100);
L
Linus Torvalds 已提交
2626
		} else {
2627

L
Linus Torvalds 已提交
2628
			/* Set Auto-Neg advertisement */
2629 2630
			tlan_mii_write_reg(dev, phy, MII_AN_ADV,
					   (ability << 5) | 1);
L
Linus Torvalds 已提交
2631
			/* Enablee Auto-Neg */
2632
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1000);
L
Linus Torvalds 已提交
2633
			/* Restart Auto-Neg */
2634
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1200);
L
Linus Torvalds 已提交
2635
			/* Wait for 4 sec for autonegotiation
2636 2637 2638 2639
			 * to complete.  The max spec time is less than this
			 * but the card need additional time to start AN.
			 * .5 sec should be plenty extra.
			 */
2640
			netdev_info(dev, "Starting autonegotiation\n");
2641
			tlan_set_timer(dev, (2*HZ), TLAN_TIMER_PHY_FINISH_AN);
L
Linus Torvalds 已提交
2642 2643
			return;
		}
2644 2645 2646

	}

2647 2648 2649 2650 2651
	if ((priv->aui) && (priv->phy_num != 0)) {
		priv->phy_num = 0;
		data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN
			| TLAN_NET_CFG_PHY_EN;
		tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, data);
2652
		tlan_set_timer(dev, msecs_to_jiffies(40), TLAN_TIMER_PHY_PDOWN);
L
Linus Torvalds 已提交
2653
		return;
2654
	} else if (priv->phy_num == 0) {
L
Linus Torvalds 已提交
2655
		control = 0;
2656 2657 2658
		tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tctl);
		if (priv->aui) {
			tctl |= TLAN_TC_AUISEL;
2659
		} else {
2660 2661
			tctl &= ~TLAN_TC_AUISEL;
			if (priv->duplex == TLAN_DUPLEX_FULL) {
L
Linus Torvalds 已提交
2662
				control |= MII_GC_DUPLEX;
2663
				priv->tlan_full_duplex = true;
L
Linus Torvalds 已提交
2664
			}
2665
			if (priv->speed == TLAN_SPEED_100)
L
Linus Torvalds 已提交
2666 2667
				control |= MII_GC_SPEEDSEL;
		}
2668 2669
		tlan_mii_write_reg(dev, phy, MII_GEN_CTL, control);
		tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tctl);
L
Linus Torvalds 已提交
2670 2671 2672 2673 2674
	}

	/* Wait for 2 sec to give the transceiver time
	 * to establish link.
	 */
2675
	tlan_set_timer(dev, (4*HZ), TLAN_TIMER_FINISH_RESET);
L
Linus Torvalds 已提交
2676

2677
}
L
Linus Torvalds 已提交
2678 2679 2680 2681




2682
static void tlan_phy_finish_auto_neg(struct net_device *dev)
L
Linus Torvalds 已提交
2683
{
2684
	struct tlan_priv	*priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2685 2686 2687 2688 2689
	u16		an_adv;
	u16		an_lpa;
	u16		mode;
	u16		phy;
	u16		status;
2690

2691
	phy = priv->phy[priv->phy_num];
L
Linus Torvalds 已提交
2692

2693 2694 2695
	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
	udelay(1000);
	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
L
Linus Torvalds 已提交
2696

2697
	if (!(status & MII_GS_AUTOCMPLT)) {
L
Linus Torvalds 已提交
2698 2699 2700
		/* Wait for 8 sec to give the process
		 * more time.  Perhaps we should fail after a while.
		 */
O
Ondrej Zary 已提交
2701
		tlan_set_timer(dev, 2 * HZ, TLAN_TIMER_PHY_FINISH_AN);
L
Linus Torvalds 已提交
2702 2703 2704
		return;
	}

2705
	netdev_info(dev, "Autonegotiation complete\n");
2706 2707
	tlan_mii_read_reg(dev, phy, MII_AN_ADV, &an_adv);
	tlan_mii_read_reg(dev, phy, MII_AN_LPA, &an_lpa);
L
Linus Torvalds 已提交
2708
	mode = an_adv & an_lpa & 0x03E0;
2709 2710 2711 2712 2713
	if (mode & 0x0100)
		priv->tlan_full_duplex = true;
	else if (!(mode & 0x0080) && (mode & 0x0040))
		priv->tlan_full_duplex = true;

2714
	/* switch to internal PHY for 10 Mbps */
2715 2716 2717 2718
	if ((!(mode & 0x0180)) &&
	    (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) &&
	    (priv->phy_num != 0)) {
		priv->phy_num = 0;
2719
		tlan_set_timer(dev, msecs_to_jiffies(400), TLAN_TIMER_PHY_PDOWN);
L
Linus Torvalds 已提交
2720 2721 2722
		return;
	}

2723 2724 2725 2726 2727
	if (priv->phy_num == 0) {
		if ((priv->duplex == TLAN_DUPLEX_FULL) ||
		    (an_adv & an_lpa & 0x0040)) {
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
					   MII_GC_AUTOENB | MII_GC_DUPLEX);
2728
			netdev_info(dev, "Starting internal PHY with FULL-DUPLEX\n");
L
Linus Torvalds 已提交
2729
		} else {
2730 2731
			tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
					   MII_GC_AUTOENB);
2732
			netdev_info(dev, "Starting internal PHY with HALF-DUPLEX\n");
L
Linus Torvalds 已提交
2733 2734 2735 2736 2737
		}
	}

	/* Wait for 100 ms.  No reason in partiticular.
	 */
2738
	tlan_set_timer(dev, msecs_to_jiffies(100), TLAN_TIMER_FINISH_RESET);
2739

2740
}
L
Linus Torvalds 已提交
2741 2742


2743 2744 2745 2746 2747 2748 2749 2750
/*********************************************************************
 *
 *     tlan_phy_monitor
 *
 *     Returns:
 *	      None
 *
 *     Params:
O
Ondrej Zary 已提交
2751
 *	      data	     The device structure of this device.
2752 2753 2754
 *
 *
 *     This function monitors PHY condition by reading the status
O
Ondrej Zary 已提交
2755 2756
 *     register via the MII bus, controls LINK LED and notifies the
 *     kernel about link state.
2757 2758 2759
 *
 *******************************************************************/

2760
static void tlan_phy_monitor(struct timer_list *t)
L
Linus Torvalds 已提交
2761
{
2762 2763
	struct tlan_priv *priv = from_timer(priv, t, media_timer);
	struct net_device *dev = priv->dev;
L
Linus Torvalds 已提交
2764 2765 2766
	u16     phy;
	u16     phy_status;

2767
	phy = priv->phy[priv->phy_num];
L
Linus Torvalds 已提交
2768

2769 2770
	/* Get PHY status register */
	tlan_mii_read_reg(dev, phy, MII_GEN_STS, &phy_status);
L
Linus Torvalds 已提交
2771

2772 2773
	/* Check if link has been lost */
	if (!(phy_status & MII_GS_LINK)) {
O
Ondrej Zary 已提交
2774
		if (netif_carrier_ok(dev)) {
2775 2776
			printk(KERN_DEBUG "TLAN: %s has lost link\n",
			       dev->name);
O
Ondrej Zary 已提交
2777
			tlan_dio_write8(dev->base_addr, TLAN_LED_REG, 0);
2778
			netif_carrier_off(dev);
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789
			if (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) {
				/* power down internal PHY */
				u16 data = MII_GC_PDOWN | MII_GC_LOOPBK |
					   MII_GC_ISOLATE;

				tlan_mii_sync(dev->base_addr);
				tlan_mii_write_reg(dev, priv->phy[0],
						   MII_GEN_CTL, data);
				/* set to external PHY */
				priv->phy_num = 1;
				/* restart autonegotiation */
2790
				tlan_set_timer(dev, msecs_to_jiffies(400),
2791 2792 2793
					       TLAN_TIMER_PHY_PDOWN);
				return;
			}
L
Linus Torvalds 已提交
2794 2795 2796
		}
	}

2797
	/* Link restablished? */
O
Ondrej Zary 已提交
2798 2799
	if ((phy_status & MII_GS_LINK) && !netif_carrier_ok(dev)) {
		tlan_dio_write8(dev->base_addr, TLAN_LED_REG, TLAN_LED_LINK);
2800 2801
		printk(KERN_DEBUG "TLAN: %s has reestablished link\n",
		       dev->name);
2802
		netif_carrier_on(dev);
2803
	}
O
Ondrej Zary 已提交
2804 2805
	priv->media_timer.expires = jiffies + HZ;
	add_timer(&priv->media_timer);
2806
}
L
Linus Torvalds 已提交
2807 2808 2809 2810 2811


/*****************************************************************************
******************************************************************************

2812
ThunderLAN driver MII routines
L
Linus Torvalds 已提交
2813

2814 2815
these routines are based on the information in chap. 2 of the
"ThunderLAN Programmer's Guide", pp. 15-24.
L
Linus Torvalds 已提交
2816 2817 2818 2819 2820

******************************************************************************
*****************************************************************************/


2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
/***************************************************************
 *	tlan_mii_read_reg
 *
 *	Returns:
 *		false	if ack received ok
 *		true	if no ack received or other error
 *
 *	Parms:
 *		dev		The device structure containing
 *				The io address and interrupt count
 *				for this device.
 *		phy		The address of the PHY to be queried.
 *		reg		The register whose contents are to be
 *				retrieved.
 *		val		A pointer to a variable to store the
 *				retrieved value.
 *
 *	This function uses the TLAN's MII bus to retrieve the contents
 *	of a given register on a PHY.  It sends the appropriate info
 *	and then reads the 16-bit register value from the MII bus via
 *	the TLAN SIO register.
 *
 **************************************************************/

static bool
tlan_mii_read_reg(struct net_device *dev, u16 phy, u16 reg, u16 *val)
L
Linus Torvalds 已提交
2847 2848 2849
{
	u8	nack;
	u16	sio, tmp;
2850
	u32	i;
2851
	bool	err;
L
Linus Torvalds 已提交
2852
	int	minten;
2853
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
2854 2855
	unsigned long flags = 0;

2856
	err = false;
L
Linus Torvalds 已提交
2857 2858
	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
2859

L
Linus Torvalds 已提交
2860 2861 2862
	if (!in_irq())
		spin_lock_irqsave(&priv->lock, flags);

2863
	tlan_mii_sync(dev->base_addr);
L
Linus Torvalds 已提交
2864

2865 2866 2867
	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
	if (minten)
		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
L
Linus Torvalds 已提交
2868

2869 2870 2871 2872
	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* read  (10b) */
	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
L
Linus Torvalds 已提交
2873 2874


2875
	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);	/* change direction */
L
Linus Torvalds 已提交
2876

2877 2878 2879
	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* clock idle bit */
	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* wait 300ns */
L
Linus Torvalds 已提交
2880

2881 2882 2883
	nack = tlan_get_bit(TLAN_NET_SIO_MDATA, sio);	/* check for ACK */
	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);		/* finish ACK */
	if (nack) {					/* no ACK, so fake it */
L
Linus Torvalds 已提交
2884
		for (i = 0; i < 16; i++) {
2885 2886
			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
2887 2888
		}
		tmp = 0xffff;
2889
		err = true;
L
Linus Torvalds 已提交
2890 2891
	} else {					/* ACK, so read data */
		for (tmp = 0, i = 0x8000; i; i >>= 1) {
2892 2893
			tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
			if (tlan_get_bit(TLAN_NET_SIO_MDATA, sio))
L
Linus Torvalds 已提交
2894
				tmp |= i;
2895
			tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
2896 2897 2898 2899
		}
	}


2900 2901
	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);		/* idle cycle */
	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
2902

2903 2904
	if (minten)
		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
L
Linus Torvalds 已提交
2905 2906

	*val = tmp;
2907

L
Linus Torvalds 已提交
2908 2909 2910 2911 2912
	if (!in_irq())
		spin_unlock_irqrestore(&priv->lock, flags);

	return err;

2913
}
L
Linus Torvalds 已提交
2914 2915 2916 2917




2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934
/***************************************************************
 *	tlan_mii_send_data
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		base_port	The base IO port of the adapter	in
 *				question.
 *		dev		The address of the PHY to be queried.
 *		data		The value to be placed on the MII bus.
 *		num_bits	The number of bits in data that are to
 *				be placed on the MII bus.
 *
 *	This function sends on sequence of bits on the MII
 *	configuration bus.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2935

2936
static void tlan_mii_send_data(u16 base_port, u32 data, unsigned num_bits)
L
Linus Torvalds 已提交
2937 2938 2939 2940
{
	u16 sio;
	u32 i;

2941
	if (num_bits == 0)
L
Linus Torvalds 已提交
2942 2943
		return;

2944
	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
2945
	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
2946
	tlan_set_bit(TLAN_NET_SIO_MTXEN, sio);
L
Linus Torvalds 已提交
2947

2948 2949 2950 2951 2952
	for (i = (0x1 << (num_bits - 1)); i; i >>= 1) {
		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
		if (data & i)
			tlan_set_bit(TLAN_NET_SIO_MDATA, sio);
L
Linus Torvalds 已提交
2953
		else
2954 2955 2956
			tlan_clear_bit(TLAN_NET_SIO_MDATA, sio);
		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
		(void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
2957 2958
	}

2959
}
L
Linus Torvalds 已提交
2960 2961 2962 2963




2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
/***************************************************************
 *	TLan_MiiSync
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		base_port	The base IO port of the adapter in
 *				question.
 *
 *	This functions syncs all PHYs in terms of the MII configuration
 *	bus.
 *
 **************************************************************/
L
Linus Torvalds 已提交
2977

2978
static void tlan_mii_sync(u16 base_port)
L
Linus Torvalds 已提交
2979 2980 2981 2982
{
	int i;
	u16 sio;

2983
	outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
2984 2985
	sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;

2986 2987 2988 2989
	tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);
	for (i = 0; i < 32; i++) {
		tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
		tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
2990 2991
	}

2992
}
L
Linus Torvalds 已提交
2993 2994 2995 2996




2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
/***************************************************************
 *	tlan_mii_write_reg
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		dev		The device structure for the device
 *				to write to.
 *		phy		The address of the PHY to be written to.
 *		reg		The register whose contents are to be
 *				written.
 *		val		The value to be written to the register.
 *
 *	This function uses the TLAN's MII bus to write the contents of a
 *	given register on a PHY.  It sends the appropriate info and then
 *	writes the 16-bit register value from the MII configuration bus
 *	via the TLAN SIO register.
 *
 **************************************************************/
L
Linus Torvalds 已提交
3016

3017 3018
static void
tlan_mii_write_reg(struct net_device *dev, u16 phy, u16 reg, u16 val)
L
Linus Torvalds 已提交
3019 3020 3021 3022
{
	u16	sio;
	int	minten;
	unsigned long flags = 0;
3023
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
3024 3025 3026

	outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
	sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
3027

L
Linus Torvalds 已提交
3028 3029 3030
	if (!in_irq())
		spin_lock_irqsave(&priv->lock, flags);

3031
	tlan_mii_sync(dev->base_addr);
L
Linus Torvalds 已提交
3032

3033 3034 3035
	minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
	if (minten)
		tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
L
Linus Torvalds 已提交
3036

3037 3038 3039 3040
	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* start (01b) */
	tlan_mii_send_data(dev->base_addr, 0x1, 2);	/* write (01b) */
	tlan_mii_send_data(dev->base_addr, phy, 5);	/* device #      */
	tlan_mii_send_data(dev->base_addr, reg, 5);	/* register #    */
L
Linus Torvalds 已提交
3041

3042 3043
	tlan_mii_send_data(dev->base_addr, 0x2, 2);	/* send ACK */
	tlan_mii_send_data(dev->base_addr, val, 16);	/* send data */
L
Linus Torvalds 已提交
3044

3045 3046
	tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);	/* idle cycle */
	tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
L
Linus Torvalds 已提交
3047

3048 3049
	if (minten)
		tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
3050

L
Linus Torvalds 已提交
3051 3052 3053
	if (!in_irq())
		spin_unlock_irqrestore(&priv->lock, flags);

3054
}
L
Linus Torvalds 已提交
3055 3056 3057 3058 3059 3060 3061




/*****************************************************************************
******************************************************************************

3062
ThunderLAN driver eeprom routines
L
Linus Torvalds 已提交
3063

3064 3065 3066 3067
the Compaq netelligent 10 and 10/100 cards use a microchip 24C02A
EEPROM.  these functions are based on information in microchip's
data sheet.  I don't know how well this functions will work with
other Eeproms.
L
Linus Torvalds 已提交
3068 3069 3070 3071 3072

******************************************************************************
*****************************************************************************/


3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088
/***************************************************************
 *	tlan_ee_send_start
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		io_base		The IO port base address for the
 *				TLAN device with the EEPROM to
 *				use.
 *
 *	This function sends a start cycle to an EEPROM attached
 *	to a TLAN chip.
 *
 **************************************************************/

static void tlan_ee_send_start(u16 io_base)
L
Linus Torvalds 已提交
3089 3090 3091
{
	u16	sio;

3092
	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
3093 3094
	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;

3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128
	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
	tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
	tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);

}




/***************************************************************
 *	tlan_ee_send_byte
 *
 *	Returns:
 *		If the correct ack was received, 0, otherwise 1
 *	Parms:	io_base		The IO port base address for the
 *				TLAN device with the EEPROM to
 *				use.
 *		data		The 8 bits of information to
 *				send to the EEPROM.
 *		stop		If TLAN_EEPROM_STOP is passed, a
 *				stop cycle is sent after the
 *				byte is sent after the ack is
 *				read.
 *
 *	This function sends a byte on the serial EEPROM line,
 *	driving the clock to send each bit. The function then
 *	reverses transmission direction and reads an acknowledge
 *	bit.
 *
 **************************************************************/

static int tlan_ee_send_byte(u16 io_base, u8 data, int stop)
L
Linus Torvalds 已提交
3129 3130 3131 3132 3133
{
	int	err;
	u8	place;
	u16	sio;

3134
	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
3135 3136 3137
	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;

	/* Assume clock is low, tx is enabled; */
3138 3139 3140
	for (place = 0x80; place != 0; place >>= 1) {
		if (place & data)
			tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
L
Linus Torvalds 已提交
3141
		else
3142 3143 3144
			tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
L
Linus Torvalds 已提交
3145
	}
3146 3147 3148 3149 3150
	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
	tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
	err = tlan_get_bit(TLAN_NET_SIO_EDATA, sio);
	tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
L
Linus Torvalds 已提交
3151

3152
	if ((!err) && stop) {
S
Stephen Hemminger 已提交
3153
		/* STOP, raise data while clock is high */
3154 3155 3156
		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
L
Linus Torvalds 已提交
3157 3158
	}

3159
	return err;
L
Linus Torvalds 已提交
3160

3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190
}




/***************************************************************
 *	tlan_ee_receive_byte
 *
 *	Returns:
 *		Nothing
 *	Parms:
 *		io_base		The IO port base address for the
 *				TLAN device with the EEPROM to
 *				use.
 *		data		An address to a char to hold the
 *				data sent from the EEPROM.
 *		stop		If TLAN_EEPROM_STOP is passed, a
 *				stop cycle is sent after the
 *				byte is received, and no ack is
 *				sent.
 *
 *	This function receives 8 bits of data from the EEPROM
 *	over the serial link.  It then sends and ack bit, or no
 *	ack and a stop bit.  This function is used to retrieve
 *	data after the address of a byte in the EEPROM has been
 *	sent.
 *
 **************************************************************/

static void tlan_ee_receive_byte(u16 io_base, u8 *data, int stop)
L
Linus Torvalds 已提交
3191 3192 3193 3194
{
	u8  place;
	u16 sio;

3195
	outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
L
Linus Torvalds 已提交
3196 3197 3198 3199
	sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
	*data = 0;

	/* Assume clock is low, tx is enabled; */
3200 3201 3202 3203
	tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
	for (place = 0x80; place; place >>= 1) {
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		if (tlan_get_bit(TLAN_NET_SIO_EDATA, sio))
L
Linus Torvalds 已提交
3204
			*data |= place;
3205
		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
L
Linus Torvalds 已提交
3206 3207
	}

3208 3209 3210 3211 3212
	tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
	if (!stop) {
		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio); /* ack = 0 */
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
L
Linus Torvalds 已提交
3213
	} else {
3214 3215 3216
		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);	/* no ack = 1 (?) */
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
S
Stephen Hemminger 已提交
3217
		/* STOP, raise data while clock is high */
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249
		tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
		tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
		tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
	}

}




/***************************************************************
 *	tlan_ee_read_byte
 *
 *	Returns:
 *		No error = 0, else, the stage at which the error
 *		occurred.
 *	Parms:
 *		io_base		The IO port base address for the
 *				TLAN device with the EEPROM to
 *				use.
 *		ee_addr		The address of the byte in the
 *				EEPROM whose contents are to be
 *				retrieved.
 *		data		An address to a char to hold the
 *				data obtained from the EEPROM.
 *
 *	This function reads a byte of information from an byte
 *	cell in the EEPROM.
 *
 **************************************************************/

static int tlan_ee_read_byte(struct net_device *dev, u8 ee_addr, u8 *data)
L
Linus Torvalds 已提交
3250 3251
{
	int err;
3252
	struct tlan_priv *priv = netdev_priv(dev);
L
Linus Torvalds 已提交
3253
	unsigned long flags = 0;
3254
	int ret = 0;
L
Linus Torvalds 已提交
3255 3256 3257

	spin_lock_irqsave(&priv->lock, flags);

3258 3259 3260 3261
	tlan_ee_send_start(dev->base_addr);
	err = tlan_ee_send_byte(dev->base_addr, 0xa0, TLAN_EEPROM_ACK);
	if (err) {
		ret = 1;
L
Linus Torvalds 已提交
3262 3263
		goto fail;
	}
3264 3265 3266
	err = tlan_ee_send_byte(dev->base_addr, ee_addr, TLAN_EEPROM_ACK);
	if (err) {
		ret = 2;
L
Linus Torvalds 已提交
3267 3268
		goto fail;
	}
3269 3270 3271 3272
	tlan_ee_send_start(dev->base_addr);
	err = tlan_ee_send_byte(dev->base_addr, 0xa1, TLAN_EEPROM_ACK);
	if (err) {
		ret = 3;
L
Linus Torvalds 已提交
3273 3274
		goto fail;
	}
3275
	tlan_ee_receive_byte(dev->base_addr, data, TLAN_EEPROM_STOP);
L
Linus Torvalds 已提交
3276 3277 3278 3279 3280
fail:
	spin_unlock_irqrestore(&priv->lock, flags);

	return ret;

3281
}
L
Linus Torvalds 已提交
3282 3283 3284