blob: cffbc0373fa9bd1a3aae0e69bd26b7f7c2f070c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*======================================================================
2
3 A PCMCIA ethernet driver for SMC91c92-based cards.
4
5 This driver supports Megahertz PCMCIA ethernet cards; and
6 Megahertz, Motorola, Ositech, and Psion Dacom ethernet/modem
7 multifunction cards.
8
9 Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
10
11 smc91c92_cs.c 1.122 2002/10/25 06:26:39
12
13 This driver contains code written by Donald Becker
14 (becker@scyld.com), Rowan Hughes (x-csrdh@jcu.edu.au),
15 David Hinds (dahinds@users.sourceforge.net), and Erik Stahlman
16 (erik@vt.edu). Donald wrote the SMC 91c92 code using parts of
17 Erik's SMC 91c94 driver. Rowan wrote a similar driver, and I've
18 incorporated some parts of his driver here. I (Dave) wrote most
19 of the PCMCIA glue code, and the Ositech support code. Kelly
20 Stephens (kstephen@holli.com) added support for the Motorola
21 Mariner, with help from Allen Brost.
22
23 This software may be used and distributed according to the terms of
24 the GNU General Public License, incorporated herein by reference.
25
26======================================================================*/
27
Joe Perches636b8112010-08-12 12:22:51 +000028#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/timer.h>
36#include <linux/interrupt.h>
37#include <linux/delay.h>
38#include <linux/crc32.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/skbuff.h>
42#include <linux/if_arp.h>
43#include <linux/ioport.h>
44#include <linux/ethtool.h>
45#include <linux/mii.h>
Marcelo Feitoza Parisi4851d3a2005-07-15 10:00:41 -070046#include <linux/jiffies.h>
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053047#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <pcmcia/cistpl.h>
50#include <pcmcia/cisreg.h>
51#include <pcmcia/ciscode.h>
52#include <pcmcia/ds.h>
Dominik Brodowski50db3fd2006-01-15 10:05:19 +010053#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/io.h>
56#include <asm/system.h>
57#include <asm/uaccess.h>
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*====================================================================*/
60
Arjan van de Venf71e1302006-03-03 21:33:57 -050061static const char *if_names[] = { "auto", "10baseT", "10base2"};
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053063/* Firmware name */
64#define FIRMWARE_NAME "ositech/Xilinx7OD.bin"
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Module parameters */
67
68MODULE_DESCRIPTION("SMC 91c92 series PCMCIA ethernet driver");
69MODULE_LICENSE("GPL");
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053070MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
73
74/*
75 Transceiver/media type.
76 0 = auto
77 1 = 10baseT (and autoselect if #define AUTOSELECT),
78 2 = AUI/10base2,
79*/
80INT_MODULE_PARM(if_port, 0);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83#define DRV_NAME "smc91c92_cs"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040084#define DRV_VERSION "1.123"
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*====================================================================*/
87
88/* Operational parameter that usually are not changed. */
89
90/* Time in jiffies before concluding Tx hung */
91#define TX_TIMEOUT ((400*HZ)/1000)
92
93/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
94#define INTR_WORK 4
95
96/* Times to check the check the chip before concluding that it doesn't
97 currently have room for another Tx packet. */
98#define MEMORY_WAIT_TIME 8
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100struct smc_private {
Dominik Brodowskifd238232006-03-05 10:45:09 +0100101 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 spinlock_t lock;
103 u_short manfid;
104 u_short cardid;
Stephen Hemminger6fb72982009-03-20 19:36:09 +0000105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct sk_buff *saved_skb;
107 int packets_waiting;
108 void __iomem *base;
109 u_short cfg;
110 struct timer_list media;
111 int watchdog, tx_err;
112 u_short media_status;
113 u_short fast_poll;
114 u_short link_status;
115 struct mii_if_info mii_if;
116 int duplex;
117 int rx_ovrn;
118};
119
120/* Special definitions for Megahertz multifunction cards */
121#define MEGAHERTZ_ISR 0x0380
122
123/* Special function registers for Motorola Mariner */
124#define MOT_LAN 0x0000
125#define MOT_UART 0x0020
126#define MOT_EEPROM 0x20
127
128#define MOT_NORMAL \
129(COR_LEVEL_REQ | COR_FUNC_ENA | COR_ADDR_DECODE | COR_IREQ_ENA)
130
131/* Special function registers for Ositech cards */
132#define OSITECH_AUI_CTL 0x0c
133#define OSITECH_PWRDOWN 0x0d
134#define OSITECH_RESET 0x0e
135#define OSITECH_ISR 0x0f
136#define OSITECH_AUI_PWR 0x0c
137#define OSITECH_RESET_ISR 0x0e
138
139#define OSI_AUI_PWR 0x40
140#define OSI_LAN_PWRDOWN 0x02
141#define OSI_MODEM_PWRDOWN 0x01
142#define OSI_LAN_RESET 0x02
143#define OSI_MODEM_RESET 0x01
144
145/* Symbolic constants for the SMC91c9* series chips, from Erik Stahlman. */
146#define BANK_SELECT 14 /* Window select register. */
147#define SMC_SELECT_BANK(x) { outw(x, ioaddr + BANK_SELECT); }
148
149/* Bank 0 registers. */
150#define TCR 0 /* transmit control register */
151#define TCR_CLEAR 0 /* do NOTHING */
152#define TCR_ENABLE 0x0001 /* if this is 1, we can transmit */
153#define TCR_PAD_EN 0x0080 /* pads short packets to 64 bytes */
154#define TCR_MONCSN 0x0400 /* Monitor Carrier. */
155#define TCR_FDUPLX 0x0800 /* Full duplex mode. */
156#define TCR_NORMAL TCR_ENABLE | TCR_PAD_EN
157
158#define EPH 2 /* Ethernet Protocol Handler report. */
159#define EPH_TX_SUC 0x0001
160#define EPH_SNGLCOL 0x0002
161#define EPH_MULCOL 0x0004
162#define EPH_LTX_MULT 0x0008
163#define EPH_16COL 0x0010
164#define EPH_SQET 0x0020
165#define EPH_LTX_BRD 0x0040
166#define EPH_TX_DEFR 0x0080
167#define EPH_LAT_COL 0x0200
168#define EPH_LOST_CAR 0x0400
169#define EPH_EXC_DEF 0x0800
170#define EPH_CTR_ROL 0x1000
171#define EPH_RX_OVRN 0x2000
172#define EPH_LINK_OK 0x4000
173#define EPH_TX_UNRN 0x8000
174#define MEMINFO 8 /* Memory Information Register */
175#define MEMCFG 10 /* Memory Configuration Register */
176
177/* Bank 1 registers. */
178#define CONFIG 0
179#define CFG_MII_SELECT 0x8000 /* 91C100 only */
180#define CFG_NO_WAIT 0x1000
181#define CFG_FULL_STEP 0x0400
182#define CFG_SET_SQLCH 0x0200
183#define CFG_AUI_SELECT 0x0100
184#define CFG_16BIT 0x0080
185#define CFG_DIS_LINK 0x0040
186#define CFG_STATIC 0x0030
187#define CFG_IRQ_SEL_1 0x0004
188#define CFG_IRQ_SEL_0 0x0002
189#define BASE_ADDR 2
190#define ADDR0 4
191#define GENERAL 10
192#define CONTROL 12
193#define CTL_STORE 0x0001
194#define CTL_RELOAD 0x0002
195#define CTL_EE_SELECT 0x0004
196#define CTL_TE_ENABLE 0x0020
197#define CTL_CR_ENABLE 0x0040
198#define CTL_LE_ENABLE 0x0080
199#define CTL_AUTO_RELEASE 0x0800
200#define CTL_POWERDOWN 0x2000
201
202/* Bank 2 registers. */
203#define MMU_CMD 0
204#define MC_ALLOC 0x20 /* or with number of 256 byte packets */
205#define MC_RESET 0x40
206#define MC_RELEASE 0x80 /* remove and release the current rx packet */
207#define MC_FREEPKT 0xA0 /* Release packet in PNR register */
208#define MC_ENQUEUE 0xC0 /* Enqueue the packet for transmit */
209#define PNR_ARR 2
210#define FIFO_PORTS 4
211#define FP_RXEMPTY 0x8000
212#define POINTER 6
213#define PTR_AUTO_INC 0x0040
214#define PTR_READ 0x2000
215#define PTR_AUTOINC 0x4000
216#define PTR_RCV 0x8000
217#define DATA_1 8
218#define INTERRUPT 12
219#define IM_RCV_INT 0x1
220#define IM_TX_INT 0x2
221#define IM_TX_EMPTY_INT 0x4
222#define IM_ALLOC_INT 0x8
223#define IM_RX_OVRN_INT 0x10
224#define IM_EPH_INT 0x20
225
226#define RCR 4
227enum RxCfg { RxAllMulti = 0x0004, RxPromisc = 0x0002,
228 RxEnable = 0x0100, RxStripCRC = 0x0200};
229#define RCR_SOFTRESET 0x8000 /* resets the chip */
230#define RCR_STRIP_CRC 0x200 /* strips CRC */
231#define RCR_ENABLE 0x100 /* IFF this is set, we can receive packets */
232#define RCR_ALMUL 0x4 /* receive all multicast packets */
233#define RCR_PROMISC 0x2 /* enable promiscuous mode */
234
235/* the normal settings for the RCR register : */
236#define RCR_NORMAL (RCR_STRIP_CRC | RCR_ENABLE)
237#define RCR_CLEAR 0x0 /* set it to a base state */
238#define COUNTER 6
239
240/* BANK 3 -- not the same values as in smc9194! */
241#define MULTICAST0 0
242#define MULTICAST2 2
243#define MULTICAST4 4
244#define MULTICAST6 6
245#define MGMT 8
246#define REVISION 0x0a
247
248/* Transmit status bits. */
249#define TS_SUCCESS 0x0001
250#define TS_16COL 0x0010
251#define TS_LATCOL 0x0200
252#define TS_LOSTCAR 0x0400
253
254/* Receive status bits. */
255#define RS_ALGNERR 0x8000
256#define RS_BADCRC 0x2000
257#define RS_ODDFRAME 0x1000
258#define RS_TOOLONG 0x0800
259#define RS_TOOSHORT 0x0400
260#define RS_MULTICAST 0x0001
261#define RS_ERRORS (RS_ALGNERR | RS_BADCRC | RS_TOOLONG | RS_TOOSHORT)
262
263#define set_bits(v, p) outw(inw(p)|(v), (p))
264#define mask_bits(v, p) outw(inw(p)&(v), (p))
265
266/*====================================================================*/
267
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100268static void smc91c92_detach(struct pcmcia_device *p_dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200269static int smc91c92_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200270static void smc91c92_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272static int smc_open(struct net_device *dev);
273static int smc_close(struct net_device *dev);
274static int smc_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
275static void smc_tx_timeout(struct net_device *dev);
Stephen Hemmingerdbf02fa2009-08-31 19:50:49 +0000276static netdev_tx_t smc_start_xmit(struct sk_buff *skb,
277 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100278static irqreturn_t smc_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void smc_rx(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static void set_rx_mode(struct net_device *dev);
281static int s9k_config(struct net_device *dev, struct ifmap *map);
282static void smc_set_xcvr(struct net_device *dev, int if_port);
283static void smc_reset(struct net_device *dev);
284static void media_check(u_long arg);
Olof Johansson906da802008-02-04 22:27:35 -0800285static void mdio_sync(unsigned int addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int mdio_read(struct net_device *dev, int phy_id, int loc);
287static void mdio_write(struct net_device *dev, int phy_id, int loc, int value);
288static int smc_link_ok(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400289static const struct ethtool_ops ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000291static const struct net_device_ops smc_netdev_ops = {
292 .ndo_open = smc_open,
293 .ndo_stop = smc_close,
294 .ndo_start_xmit = smc_start_xmit,
295 .ndo_tx_timeout = smc_tx_timeout,
296 .ndo_set_config = s9k_config,
297 .ndo_set_multicast_list = set_rx_mode,
Joe Perchesc061b182010-08-23 18:20:03 +0000298 .ndo_do_ioctl = smc_ioctl,
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000299 .ndo_change_mtu = eth_change_mtu,
300 .ndo_set_mac_address = eth_mac_addr,
301 .ndo_validate_addr = eth_validate_addr,
302};
303
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200304static int smc91c92_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct smc_private *smc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200309 dev_dbg(&link->dev, "smc91c92_attach()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Create new ethernet device */
312 dev = alloc_etherdev(sizeof(struct smc_private));
313 if (!dev)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100314 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200316 smc->p_dev = link;
Dominik Brodowski44b496f2010-06-11 04:44:55 +0000317 link->priv = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 spin_lock_init(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* The SMC91c92-specific entries in the device structure. */
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000322 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 SET_ETHTOOL_OPS(dev, &ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 smc->mii_if.dev = dev;
327 smc->mii_if.mdio_read = mdio_read;
328 smc->mii_if.mdio_write = mdio_write;
329 smc->mii_if.phy_id_mask = 0x1f;
330 smc->mii_if.reg_num_mask = 0x1f;
331
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200332 return smc91c92_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333} /* smc91c92_attach */
334
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200335static void smc91c92_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200339 dev_dbg(&link->dev, "smc91c92_detach\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Dominik Brodowskic7c2fa02010-03-20 19:39:26 +0100341 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100343 smc91c92_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 free_netdev(dev);
346} /* smc91c92_detach */
347
348/*====================================================================*/
349
350static int cvt_ascii_address(struct net_device *dev, char *s)
351{
352 int i, j, da, c;
353
354 if (strlen(s) != 12)
355 return -1;
356 for (i = 0; i < 6; i++) {
357 da = 0;
358 for (j = 0; j < 2; j++) {
359 c = *s++;
360 da <<= 4;
361 da += ((c >= '0') && (c <= '9')) ?
362 (c - '0') : ((c & 0x0f) + 9);
363 }
364 dev->dev_addr[i] = da;
365 }
366 return 0;
367}
368
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200369/*====================================================================
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 Configuration stuff for Megahertz cards
372
373 mhz_3288_power() is used to power up a 3288's ethernet chip.
374 mhz_mfc_config() handles socket setup for multifunction (1144
375 and 3288) cards. mhz_setup() gets a card's hardware ethernet
376 address.
377
378======================================================================*/
379
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200380static int mhz_3288_power(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct net_device *dev = link->priv;
383 struct smc_private *smc = netdev_priv(dev);
384 u_char tmp;
385
386 /* Read the ISR twice... */
387 readb(smc->base+MEGAHERTZ_ISR);
388 udelay(5);
389 readb(smc->base+MEGAHERTZ_ISR);
390
391 /* Pause 200ms... */
392 mdelay(200);
393
394 /* Now read and write the COR... */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200395 tmp = readb(smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 udelay(5);
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200397 writeb(tmp, smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400}
401
Dominik Brodowski00990e72010-07-30 13:13:46 +0200402static int mhz_mfc_config_check(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200403{
404 int k;
Dominik Brodowski00990e72010-07-30 13:13:46 +0200405 p_dev->io_lines = 16;
406 p_dev->resource[1]->start = p_dev->resource[0]->start;
407 p_dev->resource[1]->end = 8;
408 p_dev->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
409 p_dev->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
410 p_dev->resource[0]->end = 16;
411 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
412 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200413 for (k = 0; k < 0x400; k += 0x10) {
414 if (k & 0x80)
415 continue;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200416 p_dev->resource[0]->start = k ^ 0x300;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200417 if (!pcmcia_request_io(p_dev))
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200418 return 0;
419 }
420 return -ENODEV;
421}
422
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200423static int mhz_mfc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct net_device *dev = link->priv;
426 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200427 unsigned int offset;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200428 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Dominik Brodowski00990e72010-07-30 13:13:46 +0200430 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ |
431 CONF_AUTO_SET_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* The Megahertz combo cards have modem-like CIS entries, so
434 we have to explicitly try a bunch of port combinations. */
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200435 if (pcmcia_loop_config(link, mhz_mfc_config_check, NULL))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200436 return -ENODEV;
437
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200438 dev->base_addr = link->resource[0]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* Allocate a memory window, for accessing the ISR */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200441 link->resource[2]->flags = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
442 link->resource[2]->start = link->resource[2]->end = 0;
443 i = pcmcia_request_window(link, link->resource[2], 0);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200444 if (i != 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200445 return -ENODEV;
446
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200447 smc->base = ioremap(link->resource[2]->start,
448 resource_size(link->resource[2]));
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200449 offset = (smc->manfid == MANFID_MOTOROLA) ? link->config_base : 0;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200450 i = pcmcia_map_mem_page(link, link->resource[2], offset);
Joe Perches8e95a202009-12-03 07:58:21 +0000451 if ((i == 0) &&
452 (smc->manfid == MANFID_MEGAHERTZ) &&
453 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
454 mhz_3288_power(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200459static int pcmcia_get_versmac(struct pcmcia_device *p_dev,
460 tuple_t *tuple,
461 void *priv)
462{
463 struct net_device *dev = priv;
464 cisparse_t parse;
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700465 u8 *buf;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200466
467 if (pcmcia_parse_tuple(tuple, &parse))
468 return -EINVAL;
469
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700470 buf = parse.version_1.str + parse.version_1.ofs[3];
471
472 if ((parse.version_1.ns > 3) && (cvt_ascii_address(dev, buf) == 0))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200473 return 0;
474
475 return -EINVAL;
476};
477
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200478static int mhz_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200481 size_t len;
482 u8 *buf;
Yum Rayan4638aef42005-05-05 15:14:10 -0700483 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /* Read the station address from the CIS. It is stored as the last
486 (fourth) string in the Version 1 Version/ID tuple. */
Dominik Brodowski7d2e8d02009-10-18 18:22:32 +0200487 if ((link->prod_id[3]) &&
488 (cvt_ascii_address(dev, link->prod_id[3]) == 0))
489 return 0;
490
491 /* Workarounds for broken cards start here. */
Chuck Ebberta1a98b72008-02-13 19:47:11 -0500492 /* Ugh -- the EM1144 card has two VERS_1 tuples!?! */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200493 if (!pcmcia_loop_tuple(link, CISTPL_VERS_1, pcmcia_get_versmac, dev))
494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Another possibility: for the EM3288, in a special tuple */
Yum Rayan4638aef42005-05-05 15:14:10 -0700497 rc = -1;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200498 len = pcmcia_get_tuple(link, 0x81, &buf);
499 if (buf && len >= 13) {
500 buf[12] = '\0';
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700501 if (cvt_ascii_address(dev, buf) == 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200502 rc = 0;
503 }
504 kfree(buf);
505
506 return rc;
507};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509/*======================================================================
510
511 Configuration stuff for the Motorola Mariner
512
513 mot_config() writes directly to the Mariner configuration
514 registers because the CIS is just bogus.
515
516======================================================================*/
517
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200518static void mot_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct net_device *dev = link->priv;
521 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -0800522 unsigned int ioaddr = dev->base_addr;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200523 unsigned int iouart = link->resource[1]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* Set UART base address and force map with COR bit 1 */
526 writeb(iouart & 0xff, smc->base + MOT_UART + CISREG_IOBASE_0);
527 writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
528 writeb(MOT_NORMAL, smc->base + MOT_UART + CISREG_COR);
529
530 /* Set SMC base address and force map with COR bit 1 */
531 writeb(ioaddr & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_0);
532 writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
533 writeb(MOT_NORMAL, smc->base + MOT_LAN + CISREG_COR);
534
535 /* Wait for things to settle down */
536 mdelay(100);
537}
538
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200539static int mot_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800542 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int i, wait, loop;
544 u_int addr;
545
546 /* Read Ethernet address from Serial EEPROM */
547
548 for (i = 0; i < 3; i++) {
549 SMC_SELECT_BANK(2);
550 outw(MOT_EEPROM + i, ioaddr + POINTER);
551 SMC_SELECT_BANK(1);
552 outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
553
554 for (loop = wait = 0; loop < 200; loop++) {
555 udelay(10);
556 wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
557 if (wait == 0) break;
558 }
559
560 if (wait)
561 return -1;
562
563 addr = inw(ioaddr + GENERAL);
564 dev->dev_addr[2*i] = addr & 0xff;
565 dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
566 }
567
568 return 0;
569}
570
571/*====================================================================*/
572
Dominik Brodowski00990e72010-07-30 13:13:46 +0200573static int smc_configcheck(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200574{
Dominik Brodowski00990e72010-07-30 13:13:46 +0200575 p_dev->resource[0]->end = 16;
576 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
577 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
578
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200579 return pcmcia_request_io(p_dev);
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200580}
581
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200582static int smc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 int i;
586
Dominik Brodowski00990e72010-07-30 13:13:46 +0200587 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
588
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200589 i = pcmcia_loop_config(link, smc_configcheck, NULL);
590 if (!i)
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200591 dev->base_addr = link->resource[0]->start;
Yum Rayan4638aef42005-05-05 15:14:10 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return i;
594}
595
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200596
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200597static int smc_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* Check for a LAN function extension tuple */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200602 if (!pcmcia_get_mac_from_cis(link, dev))
603 return 0;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Try the third string in the Version 1 Version/ID tuple. */
Dominik Brodowskia9606fd2006-06-04 18:06:13 +0200606 if (link->prod_id[2]) {
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200607 if (cvt_ascii_address(dev, link->prod_id[2]) == 0)
608 return 0;
Yum Rayan4638aef42005-05-05 15:14:10 -0700609 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200610 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*====================================================================*/
614
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200615static int osi_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800618 static const unsigned int com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int i, j;
620
Dominik Brodowski00990e72010-07-30 13:13:46 +0200621 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200622 link->resource[0]->end = 64;
623 link->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
624 link->resource[1]->end = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Enable Hard Decode, LAN, Modem */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200627 link->io_lines = 16;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200628 link->config_index = 0x23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 for (i = j = 0; j < 4; j++) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200631 link->resource[1]->start = com[j];
632 i = pcmcia_request_io(link);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200633 if (i == 0)
634 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200636 if (i != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /* Fallback: turn off hard decode */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200638 link->config_index = 0x03;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200639 link->resource[1]->end = 0;
640 i = pcmcia_request_io(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200642 dev->base_addr = link->resource[0]->start + 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return i;
644}
645
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530646static int osi_load_firmware(struct pcmcia_device *link)
647{
648 const struct firmware *fw;
649 int i, err;
650
651 err = request_firmware(&fw, FIRMWARE_NAME, &link->dev);
652 if (err) {
653 pr_err("Failed to load firmware \"%s\"\n", FIRMWARE_NAME);
654 return err;
655 }
656
657 /* Download the Seven of Diamonds firmware */
658 for (i = 0; i < fw->size; i++) {
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200659 outb(fw->data[i], link->resource[0]->start + 2);
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530660 udelay(50);
661 }
662 release_firmware(fw);
663 return err;
664}
665
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200666static int pcmcia_osi_mac(struct pcmcia_device *p_dev,
667 tuple_t *tuple,
668 void *priv)
669{
670 struct net_device *dev = priv;
671 int i;
672
673 if (tuple->TupleDataLen < 8)
674 return -EINVAL;
675 if (tuple->TupleData[0] != 0x04)
676 return -EINVAL;
677 for (i = 0; i < 6; i++)
678 dev->dev_addr[i] = tuple->TupleData[i+2];
679 return 0;
680};
681
682
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200683static int osi_setup(struct pcmcia_device *link, u_short manfid, u_short cardid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200686 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /* Read the station address from tuple 0x90, subtuple 0x04 */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200689 if (pcmcia_loop_tuple(link, 0x90, pcmcia_osi_mac, dev))
690 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (((manfid == MANFID_OSITECH) &&
693 (cardid == PRODID_OSITECH_SEVEN)) ||
694 ((manfid == MANFID_PSION) &&
695 (cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530696 rc = osi_load_firmware(link);
697 if (rc)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200698 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else if (manfid == MANFID_OSITECH) {
700 /* Make sure both functions are powered up */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200701 set_bits(0x300, link->resource[0]->start + OSITECH_AUI_PWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* Now, turn on the interrupt for both card functions */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200703 set_bits(0x300, link->resource[0]->start + OSITECH_RESET_ISR);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200704 dev_dbg(&link->dev, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200705 inw(link->resource[0]->start + OSITECH_AUI_PWR),
706 inw(link->resource[0]->start + OSITECH_RESET_ISR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200711static int smc91c92_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100712{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100713 struct net_device *dev = link->priv;
714
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100715 if (link->open)
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100716 netif_device_detach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100717
718 return 0;
719}
720
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200721static int smc91c92_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100722{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100723 struct net_device *dev = link->priv;
724 struct smc_private *smc = netdev_priv(dev);
725 int i;
726
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100727 if ((smc->manfid == MANFID_MEGAHERTZ) &&
728 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
729 mhz_3288_power(link);
730 if (smc->manfid == MANFID_MOTOROLA)
731 mot_config(link);
732 if ((smc->manfid == MANFID_OSITECH) &&
733 (smc->cardid != PRODID_OSITECH_SEVEN)) {
734 /* Power up the card and enable interrupts */
735 set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
736 set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
737 }
738 if (((smc->manfid == MANFID_OSITECH) &&
739 (smc->cardid == PRODID_OSITECH_SEVEN)) ||
740 ((smc->manfid == MANFID_PSION) &&
741 (smc->cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530742 i = osi_load_firmware(link);
743 if (i) {
744 pr_err("smc91c92_cs: Failed to load firmware\n");
745 return i;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100746 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100747 }
748 if (link->open) {
749 smc_reset(dev);
750 netif_device_attach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100751 }
752
753 return 0;
754}
755
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*======================================================================
758
759 This verifies that the chip is some SMC91cXX variant, and returns
760 the revision code if successful. Otherwise, it returns -ENODEV.
761
762======================================================================*/
763
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200764static int check_sig(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800767 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int width;
769 u_short s;
770
771 SMC_SELECT_BANK(1);
772 if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
773 /* Try powering up the chip */
774 outw(0, ioaddr + CONTROL);
775 mdelay(55);
776 }
777
778 /* Try setting bus width */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200779 width = (link->resource[0]->flags == IO_DATA_PATH_WIDTH_AUTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 s = inb(ioaddr + CONFIG);
781 if (width)
782 s |= CFG_16BIT;
783 else
784 s &= ~CFG_16BIT;
785 outb(s, ioaddr + CONFIG);
786
787 /* Check Base Address Register to make sure bus width is OK */
788 s = inw(ioaddr + BASE_ADDR);
789 if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
790 ((s >> 8) != (s & 0xff))) {
791 SMC_SELECT_BANK(3);
792 s = inw(ioaddr + REVISION);
Eric Dumazet807540b2010-09-23 05:40:09 +0000793 return s & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 if (width) {
Joe Perches636b8112010-08-12 12:22:51 +0000797 pr_info("using 8-bit IO window\n");
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100798
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200799 smc91c92_suspend(link);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200800 pcmcia_fixup_iowidth(link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200801 smc91c92_resume(link);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100802 return check_sig(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 return -ENODEV;
805}
806
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200807static int smc91c92_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct net_device *dev = link->priv;
810 struct smc_private *smc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 char *name;
Dominik Brodowski74411c02011-07-29 18:27:34 +0200812 int i, rev, j = 0;
Olof Johansson906da802008-02-04 22:27:35 -0800813 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 u_long mir;
815
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200816 dev_dbg(&link->dev, "smc91c92_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dominik Brodowskiefd50582006-10-25 21:28:53 -0400818 smc->manfid = link->manf_id;
819 smc->cardid = link->card_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if ((smc->manfid == MANFID_OSITECH) &&
822 (smc->cardid != PRODID_OSITECH_SEVEN)) {
823 i = osi_config(link);
824 } else if ((smc->manfid == MANFID_MOTOROLA) ||
825 ((smc->manfid == MANFID_MEGAHERTZ) &&
826 ((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
827 (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
828 i = mhz_mfc_config(link);
829 } else {
830 i = smc_config(link);
831 }
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200832 if (i)
833 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Dominik Brodowskieb141202010-03-07 12:21:16 +0100835 i = pcmcia_request_irq(link, smc_interrupt);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200836 if (i)
837 goto config_failed;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200838 i = pcmcia_enable_device(link);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200839 if (i)
840 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (smc->manfid == MANFID_MOTOROLA)
843 mot_config(link);
844
Dominik Brodowskieb141202010-03-07 12:21:16 +0100845 dev->irq = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if ((if_port >= 0) && (if_port <= 2))
848 dev->if_port = if_port;
849 else
Joe Perches636b8112010-08-12 12:22:51 +0000850 dev_notice(&link->dev, "invalid if_port requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 switch (smc->manfid) {
853 case MANFID_OSITECH:
854 case MANFID_PSION:
855 i = osi_setup(link, smc->manfid, smc->cardid); break;
856 case MANFID_SMC:
857 case MANFID_NEW_MEDIA:
858 i = smc_setup(link); break;
859 case 0x128: /* For broken Megahertz cards */
860 case MANFID_MEGAHERTZ:
861 i = mhz_setup(link); break;
862 case MANFID_MOTOROLA:
863 default: /* get the hw address from EEPROM */
864 i = mot_setup(link); break;
865 }
866
867 if (i != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000868 dev_notice(&link->dev, "Unable to find hardware address.\n");
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700869 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
872 smc->duplex = 0;
873 smc->rx_ovrn = 0;
874
875 rev = check_sig(link);
876 name = "???";
877 if (rev > 0)
878 switch (rev >> 4) {
879 case 3: name = "92"; break;
880 case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
881 case 5: name = "95"; break;
882 case 7: name = "100"; break;
883 case 8: name = "100-FD"; break;
884 case 9: name = "110"; break;
885 }
886
887 ioaddr = dev->base_addr;
888 if (rev > 0) {
889 u_long mcr;
890 SMC_SELECT_BANK(0);
891 mir = inw(ioaddr + MEMINFO) & 0xff;
892 if (mir == 0xff) mir++;
893 /* Get scale factor for memory size */
894 mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
895 mir *= 128 * (1<<((mcr >> 9) & 7));
896 SMC_SELECT_BANK(1);
897 smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
898 smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
899 if (smc->manfid == MANFID_OSITECH)
900 smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
901 if ((rev >> 4) >= 7)
902 smc->cfg |= CFG_MII_SELECT;
903 } else
904 mir = 0;
905
906 if (smc->cfg & CFG_MII_SELECT) {
907 SMC_SELECT_BANK(3);
908
909 for (i = 0; i < 32; i++) {
910 j = mdio_read(dev, i, 1);
911 if ((j != 0) && (j != 0xffff)) break;
912 }
913 smc->mii_if.phy_id = (i < 32) ? i : -1;
914
915 SMC_SELECT_BANK(0);
916 }
917
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100918 SET_NETDEV_DEV(dev, &link->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (register_netdev(dev) != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000921 dev_err(&link->dev, "register_netdev() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 goto config_undo;
923 }
924
Joe Perches636b8112010-08-12 12:22:51 +0000925 netdev_info(dev, "smc91c%s rev %d: io %#3lx, irq %d, hw_addr %pM\n",
926 name, (rev & 0x0f), dev->base_addr, dev->irq, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (rev > 0) {
929 if (mir & 0x3ff)
Joe Perches636b8112010-08-12 12:22:51 +0000930 netdev_info(dev, " %lu byte", mir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 else
Joe Perches636b8112010-08-12 12:22:51 +0000932 netdev_info(dev, " %lu kb", mir>>10);
933 pr_cont(" buffer, %s xcvr\n",
934 (smc->cfg & CFG_MII_SELECT) ? "MII" : if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 if (smc->cfg & CFG_MII_SELECT) {
938 if (smc->mii_if.phy_id != -1) {
Joe Perches636b8112010-08-12 12:22:51 +0000939 netdev_dbg(dev, " MII transceiver at index %d, status %x\n",
940 smc->mii_if.phy_id, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 } else {
Joe Perches636b8112010-08-12 12:22:51 +0000942 netdev_notice(dev, " No MII transceivers found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947config_undo:
948 unregister_netdev(dev);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200949config_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 smc91c92_release(link);
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700951 free_netdev(dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200952 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953} /* smc91c92_config */
954
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200955static void smc91c92_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200957 dev_dbg(&link->dev, "smc91c92_release\n");
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200958 if (link->resource[2]->end) {
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100959 struct net_device *dev = link->priv;
960 struct smc_private *smc = netdev_priv(dev);
961 iounmap(smc->base);
962 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200963 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*======================================================================
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 MII interface support for SMC91cXX based cards
969======================================================================*/
970
971#define MDIO_SHIFT_CLK 0x04
972#define MDIO_DATA_OUT 0x01
973#define MDIO_DIR_WRITE 0x08
974#define MDIO_DATA_WRITE0 (MDIO_DIR_WRITE)
975#define MDIO_DATA_WRITE1 (MDIO_DIR_WRITE | MDIO_DATA_OUT)
976#define MDIO_DATA_READ 0x02
977
Olof Johansson906da802008-02-04 22:27:35 -0800978static void mdio_sync(unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 int bits;
981 for (bits = 0; bits < 32; bits++) {
982 outb(MDIO_DATA_WRITE1, addr);
983 outb(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
984 }
985}
986
987static int mdio_read(struct net_device *dev, int phy_id, int loc)
988{
Olof Johansson906da802008-02-04 22:27:35 -0800989 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
991 int i, retval = 0;
992
993 mdio_sync(addr);
994 for (i = 13; i >= 0; i--) {
995 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
996 outb(dat, addr);
997 outb(dat | MDIO_SHIFT_CLK, addr);
998 }
999 for (i = 19; i > 0; i--) {
1000 outb(0, addr);
1001 retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
1002 outb(MDIO_SHIFT_CLK, addr);
1003 }
1004 return (retval>>1) & 0xffff;
1005}
1006
1007static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
1008{
Olof Johansson906da802008-02-04 22:27:35 -08001009 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
1011 int i;
1012
1013 mdio_sync(addr);
1014 for (i = 31; i >= 0; i--) {
1015 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
1016 outb(dat, addr);
1017 outb(dat | MDIO_SHIFT_CLK, addr);
1018 }
1019 for (i = 1; i >= 0; i--) {
1020 outb(0, addr);
1021 outb(MDIO_SHIFT_CLK, addr);
1022 }
1023}
1024
1025/*======================================================================
1026
1027 The driver core code, most of which should be common with a
1028 non-PCMCIA implementation.
1029
1030======================================================================*/
1031
1032#ifdef PCMCIA_DEBUG
1033static void smc_dump(struct net_device *dev)
1034{
Olof Johansson906da802008-02-04 22:27:35 -08001035 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 u_short i, w, save;
1037 save = inw(ioaddr + BANK_SELECT);
1038 for (w = 0; w < 4; w++) {
1039 SMC_SELECT_BANK(w);
Joe Perches636b8112010-08-12 12:22:51 +00001040 netdev_printk(KERN_DEBUG, dev, "bank %d: ", w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 for (i = 0; i < 14; i += 2)
Joe Perches636b8112010-08-12 12:22:51 +00001042 pr_cont(" %04x", inw(ioaddr + i));
1043 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045 outw(save, ioaddr + BANK_SELECT);
1046}
1047#endif
1048
1049static int smc_open(struct net_device *dev)
1050{
1051 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001052 struct pcmcia_device *link = smc->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001054 dev_dbg(&link->dev, "%s: smc_open(%p), ID/Window %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 dev->name, dev, inw(dev->base_addr + BANK_SELECT));
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001056#ifdef PCMCIA_DEBUG
1057 smc_dump(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058#endif
1059
1060 /* Check that the PCMCIA card is still here. */
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001061 if (!pcmcia_dev_present(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return -ENODEV;
1063 /* Physical device present signature. */
1064 if (check_sig(link) < 0) {
Joe Perches636b8112010-08-12 12:22:51 +00001065 netdev_info(dev, "Yikes! Bad chip signature!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -ENODEV;
1067 }
1068 link->open++;
1069
1070 netif_start_queue(dev);
1071 smc->saved_skb = NULL;
1072 smc->packets_waiting = 0;
1073
1074 smc_reset(dev);
1075 init_timer(&smc->media);
Joe Perchesc061b182010-08-23 18:20:03 +00001076 smc->media.function = media_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 smc->media.data = (u_long) dev;
1078 smc->media.expires = jiffies + HZ;
1079 add_timer(&smc->media);
1080
1081 return 0;
1082} /* smc_open */
1083
1084/*====================================================================*/
1085
1086static int smc_close(struct net_device *dev)
1087{
1088 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001089 struct pcmcia_device *link = smc->p_dev;
Olof Johansson906da802008-02-04 22:27:35 -08001090 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001092 dev_dbg(&link->dev, "%s: smc_close(), status %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 dev->name, inw(ioaddr + BANK_SELECT));
1094
1095 netif_stop_queue(dev);
1096
1097 /* Shut off all interrupts, and turn off the Tx and Rx sections.
1098 Don't bother to check for chip present. */
1099 SMC_SELECT_BANK(2); /* Nominally paranoia, but do no assume... */
1100 outw(0, ioaddr + INTERRUPT);
1101 SMC_SELECT_BANK(0);
1102 mask_bits(0xff00, ioaddr + RCR);
1103 mask_bits(0xff00, ioaddr + TCR);
1104
1105 /* Put the chip into power-down mode. */
1106 SMC_SELECT_BANK(1);
1107 outw(CTL_POWERDOWN, ioaddr + CONTROL );
1108
1109 link->open--;
1110 del_timer_sync(&smc->media);
1111
1112 return 0;
1113} /* smc_close */
1114
1115/*======================================================================
1116
1117 Transfer a packet to the hardware and trigger the packet send.
1118 This may be called at either from either the Tx queue code
1119 or the interrupt handler.
1120
1121======================================================================*/
1122
1123static void smc_hardware_send_packet(struct net_device * dev)
1124{
1125 struct smc_private *smc = netdev_priv(dev);
1126 struct sk_buff *skb = smc->saved_skb;
Olof Johansson906da802008-02-04 22:27:35 -08001127 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 u_char packet_no;
1129
1130 if (!skb) {
Joe Perches636b8112010-08-12 12:22:51 +00001131 netdev_err(dev, "In XMIT with no packet to send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return;
1133 }
1134
1135 /* There should be a packet slot waiting. */
1136 packet_no = inw(ioaddr + PNR_ARR) >> 8;
1137 if (packet_no & 0x80) {
1138 /* If not, there is a hardware problem! Likely an ejected card. */
Joe Perches636b8112010-08-12 12:22:51 +00001139 netdev_warn(dev, "hardware Tx buffer allocation failed, status %#2.2x\n",
1140 packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 dev_kfree_skb_irq(skb);
1142 smc->saved_skb = NULL;
1143 netif_start_queue(dev);
1144 return;
1145 }
1146
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001147 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* The card should use the just-allocated buffer. */
1149 outw(packet_no, ioaddr + PNR_ARR);
1150 /* point to the beginning of the packet */
1151 outw(PTR_AUTOINC , ioaddr + POINTER);
1152
1153 /* Send the packet length (+6 for status, length and ctl byte)
1154 and the status word (set to zeros). */
1155 {
1156 u_char *buf = skb->data;
1157 u_int length = skb->len; /* The chip will pad to ethernet min. */
1158
Joe Perches636b8112010-08-12 12:22:51 +00001159 netdev_dbg(dev, "Trying to xmit packet of length %d\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* send the packet length: +6 for status word, length, and ctl */
1162 outw(0, ioaddr + DATA_1);
1163 outw(length + 6, ioaddr + DATA_1);
1164 outsw(ioaddr + DATA_1, buf, length >> 1);
1165
1166 /* The odd last byte, if there is one, goes in the control word. */
1167 outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
1168 }
1169
1170 /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
1171 outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
1172 (inw(ioaddr + INTERRUPT) & 0xff00),
1173 ioaddr + INTERRUPT);
1174
1175 /* The chip does the rest of the work. */
1176 outw(MC_ENQUEUE , ioaddr + MMU_CMD);
1177
1178 smc->saved_skb = NULL;
1179 dev_kfree_skb_irq(skb);
1180 dev->trans_start = jiffies;
1181 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184/*====================================================================*/
1185
1186static void smc_tx_timeout(struct net_device *dev)
1187{
1188 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001189 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Joe Perches636b8112010-08-12 12:22:51 +00001191 netdev_notice(dev, "transmit timed out, Tx_status %2.2x status %4.4x.\n",
1192 inw(ioaddr)&0xff, inw(ioaddr + 2));
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001193 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 smc_reset(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001195 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 smc->saved_skb = NULL;
1197 netif_wake_queue(dev);
1198}
1199
Stephen Hemmingerdbf02fa2009-08-31 19:50:49 +00001200static netdev_tx_t smc_start_xmit(struct sk_buff *skb,
1201 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001204 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 u_short num_pages;
1206 short time_out, ir;
Komuro85e27832007-07-23 21:36:06 +09001207 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 netif_stop_queue(dev);
1210
Joe Perches636b8112010-08-12 12:22:51 +00001211 netdev_dbg(dev, "smc_start_xmit(length = %d) called, status %04x\n",
1212 skb->len, inw(ioaddr + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (smc->saved_skb) {
1215 /* THIS SHOULD NEVER HAPPEN. */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001216 dev->stats.tx_aborted_errors++;
Joe Perches636b8112010-08-12 12:22:51 +00001217 netdev_printk(KERN_DEBUG, dev,
1218 "Internal error -- sent packet while busy\n");
Patrick McHardy5b548142009-06-12 06:22:29 +00001219 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 smc->saved_skb = skb;
1222
1223 num_pages = skb->len >> 8;
1224
1225 if (num_pages > 7) {
Joe Perches636b8112010-08-12 12:22:51 +00001226 netdev_err(dev, "Far too big packet error: %d pages\n", num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dev_kfree_skb (skb);
1228 smc->saved_skb = NULL;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001229 dev->stats.tx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001230 return NETDEV_TX_OK; /* Do not re-queue this packet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 /* A packet is now waiting. */
1233 smc->packets_waiting++;
1234
Komuro85e27832007-07-23 21:36:06 +09001235 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 SMC_SELECT_BANK(2); /* Paranoia, we should always be in window 2 */
1237
1238 /* need MC_RESET to keep the memory consistent. errata? */
1239 if (smc->rx_ovrn) {
1240 outw(MC_RESET, ioaddr + MMU_CMD);
1241 smc->rx_ovrn = 0;
1242 }
1243
1244 /* Allocate the memory; send the packet now if we win. */
1245 outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
1246 for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
1247 ir = inw(ioaddr+INTERRUPT);
1248 if (ir & IM_ALLOC_INT) {
1249 /* Acknowledge the interrupt, send the packet. */
1250 outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
1251 smc_hardware_send_packet(dev); /* Send the packet now.. */
Komuro85e27832007-07-23 21:36:06 +09001252 spin_unlock_irqrestore(&smc->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001253 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255 }
1256
1257 /* Otherwise defer until the Tx-space-allocated interrupt. */
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001258 pr_debug("%s: memory allocation deferred.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
Komuro85e27832007-07-23 21:36:06 +09001260 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Patrick McHardy6ed10652009-06-23 06:03:08 +00001262 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/*======================================================================
1266
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001267 Handle a Tx anomalous event. Entered while in Window 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269======================================================================*/
1270
1271static void smc_tx_err(struct net_device * dev)
1272{
1273 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001274 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
1276 int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
1277 int tx_status;
1278
1279 /* select this as the packet to read from */
1280 outw(packet_no, ioaddr + PNR_ARR);
1281
1282 /* read the first word from this packet */
1283 outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
1284
1285 tx_status = inw(ioaddr + DATA_1);
1286
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001287 dev->stats.tx_errors++;
1288 if (tx_status & TS_LOSTCAR) dev->stats.tx_carrier_errors++;
1289 if (tx_status & TS_LATCOL) dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (tx_status & TS_16COL) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001291 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 smc->tx_err++;
1293 }
1294
1295 if (tx_status & TS_SUCCESS) {
Joe Perches636b8112010-08-12 12:22:51 +00001296 netdev_notice(dev, "Successful packet caused error interrupt?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298 /* re-enable transmit */
1299 SMC_SELECT_BANK(0);
1300 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1301 SMC_SELECT_BANK(2);
1302
1303 outw(MC_FREEPKT, ioaddr + MMU_CMD); /* Free the packet memory. */
1304
1305 /* one less packet waiting for me */
1306 smc->packets_waiting--;
1307
1308 outw(saved_packet, ioaddr + PNR_ARR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311/*====================================================================*/
1312
1313static void smc_eph_irq(struct net_device *dev)
1314{
1315 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001316 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 u_short card_stats, ephs;
1318
1319 SMC_SELECT_BANK(0);
1320 ephs = inw(ioaddr + EPH);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001321 pr_debug("%s: Ethernet protocol handler interrupt, status"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 " %4.4x.\n", dev->name, ephs);
1323 /* Could be a counter roll-over warning: update stats. */
1324 card_stats = inw(ioaddr + COUNTER);
1325 /* single collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001326 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 card_stats >>= 4;
1328 /* multiple collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001329 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#if 0 /* These are for when linux supports these statistics */
1331 card_stats >>= 4; /* deferred */
1332 card_stats >>= 4; /* excess deferred */
1333#endif
1334 /* If we had a transmit error we must re-enable the transmitter. */
1335 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1336
1337 /* Clear a link error interrupt. */
1338 SMC_SELECT_BANK(1);
1339 outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
1340 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1341 ioaddr + CONTROL);
1342 SMC_SELECT_BANK(2);
1343}
1344
1345/*====================================================================*/
1346
David Howells7d12e782006-10-05 14:55:46 +01001347static irqreturn_t smc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct net_device *dev = dev_id;
1350 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001351 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 u_short saved_bank, saved_pointer, mask, status;
1353 unsigned int handled = 1;
1354 char bogus_cnt = INTR_WORK; /* Work we are willing to do. */
1355
1356 if (!netif_device_present(dev))
1357 return IRQ_NONE;
1358
1359 ioaddr = dev->base_addr;
1360
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001361 pr_debug("%s: SMC91c92 interrupt %d at %#x.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 irq, ioaddr);
1363
Komuro85e27832007-07-23 21:36:06 +09001364 spin_lock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 smc->watchdog = 0;
1366 saved_bank = inw(ioaddr + BANK_SELECT);
1367 if ((saved_bank & 0xff00) != 0x3300) {
1368 /* The device does not exist -- the card could be off-line, or
1369 maybe it has been ejected. */
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001370 pr_debug("%s: SMC91c92 interrupt %d for non-existent"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 "/ejected device.\n", dev->name, irq);
1372 handled = 0;
1373 goto irq_done;
1374 }
1375
1376 SMC_SELECT_BANK(2);
1377 saved_pointer = inw(ioaddr + POINTER);
1378 mask = inw(ioaddr + INTERRUPT) >> 8;
1379 /* clear all interrupts */
1380 outw(0, ioaddr + INTERRUPT);
1381
1382 do { /* read the status flag, and mask it */
1383 status = inw(ioaddr + INTERRUPT) & 0xff;
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001384 pr_debug("%s: Status is %#2.2x (mask %#2.2x).\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 status, mask);
1386 if ((status & mask) == 0) {
1387 if (bogus_cnt == INTR_WORK)
1388 handled = 0;
1389 break;
1390 }
1391 if (status & IM_RCV_INT) {
1392 /* Got a packet(s). */
1393 smc_rx(dev);
1394 }
1395 if (status & IM_TX_INT) {
1396 smc_tx_err(dev);
1397 outw(IM_TX_INT, ioaddr + INTERRUPT);
1398 }
1399 status &= mask;
1400 if (status & IM_TX_EMPTY_INT) {
1401 outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
1402 mask &= ~IM_TX_EMPTY_INT;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001403 dev->stats.tx_packets += smc->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 smc->packets_waiting = 0;
1405 }
1406 if (status & IM_ALLOC_INT) {
1407 /* Clear this interrupt so it doesn't happen again */
1408 mask &= ~IM_ALLOC_INT;
1409
1410 smc_hardware_send_packet(dev);
1411
1412 /* enable xmit interrupts based on this */
1413 mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
1414
1415 /* and let the card send more packets to me */
1416 netif_wake_queue(dev);
1417 }
1418 if (status & IM_RX_OVRN_INT) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001419 dev->stats.rx_errors++;
1420 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (smc->duplex)
1422 smc->rx_ovrn = 1; /* need MC_RESET outside smc_interrupt */
1423 outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
1424 }
1425 if (status & IM_EPH_INT)
1426 smc_eph_irq(dev);
1427 } while (--bogus_cnt);
1428
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001429 pr_debug(" Restoring saved registers mask %2.2x bank %4.4x"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 " pointer %4.4x.\n", mask, saved_bank, saved_pointer);
1431
1432 /* restore state register */
1433 outw((mask<<8), ioaddr + INTERRUPT);
1434 outw(saved_pointer, ioaddr + POINTER);
1435 SMC_SELECT_BANK(saved_bank);
1436
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001437 pr_debug("%s: Exiting interrupt IRQ%d.\n", dev->name, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439irq_done:
1440
1441 if ((smc->manfid == MANFID_OSITECH) &&
1442 (smc->cardid != PRODID_OSITECH_SEVEN)) {
1443 /* Retrigger interrupt if needed */
1444 mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
1445 set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
1446 }
1447 if (smc->manfid == MANFID_MOTOROLA) {
1448 u_char cor;
1449 cor = readb(smc->base + MOT_UART + CISREG_COR);
1450 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
1451 writeb(cor, smc->base + MOT_UART + CISREG_COR);
1452 cor = readb(smc->base + MOT_LAN + CISREG_COR);
1453 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
1454 writeb(cor, smc->base + MOT_LAN + CISREG_COR);
1455 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001456
1457 if ((smc->base != NULL) && /* Megahertz MFC's */
1458 (smc->manfid == MANFID_MEGAHERTZ) &&
1459 (smc->cardid == PRODID_MEGAHERTZ_EM3288)) {
1460
1461 u_char tmp;
1462 tmp = readb(smc->base+MEGAHERTZ_ISR);
1463 tmp = readb(smc->base+MEGAHERTZ_ISR);
1464
1465 /* Retrigger interrupt if needed */
1466 writeb(tmp, smc->base + MEGAHERTZ_ISR);
1467 writeb(tmp, smc->base + MEGAHERTZ_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001469
Komuro85e27832007-07-23 21:36:06 +09001470 spin_unlock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return IRQ_RETVAL(handled);
1472}
1473
1474/*====================================================================*/
1475
1476static void smc_rx(struct net_device *dev)
1477{
Olof Johansson906da802008-02-04 22:27:35 -08001478 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int rx_status;
1480 int packet_length; /* Caution: not frame length, rather words
1481 to transfer from the chip. */
1482
1483 /* Assertion: we are in Window 2. */
1484
1485 if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
Joe Perches636b8112010-08-12 12:22:51 +00001486 netdev_err(dev, "smc_rx() with nothing on Rx FIFO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return;
1488 }
1489
1490 /* Reset the read pointer, and read the status and packet length. */
1491 outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
1492 rx_status = inw(ioaddr + DATA_1);
1493 packet_length = inw(ioaddr + DATA_1) & 0x07ff;
1494
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001495 pr_debug("%s: Receive status %4.4x length %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 dev->name, rx_status, packet_length);
1497
1498 if (!(rx_status & RS_ERRORS)) {
1499 /* do stuff to make a new packet */
1500 struct sk_buff *skb;
1501
1502 /* Note: packet_length adds 5 or 6 extra bytes here! */
1503 skb = dev_alloc_skb(packet_length+2);
1504
1505 if (skb == NULL) {
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001506 pr_debug("%s: Low memory, packet dropped.\n", dev->name);
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001507 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 outw(MC_RELEASE, ioaddr + MMU_CMD);
1509 return;
1510 }
1511
1512 packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
1513 skb_reserve(skb, 2);
1514 insw(ioaddr+DATA_1, skb_put(skb, packet_length),
1515 (packet_length+1)>>1);
1516 skb->protocol = eth_type_trans(skb, dev);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 netif_rx(skb);
1519 dev->last_rx = jiffies;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001520 dev->stats.rx_packets++;
1521 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (rx_status & RS_MULTICAST)
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001523 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 } else {
1525 /* error ... */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001526 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001528 if (rx_status & RS_ALGNERR) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001530 dev->stats.rx_length_errors++;
1531 if (rx_status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 /* Let the MMU free the memory of this packet. */
1534 outw(MC_RELEASE, ioaddr + MMU_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*======================================================================
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 Set the receive mode.
1540
1541 This routine is used by both the protocol level to notify us of
1542 promiscuous/multicast mode changes, and by the open/reset code to
1543 initialize the Rx registers. We always set the multicast list and
1544 leave the receiver running.
1545
1546======================================================================*/
1547
1548static void set_rx_mode(struct net_device *dev)
1549{
Olof Johansson906da802008-02-04 22:27:35 -08001550 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct smc_private *smc = netdev_priv(dev);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001552 unsigned char multicast_table[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 unsigned long flags;
1554 u_short rx_cfg_setting;
Ken Kawasakia6d37022010-04-10 12:50:14 +00001555 int i;
1556
1557 memset(multicast_table, 0, sizeof(multicast_table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 rx_cfg_setting = RxStripCRC | RxEnable | RxPromisc | RxAllMulti;
1561 } else if (dev->flags & IFF_ALLMULTI)
1562 rx_cfg_setting = RxStripCRC | RxEnable | RxAllMulti;
1563 else {
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001564 if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001565 struct netdev_hw_addr *ha;
Jiri Pirko91fea582010-02-19 08:48:47 +00001566
Jiri Pirko22bedad2010-04-01 21:22:57 +00001567 netdev_for_each_mc_addr(ha, dev) {
1568 u_int position = ether_crc(6, ha->addr);
Jiri Pirko91fea582010-02-19 08:48:47 +00001569 multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 rx_cfg_setting = RxStripCRC | RxEnable;
1573 }
1574
1575 /* Load MC table and Rx setting into the chip without interrupts. */
1576 spin_lock_irqsave(&smc->lock, flags);
1577 SMC_SELECT_BANK(3);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001578 for (i = 0; i < 8; i++)
1579 outb(multicast_table[i], ioaddr + MULTICAST0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 SMC_SELECT_BANK(0);
1581 outw(rx_cfg_setting, ioaddr + RCR);
1582 SMC_SELECT_BANK(2);
1583 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*======================================================================
1587
1588 Senses when a card's config changes. Here, it's coax or TP.
1589
1590======================================================================*/
1591
1592static int s9k_config(struct net_device *dev, struct ifmap *map)
1593{
1594 struct smc_private *smc = netdev_priv(dev);
1595 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1596 if (smc->cfg & CFG_MII_SELECT)
1597 return -EOPNOTSUPP;
1598 else if (map->port > 2)
1599 return -EINVAL;
1600 dev->if_port = map->port;
Joe Perches636b8112010-08-12 12:22:51 +00001601 netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 smc_reset(dev);
1603 }
1604 return 0;
1605}
1606
1607/*======================================================================
1608
1609 Reset the chip, reloading every register that might be corrupted.
1610
1611======================================================================*/
1612
1613/*
1614 Set transceiver type, perhaps to something other than what the user
1615 specified in dev->if_port.
1616*/
1617static void smc_set_xcvr(struct net_device *dev, int if_port)
1618{
1619 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001620 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 u_short saved_bank;
1622
1623 saved_bank = inw(ioaddr + BANK_SELECT);
1624 SMC_SELECT_BANK(1);
1625 if (if_port == 2) {
1626 outw(smc->cfg | CFG_AUI_SELECT, ioaddr + CONFIG);
1627 if ((smc->manfid == MANFID_OSITECH) &&
1628 (smc->cardid != PRODID_OSITECH_SEVEN))
1629 set_bits(OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1630 smc->media_status = ((dev->if_port == 0) ? 0x0001 : 0x0002);
1631 } else {
1632 outw(smc->cfg, ioaddr + CONFIG);
1633 if ((smc->manfid == MANFID_OSITECH) &&
1634 (smc->cardid != PRODID_OSITECH_SEVEN))
1635 mask_bits(~OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1636 smc->media_status = ((dev->if_port == 0) ? 0x0012 : 0x4001);
1637 }
1638 SMC_SELECT_BANK(saved_bank);
1639}
1640
1641static void smc_reset(struct net_device *dev)
1642{
Olof Johansson906da802008-02-04 22:27:35 -08001643 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct smc_private *smc = netdev_priv(dev);
1645 int i;
1646
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001647 pr_debug("%s: smc91c92 reset called.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 /* The first interaction must be a write to bring the chip out
1650 of sleep mode. */
1651 SMC_SELECT_BANK(0);
1652 /* Reset the chip. */
1653 outw(RCR_SOFTRESET, ioaddr + RCR);
1654 udelay(10);
1655
1656 /* Clear the transmit and receive configuration registers. */
1657 outw(RCR_CLEAR, ioaddr + RCR);
1658 outw(TCR_CLEAR, ioaddr + TCR);
1659
1660 /* Set the Window 1 control, configuration and station addr registers.
1661 No point in writing the I/O base register ;-> */
1662 SMC_SELECT_BANK(1);
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001663 /* Automatically release successfully transmitted packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 Accept link errors, counter and Tx error interrupts. */
1665 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1666 ioaddr + CONTROL);
1667 smc_set_xcvr(dev, dev->if_port);
1668 if ((smc->manfid == MANFID_OSITECH) &&
1669 (smc->cardid != PRODID_OSITECH_SEVEN))
1670 outw((dev->if_port == 2 ? OSI_AUI_PWR : 0) |
1671 (inw(ioaddr-0x10+OSITECH_AUI_PWR) & 0xff00),
1672 ioaddr - 0x10 + OSITECH_AUI_PWR);
1673
1674 /* Fill in the physical address. The databook is wrong about the order! */
1675 for (i = 0; i < 6; i += 2)
1676 outw((dev->dev_addr[i+1]<<8)+dev->dev_addr[i],
1677 ioaddr + ADDR0 + i);
1678
1679 /* Reset the MMU */
1680 SMC_SELECT_BANK(2);
1681 outw(MC_RESET, ioaddr + MMU_CMD);
1682 outw(0, ioaddr + INTERRUPT);
1683
1684 /* Re-enable the chip. */
1685 SMC_SELECT_BANK(0);
1686 outw(((smc->cfg & CFG_MII_SELECT) ? 0 : TCR_MONCSN) |
1687 TCR_ENABLE | TCR_PAD_EN | smc->duplex, ioaddr + TCR);
1688 set_rx_mode(dev);
1689
1690 if (smc->cfg & CFG_MII_SELECT) {
1691 SMC_SELECT_BANK(3);
1692
1693 /* Reset MII */
1694 mdio_write(dev, smc->mii_if.phy_id, 0, 0x8000);
1695
1696 /* Advertise 100F, 100H, 10F, 10H */
1697 mdio_write(dev, smc->mii_if.phy_id, 4, 0x01e1);
1698
1699 /* Restart MII autonegotiation */
1700 mdio_write(dev, smc->mii_if.phy_id, 0, 0x0000);
1701 mdio_write(dev, smc->mii_if.phy_id, 0, 0x1200);
1702 }
1703
1704 /* Enable interrupts. */
1705 SMC_SELECT_BANK(2);
1706 outw((IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT) << 8,
1707 ioaddr + INTERRUPT);
1708}
1709
1710/*======================================================================
1711
1712 Media selection timer routine
1713
1714======================================================================*/
1715
1716static void media_check(u_long arg)
1717{
1718 struct net_device *dev = (struct net_device *) arg;
1719 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001720 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 u_short i, media, saved_bank;
1722 u_short link;
Komuro85e27832007-07-23 21:36:06 +09001723 unsigned long flags;
1724
1725 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 saved_bank = inw(ioaddr + BANK_SELECT);
1728
1729 if (!netif_device_present(dev))
1730 goto reschedule;
1731
1732 SMC_SELECT_BANK(2);
1733
1734 /* need MC_RESET to keep the memory consistent. errata? */
1735 if (smc->rx_ovrn) {
1736 outw(MC_RESET, ioaddr + MMU_CMD);
1737 smc->rx_ovrn = 0;
1738 }
1739 i = inw(ioaddr + INTERRUPT);
1740 SMC_SELECT_BANK(0);
1741 media = inw(ioaddr + EPH) & EPH_LINK_OK;
1742 SMC_SELECT_BANK(1);
1743 media |= (inw(ioaddr + CONFIG) & CFG_AUI_SELECT) ? 2 : 1;
1744
Ken Kawasaki2a915152010-04-24 10:37:09 +00001745 SMC_SELECT_BANK(saved_bank);
1746 spin_unlock_irqrestore(&smc->lock, flags);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* Check for pending interrupt with watchdog flag set: with
1749 this, we can limp along even if the interrupt is blocked */
1750 if (smc->watchdog++ && ((i>>8) & i)) {
1751 if (!smc->fast_poll)
Joe Perches636b8112010-08-12 12:22:51 +00001752 netdev_info(dev, "interrupt(s) dropped!\n");
Ken Kawasaki2a915152010-04-24 10:37:09 +00001753 local_irq_save(flags);
Komuroe363d132007-02-10 11:57:35 +09001754 smc_interrupt(dev->irq, dev);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001755 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 smc->fast_poll = HZ;
1757 }
1758 if (smc->fast_poll) {
1759 smc->fast_poll--;
1760 smc->media.expires = jiffies + HZ/100;
1761 add_timer(&smc->media);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return;
1763 }
1764
Ken Kawasaki2a915152010-04-24 10:37:09 +00001765 spin_lock_irqsave(&smc->lock, flags);
1766
1767 saved_bank = inw(ioaddr + BANK_SELECT);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (smc->cfg & CFG_MII_SELECT) {
1770 if (smc->mii_if.phy_id < 0)
1771 goto reschedule;
1772
1773 SMC_SELECT_BANK(3);
1774 link = mdio_read(dev, smc->mii_if.phy_id, 1);
1775 if (!link || (link == 0xffff)) {
Joe Perches636b8112010-08-12 12:22:51 +00001776 netdev_info(dev, "MII is missing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 smc->mii_if.phy_id = -1;
1778 goto reschedule;
1779 }
1780
1781 link &= 0x0004;
1782 if (link != smc->link_status) {
1783 u_short p = mdio_read(dev, smc->mii_if.phy_id, 5);
Joe Perches636b8112010-08-12 12:22:51 +00001784 netdev_info(dev, "%s link beat\n", link ? "found" : "lost");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 smc->duplex = (((p & 0x0100) || ((p & 0x1c0) == 0x40))
1786 ? TCR_FDUPLX : 0);
1787 if (link) {
Joe Perches636b8112010-08-12 12:22:51 +00001788 netdev_info(dev, "autonegotiation complete: "
1789 "%dbaseT-%cD selected\n",
1790 (p & 0x0180) ? 100 : 10, smc->duplex ? 'F' : 'H');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
1792 SMC_SELECT_BANK(0);
1793 outw(inw(ioaddr + TCR) | smc->duplex, ioaddr + TCR);
1794 smc->link_status = link;
1795 }
1796 goto reschedule;
1797 }
1798
1799 /* Ignore collisions unless we've had no rx's recently */
Marcelo Feitoza Parisi4851d3a2005-07-15 10:00:41 -07001800 if (time_after(jiffies, dev->last_rx + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (smc->tx_err || (smc->media_status & EPH_16COL))
1802 media |= EPH_16COL;
1803 }
1804 smc->tx_err = 0;
1805
1806 if (media != smc->media_status) {
1807 if ((media & smc->media_status & 1) &&
1808 ((smc->media_status ^ media) & EPH_LINK_OK))
Joe Perches636b8112010-08-12 12:22:51 +00001809 netdev_info(dev, "%s link beat\n",
1810 smc->media_status & EPH_LINK_OK ? "lost" : "found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else if ((media & smc->media_status & 2) &&
1812 ((smc->media_status ^ media) & EPH_16COL))
Joe Perches636b8112010-08-12 12:22:51 +00001813 netdev_info(dev, "coax cable %s\n",
1814 media & EPH_16COL ? "problem" : "ok");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (dev->if_port == 0) {
1816 if (media & 1) {
1817 if (media & EPH_LINK_OK)
Joe Perches636b8112010-08-12 12:22:51 +00001818 netdev_info(dev, "flipped to 10baseT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 else
1820 smc_set_xcvr(dev, 2);
1821 } else {
1822 if (media & EPH_16COL)
1823 smc_set_xcvr(dev, 1);
1824 else
Joe Perches636b8112010-08-12 12:22:51 +00001825 netdev_info(dev, "flipped to 10base2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 }
1828 smc->media_status = media;
1829 }
1830
1831reschedule:
1832 smc->media.expires = jiffies + HZ;
1833 add_timer(&smc->media);
1834 SMC_SELECT_BANK(saved_bank);
Komuro85e27832007-07-23 21:36:06 +09001835 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838static int smc_link_ok(struct net_device *dev)
1839{
Olof Johansson906da802008-02-04 22:27:35 -08001840 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct smc_private *smc = netdev_priv(dev);
1842
1843 if (smc->cfg & CFG_MII_SELECT) {
1844 return mii_link_ok(&smc->mii_if);
1845 } else {
1846 SMC_SELECT_BANK(0);
1847 return inw(ioaddr + EPH) & EPH_LINK_OK;
1848 }
1849}
1850
1851static int smc_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1852{
1853 u16 tmp;
Olof Johansson906da802008-02-04 22:27:35 -08001854 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 ecmd->supported = (SUPPORTED_TP | SUPPORTED_AUI |
1857 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full);
1858
1859 SMC_SELECT_BANK(1);
1860 tmp = inw(ioaddr + CONFIG);
1861 ecmd->port = (tmp & CFG_AUI_SELECT) ? PORT_AUI : PORT_TP;
1862 ecmd->transceiver = XCVR_INTERNAL;
David Decotigny70739492011-04-27 18:32:40 +00001863 ethtool_cmd_speed_set(ecmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 ecmd->phy_address = ioaddr + MGMT;
1865
1866 SMC_SELECT_BANK(0);
1867 tmp = inw(ioaddr + TCR);
1868 ecmd->duplex = (tmp & TCR_FDUPLX) ? DUPLEX_FULL : DUPLEX_HALF;
1869
1870 return 0;
1871}
1872
1873static int smc_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1874{
1875 u16 tmp;
Olof Johansson906da802008-02-04 22:27:35 -08001876 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
David Decotigny25db0332011-04-27 18:32:39 +00001878 if (ethtool_cmd_speed(ecmd) != SPEED_10)
1879 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1881 return -EINVAL;
1882 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI)
1883 return -EINVAL;
1884 if (ecmd->transceiver != XCVR_INTERNAL)
1885 return -EINVAL;
1886
1887 if (ecmd->port == PORT_AUI)
1888 smc_set_xcvr(dev, 1);
1889 else
1890 smc_set_xcvr(dev, 0);
1891
1892 SMC_SELECT_BANK(0);
1893 tmp = inw(ioaddr + TCR);
1894 if (ecmd->duplex == DUPLEX_FULL)
1895 tmp |= TCR_FDUPLX;
1896 else
1897 tmp &= ~TCR_FDUPLX;
1898 outw(tmp, ioaddr + TCR);
1899
1900 return 0;
1901}
1902
1903static int check_if_running(struct net_device *dev)
1904{
1905 if (!netif_running(dev))
1906 return -EINVAL;
1907 return 0;
1908}
1909
1910static void smc_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1911{
1912 strcpy(info->driver, DRV_NAME);
1913 strcpy(info->version, DRV_VERSION);
1914}
1915
1916static int smc_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1917{
1918 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001919 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1921 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001922 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Ken Kawasaki2a915152010-04-24 10:37:09 +00001924 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001925 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (smc->cfg & CFG_MII_SELECT)
1927 ret = mii_ethtool_gset(&smc->mii_if, ecmd);
1928 else
1929 ret = smc_netdev_get_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001931 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return ret;
1933}
1934
1935static int smc_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1936{
1937 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001938 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1940 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001941 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Ken Kawasaki2a915152010-04-24 10:37:09 +00001943 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001944 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (smc->cfg & CFG_MII_SELECT)
1946 ret = mii_ethtool_sset(&smc->mii_if, ecmd);
1947 else
1948 ret = smc_netdev_set_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001950 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 return ret;
1952}
1953
1954static u32 smc_get_link(struct net_device *dev)
1955{
1956 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001957 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1959 u32 ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001960 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Ken Kawasaki2a915152010-04-24 10:37:09 +00001962 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001963 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 ret = smc_link_ok(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001966 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return ret;
1968}
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970static int smc_nway_reset(struct net_device *dev)
1971{
1972 struct smc_private *smc = netdev_priv(dev);
1973 if (smc->cfg & CFG_MII_SELECT) {
Olof Johansson906da802008-02-04 22:27:35 -08001974 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1976 int res;
1977
1978 SMC_SELECT_BANK(3);
1979 res = mii_nway_restart(&smc->mii_if);
1980 SMC_SELECT_BANK(saved_bank);
1981
1982 return res;
1983 } else
1984 return -EOPNOTSUPP;
1985}
1986
Jeff Garzik7282d492006-09-13 14:30:00 -04001987static const struct ethtool_ops ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .begin = check_if_running,
1989 .get_drvinfo = smc_get_drvinfo,
1990 .get_settings = smc_get_settings,
1991 .set_settings = smc_set_settings,
1992 .get_link = smc_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 .nway_reset = smc_nway_reset,
1994};
1995
1996static int smc_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1997{
1998 struct smc_private *smc = netdev_priv(dev);
1999 struct mii_ioctl_data *mii = if_mii(rq);
2000 int rc = 0;
2001 u16 saved_bank;
Olof Johansson906da802008-02-04 22:27:35 -08002002 unsigned int ioaddr = dev->base_addr;
Ken Kawasaki2a915152010-04-24 10:37:09 +00002003 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 if (!netif_running(dev))
2006 return -EINVAL;
2007
Ken Kawasaki2a915152010-04-24 10:37:09 +00002008 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 saved_bank = inw(ioaddr + BANK_SELECT);
2010 SMC_SELECT_BANK(3);
2011 rc = generic_mii_ioctl(&smc->mii_if, mii, cmd, NULL);
2012 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00002013 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return rc;
2015}
2016
Joe Perches25f8f542011-05-03 19:29:01 -07002017static const struct pcmcia_device_id smc91c92_ids[] = {
Dominik Brodowski5c672222005-06-27 16:28:27 -07002018 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0109, 0x0501),
2019 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0140, 0x000a),
2020 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
2021 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
2022 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
2023 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
2024 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
2025 PCMCIA_PFC_DEVICE_PROD_ID12(0, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
Komurod277ad02005-07-28 01:07:24 -07002026 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
2027 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002028 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x016c, 0x0020),
2029 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0023),
2030 PCMCIA_DEVICE_PROD_ID123("BASICS by New Media Corporation", "Ethernet", "SMC91C94", 0x23c78a9d, 0x00b2e941, 0xcef397fb),
2031 PCMCIA_DEVICE_PROD_ID12("ARGOSY", "Fast Ethernet PCCard", 0x78f308dc, 0xdcea68bc),
2032 PCMCIA_DEVICE_PROD_ID12("dit Co., Ltd.", "PC Card-10/100BTX", 0xe59365c8, 0x6a2161d1),
2033 PCMCIA_DEVICE_PROD_ID12("DYNALINK", "L100C", 0x6a26d1cf, 0xc16ce9c5),
2034 PCMCIA_DEVICE_PROD_ID12("Farallon", "Farallon Enet", 0x58d93fc4, 0x244734e9),
2035 PCMCIA_DEVICE_PROD_ID12("Megahertz", "CC10BT/2", 0x33234748, 0x3c95b953),
2036 PCMCIA_DEVICE_PROD_ID12("MELCO/SMC", "LPC-TX", 0xa2cd8e6d, 0x42da662a),
Komurod277ad02005-07-28 01:07:24 -07002037 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Four of Diamonds Ethernet", 0xc2f80cd, 0xb3466314),
2038 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Seven of Diamonds Ethernet", 0xc2f80cd, 0x194b650a),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002039 PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Fast Ethernet PCCard", 0x281f1c5d, 0xdcea68bc),
2040 PCMCIA_DEVICE_PROD_ID12("Psion", "10Mb Ethernet", 0x4ef00b21, 0x844be9e9),
2041 PCMCIA_DEVICE_PROD_ID12("SMC", "EtherEZ Ethernet 8020", 0xc4f8b18b, 0x4a0eeb2d),
2042 /* These conflict with other cards! */
2043 /* PCMCIA_DEVICE_MANF_CARD(0x0186, 0x0100), */
2044 /* PCMCIA_DEVICE_MANF_CARD(0x8a01, 0xc1ab), */
2045 PCMCIA_DEVICE_NULL,
2046};
2047MODULE_DEVICE_TABLE(pcmcia, smc91c92_ids);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049static struct pcmcia_driver smc91c92_cs_driver = {
2050 .owner = THIS_MODULE,
Dominik Brodowski2e9b9812010-08-08 11:36:26 +02002051 .name = "smc91c92_cs",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002052 .probe = smc91c92_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002053 .remove = smc91c92_detach,
Dominik Brodowski5c672222005-06-27 16:28:27 -07002054 .id_table = smc91c92_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002055 .suspend = smc91c92_suspend,
2056 .resume = smc91c92_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057};
2058
2059static int __init init_smc91c92_cs(void)
2060{
2061 return pcmcia_register_driver(&smc91c92_cs_driver);
2062}
2063
2064static void __exit exit_smc91c92_cs(void)
2065{
2066 pcmcia_unregister_driver(&smc91c92_cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069module_init(init_smc91c92_cs);
2070module_exit(exit_smc91c92_cs);