blob: 04393b5fef718c5a71877e8da9c6e351cf73823b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/uaccess.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*====================================================================*/
59
Arjan van de Venf71e1302006-03-03 21:33:57 -050060static const char *if_names[] = { "auto", "10baseT", "10base2"};
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053062/* Firmware name */
63#define FIRMWARE_NAME "ositech/Xilinx7OD.bin"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* Module parameters */
66
67MODULE_DESCRIPTION("SMC 91c92 series PCMCIA ethernet driver");
68MODULE_LICENSE("GPL");
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053069MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
72
73/*
74 Transceiver/media type.
75 0 = auto
76 1 = 10baseT (and autoselect if #define AUTOSELECT),
77 2 = AUI/10base2,
78*/
79INT_MODULE_PARM(if_port, 0);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define DRV_NAME "smc91c92_cs"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040083#define DRV_VERSION "1.123"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*====================================================================*/
86
87/* Operational parameter that usually are not changed. */
88
89/* Time in jiffies before concluding Tx hung */
90#define TX_TIMEOUT ((400*HZ)/1000)
91
92/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
93#define INTR_WORK 4
94
95/* Times to check the check the chip before concluding that it doesn't
96 currently have room for another Tx packet. */
97#define MEMORY_WAIT_TIME 8
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099struct smc_private {
Dominik Brodowskifd238232006-03-05 10:45:09 +0100100 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 spinlock_t lock;
102 u_short manfid;
103 u_short cardid;
Stephen Hemminger6fb72982009-03-20 19:36:09 +0000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct sk_buff *saved_skb;
106 int packets_waiting;
107 void __iomem *base;
108 u_short cfg;
109 struct timer_list media;
110 int watchdog, tx_err;
111 u_short media_status;
112 u_short fast_poll;
113 u_short link_status;
114 struct mii_if_info mii_if;
115 int duplex;
116 int rx_ovrn;
117};
118
119/* Special definitions for Megahertz multifunction cards */
120#define MEGAHERTZ_ISR 0x0380
121
122/* Special function registers for Motorola Mariner */
123#define MOT_LAN 0x0000
124#define MOT_UART 0x0020
125#define MOT_EEPROM 0x20
126
127#define MOT_NORMAL \
128(COR_LEVEL_REQ | COR_FUNC_ENA | COR_ADDR_DECODE | COR_IREQ_ENA)
129
130/* Special function registers for Ositech cards */
131#define OSITECH_AUI_CTL 0x0c
132#define OSITECH_PWRDOWN 0x0d
133#define OSITECH_RESET 0x0e
134#define OSITECH_ISR 0x0f
135#define OSITECH_AUI_PWR 0x0c
136#define OSITECH_RESET_ISR 0x0e
137
138#define OSI_AUI_PWR 0x40
139#define OSI_LAN_PWRDOWN 0x02
140#define OSI_MODEM_PWRDOWN 0x01
141#define OSI_LAN_RESET 0x02
142#define OSI_MODEM_RESET 0x01
143
144/* Symbolic constants for the SMC91c9* series chips, from Erik Stahlman. */
145#define BANK_SELECT 14 /* Window select register. */
146#define SMC_SELECT_BANK(x) { outw(x, ioaddr + BANK_SELECT); }
147
148/* Bank 0 registers. */
149#define TCR 0 /* transmit control register */
150#define TCR_CLEAR 0 /* do NOTHING */
151#define TCR_ENABLE 0x0001 /* if this is 1, we can transmit */
152#define TCR_PAD_EN 0x0080 /* pads short packets to 64 bytes */
153#define TCR_MONCSN 0x0400 /* Monitor Carrier. */
154#define TCR_FDUPLX 0x0800 /* Full duplex mode. */
155#define TCR_NORMAL TCR_ENABLE | TCR_PAD_EN
156
157#define EPH 2 /* Ethernet Protocol Handler report. */
158#define EPH_TX_SUC 0x0001
159#define EPH_SNGLCOL 0x0002
160#define EPH_MULCOL 0x0004
161#define EPH_LTX_MULT 0x0008
162#define EPH_16COL 0x0010
163#define EPH_SQET 0x0020
164#define EPH_LTX_BRD 0x0040
165#define EPH_TX_DEFR 0x0080
166#define EPH_LAT_COL 0x0200
167#define EPH_LOST_CAR 0x0400
168#define EPH_EXC_DEF 0x0800
169#define EPH_CTR_ROL 0x1000
170#define EPH_RX_OVRN 0x2000
171#define EPH_LINK_OK 0x4000
172#define EPH_TX_UNRN 0x8000
173#define MEMINFO 8 /* Memory Information Register */
174#define MEMCFG 10 /* Memory Configuration Register */
175
176/* Bank 1 registers. */
177#define CONFIG 0
178#define CFG_MII_SELECT 0x8000 /* 91C100 only */
179#define CFG_NO_WAIT 0x1000
180#define CFG_FULL_STEP 0x0400
181#define CFG_SET_SQLCH 0x0200
182#define CFG_AUI_SELECT 0x0100
183#define CFG_16BIT 0x0080
184#define CFG_DIS_LINK 0x0040
185#define CFG_STATIC 0x0030
186#define CFG_IRQ_SEL_1 0x0004
187#define CFG_IRQ_SEL_0 0x0002
188#define BASE_ADDR 2
189#define ADDR0 4
190#define GENERAL 10
191#define CONTROL 12
192#define CTL_STORE 0x0001
193#define CTL_RELOAD 0x0002
194#define CTL_EE_SELECT 0x0004
195#define CTL_TE_ENABLE 0x0020
196#define CTL_CR_ENABLE 0x0040
197#define CTL_LE_ENABLE 0x0080
198#define CTL_AUTO_RELEASE 0x0800
199#define CTL_POWERDOWN 0x2000
200
201/* Bank 2 registers. */
202#define MMU_CMD 0
203#define MC_ALLOC 0x20 /* or with number of 256 byte packets */
204#define MC_RESET 0x40
205#define MC_RELEASE 0x80 /* remove and release the current rx packet */
206#define MC_FREEPKT 0xA0 /* Release packet in PNR register */
207#define MC_ENQUEUE 0xC0 /* Enqueue the packet for transmit */
208#define PNR_ARR 2
209#define FIFO_PORTS 4
210#define FP_RXEMPTY 0x8000
211#define POINTER 6
212#define PTR_AUTO_INC 0x0040
213#define PTR_READ 0x2000
214#define PTR_AUTOINC 0x4000
215#define PTR_RCV 0x8000
216#define DATA_1 8
217#define INTERRUPT 12
218#define IM_RCV_INT 0x1
219#define IM_TX_INT 0x2
220#define IM_TX_EMPTY_INT 0x4
221#define IM_ALLOC_INT 0x8
222#define IM_RX_OVRN_INT 0x10
223#define IM_EPH_INT 0x20
224
225#define RCR 4
226enum RxCfg { RxAllMulti = 0x0004, RxPromisc = 0x0002,
227 RxEnable = 0x0100, RxStripCRC = 0x0200};
228#define RCR_SOFTRESET 0x8000 /* resets the chip */
229#define RCR_STRIP_CRC 0x200 /* strips CRC */
230#define RCR_ENABLE 0x100 /* IFF this is set, we can receive packets */
231#define RCR_ALMUL 0x4 /* receive all multicast packets */
232#define RCR_PROMISC 0x2 /* enable promiscuous mode */
233
234/* the normal settings for the RCR register : */
235#define RCR_NORMAL (RCR_STRIP_CRC | RCR_ENABLE)
236#define RCR_CLEAR 0x0 /* set it to a base state */
237#define COUNTER 6
238
239/* BANK 3 -- not the same values as in smc9194! */
240#define MULTICAST0 0
241#define MULTICAST2 2
242#define MULTICAST4 4
243#define MULTICAST6 6
244#define MGMT 8
245#define REVISION 0x0a
246
247/* Transmit status bits. */
248#define TS_SUCCESS 0x0001
249#define TS_16COL 0x0010
250#define TS_LATCOL 0x0200
251#define TS_LOSTCAR 0x0400
252
253/* Receive status bits. */
254#define RS_ALGNERR 0x8000
255#define RS_BADCRC 0x2000
256#define RS_ODDFRAME 0x1000
257#define RS_TOOLONG 0x0800
258#define RS_TOOSHORT 0x0400
259#define RS_MULTICAST 0x0001
260#define RS_ERRORS (RS_ALGNERR | RS_BADCRC | RS_TOOLONG | RS_TOOSHORT)
261
262#define set_bits(v, p) outw(inw(p)|(v), (p))
263#define mask_bits(v, p) outw(inw(p)&(v), (p))
264
265/*====================================================================*/
266
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100267static void smc91c92_detach(struct pcmcia_device *p_dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200268static int smc91c92_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200269static void smc91c92_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271static int smc_open(struct net_device *dev);
272static int smc_close(struct net_device *dev);
273static int smc_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
274static void smc_tx_timeout(struct net_device *dev);
Stephen Hemmingerdbf02fa2009-08-31 19:50:49 +0000275static netdev_tx_t smc_start_xmit(struct sk_buff *skb,
276 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100277static irqreturn_t smc_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void smc_rx(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void set_rx_mode(struct net_device *dev);
280static int s9k_config(struct net_device *dev, struct ifmap *map);
281static void smc_set_xcvr(struct net_device *dev, int if_port);
282static void smc_reset(struct net_device *dev);
283static void media_check(u_long arg);
Olof Johansson906da802008-02-04 22:27:35 -0800284static void mdio_sync(unsigned int addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int mdio_read(struct net_device *dev, int phy_id, int loc);
286static void mdio_write(struct net_device *dev, int phy_id, int loc, int value);
287static int smc_link_ok(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400288static const struct ethtool_ops ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000290static const struct net_device_ops smc_netdev_ops = {
291 .ndo_open = smc_open,
292 .ndo_stop = smc_close,
293 .ndo_start_xmit = smc_start_xmit,
294 .ndo_tx_timeout = smc_tx_timeout,
295 .ndo_set_config = s9k_config,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000296 .ndo_set_rx_mode = set_rx_mode,
Joe Perchesc061b182010-08-23 18:20:03 +0000297 .ndo_do_ioctl = smc_ioctl,
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000298 .ndo_change_mtu = eth_change_mtu,
299 .ndo_set_mac_address = eth_mac_addr,
300 .ndo_validate_addr = eth_validate_addr,
301};
302
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200303static int smc91c92_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct smc_private *smc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200308 dev_dbg(&link->dev, "smc91c92_attach()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Create new ethernet device */
311 dev = alloc_etherdev(sizeof(struct smc_private));
312 if (!dev)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100313 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200315 smc->p_dev = link;
Dominik Brodowski44b496f2010-06-11 04:44:55 +0000316 link->priv = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 spin_lock_init(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 /* The SMC91c92-specific entries in the device structure. */
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000321 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 SET_ETHTOOL_OPS(dev, &ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 smc->mii_if.dev = dev;
326 smc->mii_if.mdio_read = mdio_read;
327 smc->mii_if.mdio_write = mdio_write;
328 smc->mii_if.phy_id_mask = 0x1f;
329 smc->mii_if.reg_num_mask = 0x1f;
330
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200331 return smc91c92_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332} /* smc91c92_attach */
333
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200334static void smc91c92_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200338 dev_dbg(&link->dev, "smc91c92_detach\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Dominik Brodowskic7c2fa02010-03-20 19:39:26 +0100340 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100342 smc91c92_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 free_netdev(dev);
345} /* smc91c92_detach */
346
347/*====================================================================*/
348
349static int cvt_ascii_address(struct net_device *dev, char *s)
350{
351 int i, j, da, c;
352
353 if (strlen(s) != 12)
354 return -1;
355 for (i = 0; i < 6; i++) {
356 da = 0;
357 for (j = 0; j < 2; j++) {
358 c = *s++;
359 da <<= 4;
360 da += ((c >= '0') && (c <= '9')) ?
361 (c - '0') : ((c & 0x0f) + 9);
362 }
363 dev->dev_addr[i] = da;
364 }
365 return 0;
366}
367
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200368/*====================================================================
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 Configuration stuff for Megahertz cards
371
372 mhz_3288_power() is used to power up a 3288's ethernet chip.
373 mhz_mfc_config() handles socket setup for multifunction (1144
374 and 3288) cards. mhz_setup() gets a card's hardware ethernet
375 address.
376
377======================================================================*/
378
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200379static int mhz_3288_power(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct net_device *dev = link->priv;
382 struct smc_private *smc = netdev_priv(dev);
383 u_char tmp;
384
385 /* Read the ISR twice... */
386 readb(smc->base+MEGAHERTZ_ISR);
387 udelay(5);
388 readb(smc->base+MEGAHERTZ_ISR);
389
390 /* Pause 200ms... */
391 mdelay(200);
392
393 /* Now read and write the COR... */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200394 tmp = readb(smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 udelay(5);
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200396 writeb(tmp, smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 return 0;
399}
400
Dominik Brodowski00990e72010-07-30 13:13:46 +0200401static int mhz_mfc_config_check(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200402{
403 int k;
Dominik Brodowski00990e72010-07-30 13:13:46 +0200404 p_dev->io_lines = 16;
405 p_dev->resource[1]->start = p_dev->resource[0]->start;
406 p_dev->resource[1]->end = 8;
407 p_dev->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
408 p_dev->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
409 p_dev->resource[0]->end = 16;
410 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
411 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200412 for (k = 0; k < 0x400; k += 0x10) {
413 if (k & 0x80)
414 continue;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200415 p_dev->resource[0]->start = k ^ 0x300;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200416 if (!pcmcia_request_io(p_dev))
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200417 return 0;
418 }
419 return -ENODEV;
420}
421
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200422static int mhz_mfc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct net_device *dev = link->priv;
425 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200426 unsigned int offset;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200427 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dominik Brodowski00990e72010-07-30 13:13:46 +0200429 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ |
430 CONF_AUTO_SET_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* The Megahertz combo cards have modem-like CIS entries, so
433 we have to explicitly try a bunch of port combinations. */
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200434 if (pcmcia_loop_config(link, mhz_mfc_config_check, NULL))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200435 return -ENODEV;
436
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200437 dev->base_addr = link->resource[0]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Allocate a memory window, for accessing the ISR */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200440 link->resource[2]->flags = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
441 link->resource[2]->start = link->resource[2]->end = 0;
442 i = pcmcia_request_window(link, link->resource[2], 0);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200443 if (i != 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200444 return -ENODEV;
445
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200446 smc->base = ioremap(link->resource[2]->start,
447 resource_size(link->resource[2]));
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200448 offset = (smc->manfid == MANFID_MOTOROLA) ? link->config_base : 0;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200449 i = pcmcia_map_mem_page(link, link->resource[2], offset);
Joe Perches8e95a202009-12-03 07:58:21 +0000450 if ((i == 0) &&
451 (smc->manfid == MANFID_MEGAHERTZ) &&
452 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
453 mhz_3288_power(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200458static int pcmcia_get_versmac(struct pcmcia_device *p_dev,
459 tuple_t *tuple,
460 void *priv)
461{
462 struct net_device *dev = priv;
463 cisparse_t parse;
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700464 u8 *buf;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200465
466 if (pcmcia_parse_tuple(tuple, &parse))
467 return -EINVAL;
468
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700469 buf = parse.version_1.str + parse.version_1.ofs[3];
470
471 if ((parse.version_1.ns > 3) && (cvt_ascii_address(dev, buf) == 0))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200472 return 0;
473
474 return -EINVAL;
475};
476
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200477static int mhz_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200480 size_t len;
481 u8 *buf;
Yum Rayan4638aef42005-05-05 15:14:10 -0700482 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* Read the station address from the CIS. It is stored as the last
485 (fourth) string in the Version 1 Version/ID tuple. */
Dominik Brodowski7d2e8d02009-10-18 18:22:32 +0200486 if ((link->prod_id[3]) &&
487 (cvt_ascii_address(dev, link->prod_id[3]) == 0))
488 return 0;
489
490 /* Workarounds for broken cards start here. */
Chuck Ebberta1a98b72008-02-13 19:47:11 -0500491 /* Ugh -- the EM1144 card has two VERS_1 tuples!?! */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200492 if (!pcmcia_loop_tuple(link, CISTPL_VERS_1, pcmcia_get_versmac, dev))
493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* Another possibility: for the EM3288, in a special tuple */
Yum Rayan4638aef42005-05-05 15:14:10 -0700496 rc = -1;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200497 len = pcmcia_get_tuple(link, 0x81, &buf);
498 if (buf && len >= 13) {
499 buf[12] = '\0';
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700500 if (cvt_ascii_address(dev, buf) == 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200501 rc = 0;
502 }
503 kfree(buf);
504
505 return rc;
506};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508/*======================================================================
509
510 Configuration stuff for the Motorola Mariner
511
512 mot_config() writes directly to the Mariner configuration
513 registers because the CIS is just bogus.
514
515======================================================================*/
516
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200517static void mot_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct net_device *dev = link->priv;
520 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -0800521 unsigned int ioaddr = dev->base_addr;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200522 unsigned int iouart = link->resource[1]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 /* Set UART base address and force map with COR bit 1 */
525 writeb(iouart & 0xff, smc->base + MOT_UART + CISREG_IOBASE_0);
526 writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
527 writeb(MOT_NORMAL, smc->base + MOT_UART + CISREG_COR);
528
529 /* Set SMC base address and force map with COR bit 1 */
530 writeb(ioaddr & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_0);
531 writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
532 writeb(MOT_NORMAL, smc->base + MOT_LAN + CISREG_COR);
533
534 /* Wait for things to settle down */
535 mdelay(100);
536}
537
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200538static int mot_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800541 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int i, wait, loop;
543 u_int addr;
544
545 /* Read Ethernet address from Serial EEPROM */
546
547 for (i = 0; i < 3; i++) {
548 SMC_SELECT_BANK(2);
549 outw(MOT_EEPROM + i, ioaddr + POINTER);
550 SMC_SELECT_BANK(1);
551 outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
552
553 for (loop = wait = 0; loop < 200; loop++) {
554 udelay(10);
555 wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
556 if (wait == 0) break;
557 }
558
559 if (wait)
560 return -1;
561
562 addr = inw(ioaddr + GENERAL);
563 dev->dev_addr[2*i] = addr & 0xff;
564 dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
565 }
566
567 return 0;
568}
569
570/*====================================================================*/
571
Dominik Brodowski00990e72010-07-30 13:13:46 +0200572static int smc_configcheck(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200573{
Dominik Brodowski00990e72010-07-30 13:13:46 +0200574 p_dev->resource[0]->end = 16;
575 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
576 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
577
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200578 return pcmcia_request_io(p_dev);
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200579}
580
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200581static int smc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int i;
585
Dominik Brodowski00990e72010-07-30 13:13:46 +0200586 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
587
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200588 i = pcmcia_loop_config(link, smc_configcheck, NULL);
589 if (!i)
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200590 dev->base_addr = link->resource[0]->start;
Yum Rayan4638aef42005-05-05 15:14:10 -0700591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return i;
593}
594
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200595
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200596static int smc_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Check for a LAN function extension tuple */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200601 if (!pcmcia_get_mac_from_cis(link, dev))
602 return 0;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Try the third string in the Version 1 Version/ID tuple. */
Dominik Brodowskia9606fd2006-06-04 18:06:13 +0200605 if (link->prod_id[2]) {
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200606 if (cvt_ascii_address(dev, link->prod_id[2]) == 0)
607 return 0;
Yum Rayan4638aef42005-05-05 15:14:10 -0700608 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200609 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*====================================================================*/
613
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200614static int osi_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800617 static const unsigned int com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int i, j;
619
Dominik Brodowski00990e72010-07-30 13:13:46 +0200620 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200621 link->resource[0]->end = 64;
622 link->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
623 link->resource[1]->end = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* Enable Hard Decode, LAN, Modem */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200626 link->io_lines = 16;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200627 link->config_index = 0x23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 for (i = j = 0; j < 4; j++) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200630 link->resource[1]->start = com[j];
631 i = pcmcia_request_io(link);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200632 if (i == 0)
633 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200635 if (i != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /* Fallback: turn off hard decode */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200637 link->config_index = 0x03;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200638 link->resource[1]->end = 0;
639 i = pcmcia_request_io(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200641 dev->base_addr = link->resource[0]->start + 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return i;
643}
644
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530645static int osi_load_firmware(struct pcmcia_device *link)
646{
647 const struct firmware *fw;
648 int i, err;
649
650 err = request_firmware(&fw, FIRMWARE_NAME, &link->dev);
651 if (err) {
652 pr_err("Failed to load firmware \"%s\"\n", FIRMWARE_NAME);
653 return err;
654 }
655
656 /* Download the Seven of Diamonds firmware */
657 for (i = 0; i < fw->size; i++) {
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200658 outb(fw->data[i], link->resource[0]->start + 2);
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530659 udelay(50);
660 }
661 release_firmware(fw);
662 return err;
663}
664
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200665static int pcmcia_osi_mac(struct pcmcia_device *p_dev,
666 tuple_t *tuple,
667 void *priv)
668{
669 struct net_device *dev = priv;
670 int i;
671
672 if (tuple->TupleDataLen < 8)
673 return -EINVAL;
674 if (tuple->TupleData[0] != 0x04)
675 return -EINVAL;
676 for (i = 0; i < 6; i++)
677 dev->dev_addr[i] = tuple->TupleData[i+2];
678 return 0;
679};
680
681
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200682static int osi_setup(struct pcmcia_device *link, u_short manfid, u_short cardid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200685 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Read the station address from tuple 0x90, subtuple 0x04 */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200688 if (pcmcia_loop_tuple(link, 0x90, pcmcia_osi_mac, dev))
689 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (((manfid == MANFID_OSITECH) &&
692 (cardid == PRODID_OSITECH_SEVEN)) ||
693 ((manfid == MANFID_PSION) &&
694 (cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530695 rc = osi_load_firmware(link);
696 if (rc)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200697 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 } else if (manfid == MANFID_OSITECH) {
699 /* Make sure both functions are powered up */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200700 set_bits(0x300, link->resource[0]->start + OSITECH_AUI_PWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /* Now, turn on the interrupt for both card functions */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200702 set_bits(0x300, link->resource[0]->start + OSITECH_RESET_ISR);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200703 dev_dbg(&link->dev, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200704 inw(link->resource[0]->start + OSITECH_AUI_PWR),
705 inw(link->resource[0]->start + OSITECH_RESET_ISR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200710static int smc91c92_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100711{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100712 struct net_device *dev = link->priv;
713
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100714 if (link->open)
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100715 netif_device_detach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100716
717 return 0;
718}
719
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200720static int smc91c92_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100721{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100722 struct net_device *dev = link->priv;
723 struct smc_private *smc = netdev_priv(dev);
724 int i;
725
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100726 if ((smc->manfid == MANFID_MEGAHERTZ) &&
727 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
728 mhz_3288_power(link);
729 if (smc->manfid == MANFID_MOTOROLA)
730 mot_config(link);
731 if ((smc->manfid == MANFID_OSITECH) &&
732 (smc->cardid != PRODID_OSITECH_SEVEN)) {
733 /* Power up the card and enable interrupts */
734 set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
735 set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
736 }
737 if (((smc->manfid == MANFID_OSITECH) &&
738 (smc->cardid == PRODID_OSITECH_SEVEN)) ||
739 ((smc->manfid == MANFID_PSION) &&
740 (smc->cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530741 i = osi_load_firmware(link);
742 if (i) {
743 pr_err("smc91c92_cs: Failed to load firmware\n");
744 return i;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100745 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100746 }
747 if (link->open) {
748 smc_reset(dev);
749 netif_device_attach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100750 }
751
752 return 0;
753}
754
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*======================================================================
757
758 This verifies that the chip is some SMC91cXX variant, and returns
759 the revision code if successful. Otherwise, it returns -ENODEV.
760
761======================================================================*/
762
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200763static int check_sig(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct net_device *dev = link->priv;
Olof Johansson906da802008-02-04 22:27:35 -0800766 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int width;
768 u_short s;
769
770 SMC_SELECT_BANK(1);
771 if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
772 /* Try powering up the chip */
773 outw(0, ioaddr + CONTROL);
774 mdelay(55);
775 }
776
777 /* Try setting bus width */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200778 width = (link->resource[0]->flags == IO_DATA_PATH_WIDTH_AUTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 s = inb(ioaddr + CONFIG);
780 if (width)
781 s |= CFG_16BIT;
782 else
783 s &= ~CFG_16BIT;
784 outb(s, ioaddr + CONFIG);
785
786 /* Check Base Address Register to make sure bus width is OK */
787 s = inw(ioaddr + BASE_ADDR);
788 if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
789 ((s >> 8) != (s & 0xff))) {
790 SMC_SELECT_BANK(3);
791 s = inw(ioaddr + REVISION);
Eric Dumazet807540b2010-09-23 05:40:09 +0000792 return s & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
795 if (width) {
Joe Perches636b8112010-08-12 12:22:51 +0000796 pr_info("using 8-bit IO window\n");
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100797
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200798 smc91c92_suspend(link);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200799 pcmcia_fixup_iowidth(link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200800 smc91c92_resume(link);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100801 return check_sig(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 return -ENODEV;
804}
805
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200806static int smc91c92_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct net_device *dev = link->priv;
809 struct smc_private *smc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 char *name;
Dominik Brodowski74411c02011-07-29 18:27:34 +0200811 int i, rev, j = 0;
Olof Johansson906da802008-02-04 22:27:35 -0800812 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 u_long mir;
814
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200815 dev_dbg(&link->dev, "smc91c92_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dominik Brodowskiefd50582006-10-25 21:28:53 -0400817 smc->manfid = link->manf_id;
818 smc->cardid = link->card_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if ((smc->manfid == MANFID_OSITECH) &&
821 (smc->cardid != PRODID_OSITECH_SEVEN)) {
822 i = osi_config(link);
823 } else if ((smc->manfid == MANFID_MOTOROLA) ||
824 ((smc->manfid == MANFID_MEGAHERTZ) &&
825 ((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
826 (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
827 i = mhz_mfc_config(link);
828 } else {
829 i = smc_config(link);
830 }
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200831 if (i)
832 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Dominik Brodowskieb141202010-03-07 12:21:16 +0100834 i = pcmcia_request_irq(link, smc_interrupt);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200835 if (i)
836 goto config_failed;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200837 i = pcmcia_enable_device(link);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200838 if (i)
839 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (smc->manfid == MANFID_MOTOROLA)
842 mot_config(link);
843
Dominik Brodowskieb141202010-03-07 12:21:16 +0100844 dev->irq = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if ((if_port >= 0) && (if_port <= 2))
847 dev->if_port = if_port;
848 else
Joe Perches636b8112010-08-12 12:22:51 +0000849 dev_notice(&link->dev, "invalid if_port requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 switch (smc->manfid) {
852 case MANFID_OSITECH:
853 case MANFID_PSION:
854 i = osi_setup(link, smc->manfid, smc->cardid); break;
855 case MANFID_SMC:
856 case MANFID_NEW_MEDIA:
857 i = smc_setup(link); break;
858 case 0x128: /* For broken Megahertz cards */
859 case MANFID_MEGAHERTZ:
860 i = mhz_setup(link); break;
861 case MANFID_MOTOROLA:
862 default: /* get the hw address from EEPROM */
863 i = mot_setup(link); break;
864 }
865
866 if (i != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000867 dev_notice(&link->dev, "Unable to find hardware address.\n");
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700868 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870
871 smc->duplex = 0;
872 smc->rx_ovrn = 0;
873
874 rev = check_sig(link);
875 name = "???";
876 if (rev > 0)
877 switch (rev >> 4) {
878 case 3: name = "92"; break;
879 case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
880 case 5: name = "95"; break;
881 case 7: name = "100"; break;
882 case 8: name = "100-FD"; break;
883 case 9: name = "110"; break;
884 }
885
886 ioaddr = dev->base_addr;
887 if (rev > 0) {
888 u_long mcr;
889 SMC_SELECT_BANK(0);
890 mir = inw(ioaddr + MEMINFO) & 0xff;
891 if (mir == 0xff) mir++;
892 /* Get scale factor for memory size */
893 mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
894 mir *= 128 * (1<<((mcr >> 9) & 7));
895 SMC_SELECT_BANK(1);
896 smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
897 smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
898 if (smc->manfid == MANFID_OSITECH)
899 smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
900 if ((rev >> 4) >= 7)
901 smc->cfg |= CFG_MII_SELECT;
902 } else
903 mir = 0;
904
905 if (smc->cfg & CFG_MII_SELECT) {
906 SMC_SELECT_BANK(3);
907
908 for (i = 0; i < 32; i++) {
909 j = mdio_read(dev, i, 1);
910 if ((j != 0) && (j != 0xffff)) break;
911 }
912 smc->mii_if.phy_id = (i < 32) ? i : -1;
913
914 SMC_SELECT_BANK(0);
915 }
916
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100917 SET_NETDEV_DEV(dev, &link->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (register_netdev(dev) != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000920 dev_err(&link->dev, "register_netdev() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto config_undo;
922 }
923
Joe Perches636b8112010-08-12 12:22:51 +0000924 netdev_info(dev, "smc91c%s rev %d: io %#3lx, irq %d, hw_addr %pM\n",
925 name, (rev & 0x0f), dev->base_addr, dev->irq, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (rev > 0) {
928 if (mir & 0x3ff)
Joe Perches636b8112010-08-12 12:22:51 +0000929 netdev_info(dev, " %lu byte", mir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 else
Joe Perches636b8112010-08-12 12:22:51 +0000931 netdev_info(dev, " %lu kb", mir>>10);
932 pr_cont(" buffer, %s xcvr\n",
933 (smc->cfg & CFG_MII_SELECT) ? "MII" : if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935
936 if (smc->cfg & CFG_MII_SELECT) {
937 if (smc->mii_if.phy_id != -1) {
Joe Perches636b8112010-08-12 12:22:51 +0000938 netdev_dbg(dev, " MII transceiver at index %d, status %x\n",
939 smc->mii_if.phy_id, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } else {
Joe Perches636b8112010-08-12 12:22:51 +0000941 netdev_notice(dev, " No MII transceivers found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946config_undo:
947 unregister_netdev(dev);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200948config_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 smc91c92_release(link);
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700950 free_netdev(dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200951 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952} /* smc91c92_config */
953
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200954static void smc91c92_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200956 dev_dbg(&link->dev, "smc91c92_release\n");
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200957 if (link->resource[2]->end) {
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100958 struct net_device *dev = link->priv;
959 struct smc_private *smc = netdev_priv(dev);
960 iounmap(smc->base);
961 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200962 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*======================================================================
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 MII interface support for SMC91cXX based cards
968======================================================================*/
969
970#define MDIO_SHIFT_CLK 0x04
971#define MDIO_DATA_OUT 0x01
972#define MDIO_DIR_WRITE 0x08
973#define MDIO_DATA_WRITE0 (MDIO_DIR_WRITE)
974#define MDIO_DATA_WRITE1 (MDIO_DIR_WRITE | MDIO_DATA_OUT)
975#define MDIO_DATA_READ 0x02
976
Olof Johansson906da802008-02-04 22:27:35 -0800977static void mdio_sync(unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 int bits;
980 for (bits = 0; bits < 32; bits++) {
981 outb(MDIO_DATA_WRITE1, addr);
982 outb(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
983 }
984}
985
986static int mdio_read(struct net_device *dev, int phy_id, int loc)
987{
Olof Johansson906da802008-02-04 22:27:35 -0800988 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
990 int i, retval = 0;
991
992 mdio_sync(addr);
993 for (i = 13; i >= 0; i--) {
994 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
995 outb(dat, addr);
996 outb(dat | MDIO_SHIFT_CLK, addr);
997 }
998 for (i = 19; i > 0; i--) {
999 outb(0, addr);
1000 retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
1001 outb(MDIO_SHIFT_CLK, addr);
1002 }
1003 return (retval>>1) & 0xffff;
1004}
1005
1006static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
1007{
Olof Johansson906da802008-02-04 22:27:35 -08001008 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
1010 int i;
1011
1012 mdio_sync(addr);
1013 for (i = 31; i >= 0; i--) {
1014 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
1015 outb(dat, addr);
1016 outb(dat | MDIO_SHIFT_CLK, addr);
1017 }
1018 for (i = 1; i >= 0; i--) {
1019 outb(0, addr);
1020 outb(MDIO_SHIFT_CLK, addr);
1021 }
1022}
1023
1024/*======================================================================
1025
1026 The driver core code, most of which should be common with a
1027 non-PCMCIA implementation.
1028
1029======================================================================*/
1030
1031#ifdef PCMCIA_DEBUG
1032static void smc_dump(struct net_device *dev)
1033{
Olof Johansson906da802008-02-04 22:27:35 -08001034 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 u_short i, w, save;
1036 save = inw(ioaddr + BANK_SELECT);
1037 for (w = 0; w < 4; w++) {
1038 SMC_SELECT_BANK(w);
Joe Perches636b8112010-08-12 12:22:51 +00001039 netdev_printk(KERN_DEBUG, dev, "bank %d: ", w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 for (i = 0; i < 14; i += 2)
Joe Perches636b8112010-08-12 12:22:51 +00001041 pr_cont(" %04x", inw(ioaddr + i));
1042 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044 outw(save, ioaddr + BANK_SELECT);
1045}
1046#endif
1047
1048static int smc_open(struct net_device *dev)
1049{
1050 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001051 struct pcmcia_device *link = smc->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001053 dev_dbg(&link->dev, "%s: smc_open(%p), ID/Window %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dev->name, dev, inw(dev->base_addr + BANK_SELECT));
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001055#ifdef PCMCIA_DEBUG
1056 smc_dump(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#endif
1058
1059 /* Check that the PCMCIA card is still here. */
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001060 if (!pcmcia_dev_present(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -ENODEV;
1062 /* Physical device present signature. */
1063 if (check_sig(link) < 0) {
Joe Perches636b8112010-08-12 12:22:51 +00001064 netdev_info(dev, "Yikes! Bad chip signature!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return -ENODEV;
1066 }
1067 link->open++;
1068
1069 netif_start_queue(dev);
1070 smc->saved_skb = NULL;
1071 smc->packets_waiting = 0;
1072
1073 smc_reset(dev);
1074 init_timer(&smc->media);
Joe Perchesc061b182010-08-23 18:20:03 +00001075 smc->media.function = media_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 smc->media.data = (u_long) dev;
1077 smc->media.expires = jiffies + HZ;
1078 add_timer(&smc->media);
1079
1080 return 0;
1081} /* smc_open */
1082
1083/*====================================================================*/
1084
1085static int smc_close(struct net_device *dev)
1086{
1087 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001088 struct pcmcia_device *link = smc->p_dev;
Olof Johansson906da802008-02-04 22:27:35 -08001089 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001091 dev_dbg(&link->dev, "%s: smc_close(), status %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 dev->name, inw(ioaddr + BANK_SELECT));
1093
1094 netif_stop_queue(dev);
1095
1096 /* Shut off all interrupts, and turn off the Tx and Rx sections.
1097 Don't bother to check for chip present. */
1098 SMC_SELECT_BANK(2); /* Nominally paranoia, but do no assume... */
1099 outw(0, ioaddr + INTERRUPT);
1100 SMC_SELECT_BANK(0);
1101 mask_bits(0xff00, ioaddr + RCR);
1102 mask_bits(0xff00, ioaddr + TCR);
1103
1104 /* Put the chip into power-down mode. */
1105 SMC_SELECT_BANK(1);
1106 outw(CTL_POWERDOWN, ioaddr + CONTROL );
1107
1108 link->open--;
1109 del_timer_sync(&smc->media);
1110
1111 return 0;
1112} /* smc_close */
1113
1114/*======================================================================
1115
1116 Transfer a packet to the hardware and trigger the packet send.
1117 This may be called at either from either the Tx queue code
1118 or the interrupt handler.
1119
1120======================================================================*/
1121
1122static void smc_hardware_send_packet(struct net_device * dev)
1123{
1124 struct smc_private *smc = netdev_priv(dev);
1125 struct sk_buff *skb = smc->saved_skb;
Olof Johansson906da802008-02-04 22:27:35 -08001126 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 u_char packet_no;
1128
1129 if (!skb) {
Joe Perches636b8112010-08-12 12:22:51 +00001130 netdev_err(dev, "In XMIT with no packet to send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return;
1132 }
1133
1134 /* There should be a packet slot waiting. */
1135 packet_no = inw(ioaddr + PNR_ARR) >> 8;
1136 if (packet_no & 0x80) {
1137 /* If not, there is a hardware problem! Likely an ejected card. */
Joe Perches636b8112010-08-12 12:22:51 +00001138 netdev_warn(dev, "hardware Tx buffer allocation failed, status %#2.2x\n",
1139 packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 dev_kfree_skb_irq(skb);
1141 smc->saved_skb = NULL;
1142 netif_start_queue(dev);
1143 return;
1144 }
1145
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001146 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* The card should use the just-allocated buffer. */
1148 outw(packet_no, ioaddr + PNR_ARR);
1149 /* point to the beginning of the packet */
1150 outw(PTR_AUTOINC , ioaddr + POINTER);
1151
1152 /* Send the packet length (+6 for status, length and ctl byte)
1153 and the status word (set to zeros). */
1154 {
1155 u_char *buf = skb->data;
1156 u_int length = skb->len; /* The chip will pad to ethernet min. */
1157
Joe Perches636b8112010-08-12 12:22:51 +00001158 netdev_dbg(dev, "Trying to xmit packet of length %d\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /* send the packet length: +6 for status word, length, and ctl */
1161 outw(0, ioaddr + DATA_1);
1162 outw(length + 6, ioaddr + DATA_1);
1163 outsw(ioaddr + DATA_1, buf, length >> 1);
1164
1165 /* The odd last byte, if there is one, goes in the control word. */
1166 outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
1167 }
1168
1169 /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
1170 outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
1171 (inw(ioaddr + INTERRUPT) & 0xff00),
1172 ioaddr + INTERRUPT);
1173
1174 /* The chip does the rest of the work. */
1175 outw(MC_ENQUEUE , ioaddr + MMU_CMD);
1176
1177 smc->saved_skb = NULL;
1178 dev_kfree_skb_irq(skb);
1179 dev->trans_start = jiffies;
1180 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183/*====================================================================*/
1184
1185static void smc_tx_timeout(struct net_device *dev)
1186{
1187 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001188 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Joe Perches636b8112010-08-12 12:22:51 +00001190 netdev_notice(dev, "transmit timed out, Tx_status %2.2x status %4.4x.\n",
1191 inw(ioaddr)&0xff, inw(ioaddr + 2));
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001192 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 smc_reset(dev);
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001194 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 smc->saved_skb = NULL;
1196 netif_wake_queue(dev);
1197}
1198
Stephen Hemmingerdbf02fa2009-08-31 19:50:49 +00001199static netdev_tx_t smc_start_xmit(struct sk_buff *skb,
1200 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001203 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 u_short num_pages;
1205 short time_out, ir;
Komuro85e27832007-07-23 21:36:06 +09001206 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 netif_stop_queue(dev);
1209
Joe Perches636b8112010-08-12 12:22:51 +00001210 netdev_dbg(dev, "smc_start_xmit(length = %d) called, status %04x\n",
1211 skb->len, inw(ioaddr + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 if (smc->saved_skb) {
1214 /* THIS SHOULD NEVER HAPPEN. */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001215 dev->stats.tx_aborted_errors++;
Joe Perches636b8112010-08-12 12:22:51 +00001216 netdev_printk(KERN_DEBUG, dev,
1217 "Internal error -- sent packet while busy\n");
Patrick McHardy5b548142009-06-12 06:22:29 +00001218 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 smc->saved_skb = skb;
1221
1222 num_pages = skb->len >> 8;
1223
1224 if (num_pages > 7) {
Joe Perches636b8112010-08-12 12:22:51 +00001225 netdev_err(dev, "Far too big packet error: %d pages\n", num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 dev_kfree_skb (skb);
1227 smc->saved_skb = NULL;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001228 dev->stats.tx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001229 return NETDEV_TX_OK; /* Do not re-queue this packet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 /* A packet is now waiting. */
1232 smc->packets_waiting++;
1233
Komuro85e27832007-07-23 21:36:06 +09001234 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 SMC_SELECT_BANK(2); /* Paranoia, we should always be in window 2 */
1236
1237 /* need MC_RESET to keep the memory consistent. errata? */
1238 if (smc->rx_ovrn) {
1239 outw(MC_RESET, ioaddr + MMU_CMD);
1240 smc->rx_ovrn = 0;
1241 }
1242
1243 /* Allocate the memory; send the packet now if we win. */
1244 outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
1245 for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
1246 ir = inw(ioaddr+INTERRUPT);
1247 if (ir & IM_ALLOC_INT) {
1248 /* Acknowledge the interrupt, send the packet. */
1249 outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
1250 smc_hardware_send_packet(dev); /* Send the packet now.. */
Komuro85e27832007-07-23 21:36:06 +09001251 spin_unlock_irqrestore(&smc->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001252 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 }
1255
1256 /* Otherwise defer until the Tx-space-allocated interrupt. */
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001257 pr_debug("%s: memory allocation deferred.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
Komuro85e27832007-07-23 21:36:06 +09001259 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Patrick McHardy6ed10652009-06-23 06:03:08 +00001261 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264/*======================================================================
1265
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001266 Handle a Tx anomalous event. Entered while in Window 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268======================================================================*/
1269
1270static void smc_tx_err(struct net_device * dev)
1271{
1272 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001273 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
1275 int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
1276 int tx_status;
1277
1278 /* select this as the packet to read from */
1279 outw(packet_no, ioaddr + PNR_ARR);
1280
1281 /* read the first word from this packet */
1282 outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
1283
1284 tx_status = inw(ioaddr + DATA_1);
1285
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001286 dev->stats.tx_errors++;
1287 if (tx_status & TS_LOSTCAR) dev->stats.tx_carrier_errors++;
1288 if (tx_status & TS_LATCOL) dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (tx_status & TS_16COL) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001290 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 smc->tx_err++;
1292 }
1293
1294 if (tx_status & TS_SUCCESS) {
Joe Perches636b8112010-08-12 12:22:51 +00001295 netdev_notice(dev, "Successful packet caused error interrupt?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297 /* re-enable transmit */
1298 SMC_SELECT_BANK(0);
1299 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1300 SMC_SELECT_BANK(2);
1301
1302 outw(MC_FREEPKT, ioaddr + MMU_CMD); /* Free the packet memory. */
1303
1304 /* one less packet waiting for me */
1305 smc->packets_waiting--;
1306
1307 outw(saved_packet, ioaddr + PNR_ARR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
1310/*====================================================================*/
1311
1312static void smc_eph_irq(struct net_device *dev)
1313{
1314 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001315 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 u_short card_stats, ephs;
1317
1318 SMC_SELECT_BANK(0);
1319 ephs = inw(ioaddr + EPH);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001320 pr_debug("%s: Ethernet protocol handler interrupt, status"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 " %4.4x.\n", dev->name, ephs);
1322 /* Could be a counter roll-over warning: update stats. */
1323 card_stats = inw(ioaddr + COUNTER);
1324 /* single collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001325 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 card_stats >>= 4;
1327 /* multiple collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001328 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329#if 0 /* These are for when linux supports these statistics */
1330 card_stats >>= 4; /* deferred */
1331 card_stats >>= 4; /* excess deferred */
1332#endif
1333 /* If we had a transmit error we must re-enable the transmitter. */
1334 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1335
1336 /* Clear a link error interrupt. */
1337 SMC_SELECT_BANK(1);
1338 outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
1339 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1340 ioaddr + CONTROL);
1341 SMC_SELECT_BANK(2);
1342}
1343
1344/*====================================================================*/
1345
David Howells7d12e782006-10-05 14:55:46 +01001346static irqreturn_t smc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct net_device *dev = dev_id;
1349 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001350 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 u_short saved_bank, saved_pointer, mask, status;
1352 unsigned int handled = 1;
1353 char bogus_cnt = INTR_WORK; /* Work we are willing to do. */
1354
1355 if (!netif_device_present(dev))
1356 return IRQ_NONE;
1357
1358 ioaddr = dev->base_addr;
1359
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001360 pr_debug("%s: SMC91c92 interrupt %d at %#x.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 irq, ioaddr);
1362
Komuro85e27832007-07-23 21:36:06 +09001363 spin_lock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 smc->watchdog = 0;
1365 saved_bank = inw(ioaddr + BANK_SELECT);
1366 if ((saved_bank & 0xff00) != 0x3300) {
1367 /* The device does not exist -- the card could be off-line, or
1368 maybe it has been ejected. */
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001369 pr_debug("%s: SMC91c92 interrupt %d for non-existent"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 "/ejected device.\n", dev->name, irq);
1371 handled = 0;
1372 goto irq_done;
1373 }
1374
1375 SMC_SELECT_BANK(2);
1376 saved_pointer = inw(ioaddr + POINTER);
1377 mask = inw(ioaddr + INTERRUPT) >> 8;
1378 /* clear all interrupts */
1379 outw(0, ioaddr + INTERRUPT);
1380
1381 do { /* read the status flag, and mask it */
1382 status = inw(ioaddr + INTERRUPT) & 0xff;
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001383 pr_debug("%s: Status is %#2.2x (mask %#2.2x).\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 status, mask);
1385 if ((status & mask) == 0) {
1386 if (bogus_cnt == INTR_WORK)
1387 handled = 0;
1388 break;
1389 }
1390 if (status & IM_RCV_INT) {
1391 /* Got a packet(s). */
1392 smc_rx(dev);
1393 }
1394 if (status & IM_TX_INT) {
1395 smc_tx_err(dev);
1396 outw(IM_TX_INT, ioaddr + INTERRUPT);
1397 }
1398 status &= mask;
1399 if (status & IM_TX_EMPTY_INT) {
1400 outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
1401 mask &= ~IM_TX_EMPTY_INT;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001402 dev->stats.tx_packets += smc->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 smc->packets_waiting = 0;
1404 }
1405 if (status & IM_ALLOC_INT) {
1406 /* Clear this interrupt so it doesn't happen again */
1407 mask &= ~IM_ALLOC_INT;
1408
1409 smc_hardware_send_packet(dev);
1410
1411 /* enable xmit interrupts based on this */
1412 mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
1413
1414 /* and let the card send more packets to me */
1415 netif_wake_queue(dev);
1416 }
1417 if (status & IM_RX_OVRN_INT) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001418 dev->stats.rx_errors++;
1419 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (smc->duplex)
1421 smc->rx_ovrn = 1; /* need MC_RESET outside smc_interrupt */
1422 outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
1423 }
1424 if (status & IM_EPH_INT)
1425 smc_eph_irq(dev);
1426 } while (--bogus_cnt);
1427
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001428 pr_debug(" Restoring saved registers mask %2.2x bank %4.4x"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 " pointer %4.4x.\n", mask, saved_bank, saved_pointer);
1430
1431 /* restore state register */
1432 outw((mask<<8), ioaddr + INTERRUPT);
1433 outw(saved_pointer, ioaddr + POINTER);
1434 SMC_SELECT_BANK(saved_bank);
1435
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001436 pr_debug("%s: Exiting interrupt IRQ%d.\n", dev->name, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438irq_done:
1439
1440 if ((smc->manfid == MANFID_OSITECH) &&
1441 (smc->cardid != PRODID_OSITECH_SEVEN)) {
1442 /* Retrigger interrupt if needed */
1443 mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
1444 set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
1445 }
1446 if (smc->manfid == MANFID_MOTOROLA) {
1447 u_char cor;
1448 cor = readb(smc->base + MOT_UART + CISREG_COR);
1449 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
1450 writeb(cor, smc->base + MOT_UART + CISREG_COR);
1451 cor = readb(smc->base + MOT_LAN + CISREG_COR);
1452 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
1453 writeb(cor, smc->base + MOT_LAN + CISREG_COR);
1454 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001455
1456 if ((smc->base != NULL) && /* Megahertz MFC's */
1457 (smc->manfid == MANFID_MEGAHERTZ) &&
1458 (smc->cardid == PRODID_MEGAHERTZ_EM3288)) {
1459
1460 u_char tmp;
1461 tmp = readb(smc->base+MEGAHERTZ_ISR);
1462 tmp = readb(smc->base+MEGAHERTZ_ISR);
1463
1464 /* Retrigger interrupt if needed */
1465 writeb(tmp, smc->base + MEGAHERTZ_ISR);
1466 writeb(tmp, smc->base + MEGAHERTZ_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001468
Komuro85e27832007-07-23 21:36:06 +09001469 spin_unlock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return IRQ_RETVAL(handled);
1471}
1472
1473/*====================================================================*/
1474
1475static void smc_rx(struct net_device *dev)
1476{
Olof Johansson906da802008-02-04 22:27:35 -08001477 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int rx_status;
1479 int packet_length; /* Caution: not frame length, rather words
1480 to transfer from the chip. */
1481
1482 /* Assertion: we are in Window 2. */
1483
1484 if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
Joe Perches636b8112010-08-12 12:22:51 +00001485 netdev_err(dev, "smc_rx() with nothing on Rx FIFO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return;
1487 }
1488
1489 /* Reset the read pointer, and read the status and packet length. */
1490 outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
1491 rx_status = inw(ioaddr + DATA_1);
1492 packet_length = inw(ioaddr + DATA_1) & 0x07ff;
1493
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001494 pr_debug("%s: Receive status %4.4x length %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 dev->name, rx_status, packet_length);
1496
1497 if (!(rx_status & RS_ERRORS)) {
1498 /* do stuff to make a new packet */
1499 struct sk_buff *skb;
1500
1501 /* Note: packet_length adds 5 or 6 extra bytes here! */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001502 skb = netdev_alloc_skb(dev, packet_length+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 if (skb == NULL) {
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001505 pr_debug("%s: Low memory, packet dropped.\n", dev->name);
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001506 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 outw(MC_RELEASE, ioaddr + MMU_CMD);
1508 return;
1509 }
1510
1511 packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
1512 skb_reserve(skb, 2);
1513 insw(ioaddr+DATA_1, skb_put(skb, packet_length),
1514 (packet_length+1)>>1);
1515 skb->protocol = eth_type_trans(skb, dev);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 netif_rx(skb);
1518 dev->last_rx = jiffies;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001519 dev->stats.rx_packets++;
1520 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (rx_status & RS_MULTICAST)
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001522 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 } else {
1524 /* error ... */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001525 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001527 if (rx_status & RS_ALGNERR) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001529 dev->stats.rx_length_errors++;
1530 if (rx_status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532 /* Let the MMU free the memory of this packet. */
1533 outw(MC_RELEASE, ioaddr + MMU_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536/*======================================================================
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 Set the receive mode.
1539
1540 This routine is used by both the protocol level to notify us of
1541 promiscuous/multicast mode changes, and by the open/reset code to
1542 initialize the Rx registers. We always set the multicast list and
1543 leave the receiver running.
1544
1545======================================================================*/
1546
1547static void set_rx_mode(struct net_device *dev)
1548{
Olof Johansson906da802008-02-04 22:27:35 -08001549 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct smc_private *smc = netdev_priv(dev);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001551 unsigned char multicast_table[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 unsigned long flags;
1553 u_short rx_cfg_setting;
Ken Kawasakia6d37022010-04-10 12:50:14 +00001554 int i;
1555
1556 memset(multicast_table, 0, sizeof(multicast_table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 rx_cfg_setting = RxStripCRC | RxEnable | RxPromisc | RxAllMulti;
1560 } else if (dev->flags & IFF_ALLMULTI)
1561 rx_cfg_setting = RxStripCRC | RxEnable | RxAllMulti;
1562 else {
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001563 if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001564 struct netdev_hw_addr *ha;
Jiri Pirko91fea582010-02-19 08:48:47 +00001565
Jiri Pirko22bedad32010-04-01 21:22:57 +00001566 netdev_for_each_mc_addr(ha, dev) {
1567 u_int position = ether_crc(6, ha->addr);
Jiri Pirko91fea582010-02-19 08:48:47 +00001568 multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
1569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 rx_cfg_setting = RxStripCRC | RxEnable;
1572 }
1573
1574 /* Load MC table and Rx setting into the chip without interrupts. */
1575 spin_lock_irqsave(&smc->lock, flags);
1576 SMC_SELECT_BANK(3);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001577 for (i = 0; i < 8; i++)
1578 outb(multicast_table[i], ioaddr + MULTICAST0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 SMC_SELECT_BANK(0);
1580 outw(rx_cfg_setting, ioaddr + RCR);
1581 SMC_SELECT_BANK(2);
1582 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585/*======================================================================
1586
1587 Senses when a card's config changes. Here, it's coax or TP.
1588
1589======================================================================*/
1590
1591static int s9k_config(struct net_device *dev, struct ifmap *map)
1592{
1593 struct smc_private *smc = netdev_priv(dev);
1594 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1595 if (smc->cfg & CFG_MII_SELECT)
1596 return -EOPNOTSUPP;
1597 else if (map->port > 2)
1598 return -EINVAL;
1599 dev->if_port = map->port;
Joe Perches636b8112010-08-12 12:22:51 +00001600 netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 smc_reset(dev);
1602 }
1603 return 0;
1604}
1605
1606/*======================================================================
1607
1608 Reset the chip, reloading every register that might be corrupted.
1609
1610======================================================================*/
1611
1612/*
1613 Set transceiver type, perhaps to something other than what the user
1614 specified in dev->if_port.
1615*/
1616static void smc_set_xcvr(struct net_device *dev, int if_port)
1617{
1618 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001619 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 u_short saved_bank;
1621
1622 saved_bank = inw(ioaddr + BANK_SELECT);
1623 SMC_SELECT_BANK(1);
1624 if (if_port == 2) {
1625 outw(smc->cfg | CFG_AUI_SELECT, ioaddr + CONFIG);
1626 if ((smc->manfid == MANFID_OSITECH) &&
1627 (smc->cardid != PRODID_OSITECH_SEVEN))
1628 set_bits(OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1629 smc->media_status = ((dev->if_port == 0) ? 0x0001 : 0x0002);
1630 } else {
1631 outw(smc->cfg, ioaddr + CONFIG);
1632 if ((smc->manfid == MANFID_OSITECH) &&
1633 (smc->cardid != PRODID_OSITECH_SEVEN))
1634 mask_bits(~OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1635 smc->media_status = ((dev->if_port == 0) ? 0x0012 : 0x4001);
1636 }
1637 SMC_SELECT_BANK(saved_bank);
1638}
1639
1640static void smc_reset(struct net_device *dev)
1641{
Olof Johansson906da802008-02-04 22:27:35 -08001642 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct smc_private *smc = netdev_priv(dev);
1644 int i;
1645
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001646 pr_debug("%s: smc91c92 reset called.\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 /* The first interaction must be a write to bring the chip out
1649 of sleep mode. */
1650 SMC_SELECT_BANK(0);
1651 /* Reset the chip. */
1652 outw(RCR_SOFTRESET, ioaddr + RCR);
1653 udelay(10);
1654
1655 /* Clear the transmit and receive configuration registers. */
1656 outw(RCR_CLEAR, ioaddr + RCR);
1657 outw(TCR_CLEAR, ioaddr + TCR);
1658
1659 /* Set the Window 1 control, configuration and station addr registers.
1660 No point in writing the I/O base register ;-> */
1661 SMC_SELECT_BANK(1);
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001662 /* Automatically release successfully transmitted packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 Accept link errors, counter and Tx error interrupts. */
1664 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1665 ioaddr + CONTROL);
1666 smc_set_xcvr(dev, dev->if_port);
1667 if ((smc->manfid == MANFID_OSITECH) &&
1668 (smc->cardid != PRODID_OSITECH_SEVEN))
1669 outw((dev->if_port == 2 ? OSI_AUI_PWR : 0) |
1670 (inw(ioaddr-0x10+OSITECH_AUI_PWR) & 0xff00),
1671 ioaddr - 0x10 + OSITECH_AUI_PWR);
1672
1673 /* Fill in the physical address. The databook is wrong about the order! */
1674 for (i = 0; i < 6; i += 2)
1675 outw((dev->dev_addr[i+1]<<8)+dev->dev_addr[i],
1676 ioaddr + ADDR0 + i);
1677
1678 /* Reset the MMU */
1679 SMC_SELECT_BANK(2);
1680 outw(MC_RESET, ioaddr + MMU_CMD);
1681 outw(0, ioaddr + INTERRUPT);
1682
1683 /* Re-enable the chip. */
1684 SMC_SELECT_BANK(0);
1685 outw(((smc->cfg & CFG_MII_SELECT) ? 0 : TCR_MONCSN) |
1686 TCR_ENABLE | TCR_PAD_EN | smc->duplex, ioaddr + TCR);
1687 set_rx_mode(dev);
1688
1689 if (smc->cfg & CFG_MII_SELECT) {
1690 SMC_SELECT_BANK(3);
1691
1692 /* Reset MII */
1693 mdio_write(dev, smc->mii_if.phy_id, 0, 0x8000);
1694
1695 /* Advertise 100F, 100H, 10F, 10H */
1696 mdio_write(dev, smc->mii_if.phy_id, 4, 0x01e1);
1697
1698 /* Restart MII autonegotiation */
1699 mdio_write(dev, smc->mii_if.phy_id, 0, 0x0000);
1700 mdio_write(dev, smc->mii_if.phy_id, 0, 0x1200);
1701 }
1702
1703 /* Enable interrupts. */
1704 SMC_SELECT_BANK(2);
1705 outw((IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT) << 8,
1706 ioaddr + INTERRUPT);
1707}
1708
1709/*======================================================================
1710
1711 Media selection timer routine
1712
1713======================================================================*/
1714
1715static void media_check(u_long arg)
1716{
1717 struct net_device *dev = (struct net_device *) arg;
1718 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001719 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 u_short i, media, saved_bank;
1721 u_short link;
Komuro85e27832007-07-23 21:36:06 +09001722 unsigned long flags;
1723
1724 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 saved_bank = inw(ioaddr + BANK_SELECT);
1727
1728 if (!netif_device_present(dev))
1729 goto reschedule;
1730
1731 SMC_SELECT_BANK(2);
1732
1733 /* need MC_RESET to keep the memory consistent. errata? */
1734 if (smc->rx_ovrn) {
1735 outw(MC_RESET, ioaddr + MMU_CMD);
1736 smc->rx_ovrn = 0;
1737 }
1738 i = inw(ioaddr + INTERRUPT);
1739 SMC_SELECT_BANK(0);
1740 media = inw(ioaddr + EPH) & EPH_LINK_OK;
1741 SMC_SELECT_BANK(1);
1742 media |= (inw(ioaddr + CONFIG) & CFG_AUI_SELECT) ? 2 : 1;
1743
Ken Kawasaki2a915152010-04-24 10:37:09 +00001744 SMC_SELECT_BANK(saved_bank);
1745 spin_unlock_irqrestore(&smc->lock, flags);
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* Check for pending interrupt with watchdog flag set: with
1748 this, we can limp along even if the interrupt is blocked */
1749 if (smc->watchdog++ && ((i>>8) & i)) {
1750 if (!smc->fast_poll)
Joe Perches636b8112010-08-12 12:22:51 +00001751 netdev_info(dev, "interrupt(s) dropped!\n");
Ken Kawasaki2a915152010-04-24 10:37:09 +00001752 local_irq_save(flags);
Komuroe363d132007-02-10 11:57:35 +09001753 smc_interrupt(dev->irq, dev);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001754 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 smc->fast_poll = HZ;
1756 }
1757 if (smc->fast_poll) {
1758 smc->fast_poll--;
1759 smc->media.expires = jiffies + HZ/100;
1760 add_timer(&smc->media);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return;
1762 }
1763
Ken Kawasaki2a915152010-04-24 10:37:09 +00001764 spin_lock_irqsave(&smc->lock, flags);
1765
1766 saved_bank = inw(ioaddr + BANK_SELECT);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (smc->cfg & CFG_MII_SELECT) {
1769 if (smc->mii_if.phy_id < 0)
1770 goto reschedule;
1771
1772 SMC_SELECT_BANK(3);
1773 link = mdio_read(dev, smc->mii_if.phy_id, 1);
1774 if (!link || (link == 0xffff)) {
Joe Perches636b8112010-08-12 12:22:51 +00001775 netdev_info(dev, "MII is missing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 smc->mii_if.phy_id = -1;
1777 goto reschedule;
1778 }
1779
1780 link &= 0x0004;
1781 if (link != smc->link_status) {
1782 u_short p = mdio_read(dev, smc->mii_if.phy_id, 5);
Joe Perches636b8112010-08-12 12:22:51 +00001783 netdev_info(dev, "%s link beat\n", link ? "found" : "lost");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 smc->duplex = (((p & 0x0100) || ((p & 0x1c0) == 0x40))
1785 ? TCR_FDUPLX : 0);
1786 if (link) {
Joe Perches636b8112010-08-12 12:22:51 +00001787 netdev_info(dev, "autonegotiation complete: "
1788 "%dbaseT-%cD selected\n",
1789 (p & 0x0180) ? 100 : 10, smc->duplex ? 'F' : 'H');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
1791 SMC_SELECT_BANK(0);
1792 outw(inw(ioaddr + TCR) | smc->duplex, ioaddr + TCR);
1793 smc->link_status = link;
1794 }
1795 goto reschedule;
1796 }
1797
1798 /* Ignore collisions unless we've had no rx's recently */
Marcelo Feitoza Parisi4851d3a2005-07-15 10:00:41 -07001799 if (time_after(jiffies, dev->last_rx + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (smc->tx_err || (smc->media_status & EPH_16COL))
1801 media |= EPH_16COL;
1802 }
1803 smc->tx_err = 0;
1804
1805 if (media != smc->media_status) {
1806 if ((media & smc->media_status & 1) &&
1807 ((smc->media_status ^ media) & EPH_LINK_OK))
Joe Perches636b8112010-08-12 12:22:51 +00001808 netdev_info(dev, "%s link beat\n",
1809 smc->media_status & EPH_LINK_OK ? "lost" : "found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 else if ((media & smc->media_status & 2) &&
1811 ((smc->media_status ^ media) & EPH_16COL))
Joe Perches636b8112010-08-12 12:22:51 +00001812 netdev_info(dev, "coax cable %s\n",
1813 media & EPH_16COL ? "problem" : "ok");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (dev->if_port == 0) {
1815 if (media & 1) {
1816 if (media & EPH_LINK_OK)
Joe Perches636b8112010-08-12 12:22:51 +00001817 netdev_info(dev, "flipped to 10baseT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 else
1819 smc_set_xcvr(dev, 2);
1820 } else {
1821 if (media & EPH_16COL)
1822 smc_set_xcvr(dev, 1);
1823 else
Joe Perches636b8112010-08-12 12:22:51 +00001824 netdev_info(dev, "flipped to 10base2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826 }
1827 smc->media_status = media;
1828 }
1829
1830reschedule:
1831 smc->media.expires = jiffies + HZ;
1832 add_timer(&smc->media);
1833 SMC_SELECT_BANK(saved_bank);
Komuro85e27832007-07-23 21:36:06 +09001834 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static int smc_link_ok(struct net_device *dev)
1838{
Olof Johansson906da802008-02-04 22:27:35 -08001839 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct smc_private *smc = netdev_priv(dev);
1841
1842 if (smc->cfg & CFG_MII_SELECT) {
1843 return mii_link_ok(&smc->mii_if);
1844 } else {
1845 SMC_SELECT_BANK(0);
1846 return inw(ioaddr + EPH) & EPH_LINK_OK;
1847 }
1848}
1849
1850static int smc_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1851{
1852 u16 tmp;
Olof Johansson906da802008-02-04 22:27:35 -08001853 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 ecmd->supported = (SUPPORTED_TP | SUPPORTED_AUI |
1856 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full);
1857
1858 SMC_SELECT_BANK(1);
1859 tmp = inw(ioaddr + CONFIG);
1860 ecmd->port = (tmp & CFG_AUI_SELECT) ? PORT_AUI : PORT_TP;
1861 ecmd->transceiver = XCVR_INTERNAL;
David Decotigny70739492011-04-27 18:32:40 +00001862 ethtool_cmd_speed_set(ecmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ecmd->phy_address = ioaddr + MGMT;
1864
1865 SMC_SELECT_BANK(0);
1866 tmp = inw(ioaddr + TCR);
1867 ecmd->duplex = (tmp & TCR_FDUPLX) ? DUPLEX_FULL : DUPLEX_HALF;
1868
1869 return 0;
1870}
1871
1872static int smc_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1873{
1874 u16 tmp;
Olof Johansson906da802008-02-04 22:27:35 -08001875 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
David Decotigny25db0332011-04-27 18:32:39 +00001877 if (ethtool_cmd_speed(ecmd) != SPEED_10)
1878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1880 return -EINVAL;
1881 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI)
1882 return -EINVAL;
1883 if (ecmd->transceiver != XCVR_INTERNAL)
1884 return -EINVAL;
1885
1886 if (ecmd->port == PORT_AUI)
1887 smc_set_xcvr(dev, 1);
1888 else
1889 smc_set_xcvr(dev, 0);
1890
1891 SMC_SELECT_BANK(0);
1892 tmp = inw(ioaddr + TCR);
1893 if (ecmd->duplex == DUPLEX_FULL)
1894 tmp |= TCR_FDUPLX;
1895 else
1896 tmp &= ~TCR_FDUPLX;
1897 outw(tmp, ioaddr + TCR);
1898
1899 return 0;
1900}
1901
1902static int check_if_running(struct net_device *dev)
1903{
1904 if (!netif_running(dev))
1905 return -EINVAL;
1906 return 0;
1907}
1908
1909static void smc_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1910{
Rick Jones33a5ba12011-11-15 14:59:53 +00001911 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1912 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static int smc_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1916{
1917 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001918 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1920 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001921 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Ken Kawasaki2a915152010-04-24 10:37:09 +00001923 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001924 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (smc->cfg & CFG_MII_SELECT)
1926 ret = mii_ethtool_gset(&smc->mii_if, ecmd);
1927 else
1928 ret = smc_netdev_get_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001930 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return ret;
1932}
1933
1934static int smc_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1935{
1936 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001937 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1939 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001940 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Ken Kawasaki2a915152010-04-24 10:37:09 +00001942 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001943 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (smc->cfg & CFG_MII_SELECT)
1945 ret = mii_ethtool_sset(&smc->mii_if, ecmd);
1946 else
1947 ret = smc_netdev_set_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001949 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return ret;
1951}
1952
1953static u32 smc_get_link(struct net_device *dev)
1954{
1955 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da802008-02-04 22:27:35 -08001956 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1958 u32 ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001959 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Ken Kawasaki2a915152010-04-24 10:37:09 +00001961 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001962 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ret = smc_link_ok(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001965 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return ret;
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969static int smc_nway_reset(struct net_device *dev)
1970{
1971 struct smc_private *smc = netdev_priv(dev);
1972 if (smc->cfg & CFG_MII_SELECT) {
Olof Johansson906da802008-02-04 22:27:35 -08001973 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1975 int res;
1976
1977 SMC_SELECT_BANK(3);
1978 res = mii_nway_restart(&smc->mii_if);
1979 SMC_SELECT_BANK(saved_bank);
1980
1981 return res;
1982 } else
1983 return -EOPNOTSUPP;
1984}
1985
Jeff Garzik7282d492006-09-13 14:30:00 -04001986static const struct ethtool_ops ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 .begin = check_if_running,
1988 .get_drvinfo = smc_get_drvinfo,
1989 .get_settings = smc_get_settings,
1990 .set_settings = smc_set_settings,
1991 .get_link = smc_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 .nway_reset = smc_nway_reset,
1993};
1994
1995static int smc_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1996{
1997 struct smc_private *smc = netdev_priv(dev);
1998 struct mii_ioctl_data *mii = if_mii(rq);
1999 int rc = 0;
2000 u16 saved_bank;
Olof Johansson906da802008-02-04 22:27:35 -08002001 unsigned int ioaddr = dev->base_addr;
Ken Kawasaki2a915152010-04-24 10:37:09 +00002002 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 if (!netif_running(dev))
2005 return -EINVAL;
2006
Ken Kawasaki2a915152010-04-24 10:37:09 +00002007 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 saved_bank = inw(ioaddr + BANK_SELECT);
2009 SMC_SELECT_BANK(3);
2010 rc = generic_mii_ioctl(&smc->mii_if, mii, cmd, NULL);
2011 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00002012 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return rc;
2014}
2015
Joe Perches25f8f542011-05-03 19:29:01 -07002016static const struct pcmcia_device_id smc91c92_ids[] = {
Dominik Brodowski5c672222005-06-27 16:28:27 -07002017 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0109, 0x0501),
2018 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0140, 0x000a),
2019 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
2020 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
2021 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
2022 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
2023 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
2024 PCMCIA_PFC_DEVICE_PROD_ID12(0, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
Komurod277ad02005-07-28 01:07:24 -07002025 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
2026 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002027 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x016c, 0x0020),
2028 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0023),
2029 PCMCIA_DEVICE_PROD_ID123("BASICS by New Media Corporation", "Ethernet", "SMC91C94", 0x23c78a9d, 0x00b2e941, 0xcef397fb),
2030 PCMCIA_DEVICE_PROD_ID12("ARGOSY", "Fast Ethernet PCCard", 0x78f308dc, 0xdcea68bc),
2031 PCMCIA_DEVICE_PROD_ID12("dit Co., Ltd.", "PC Card-10/100BTX", 0xe59365c8, 0x6a2161d1),
2032 PCMCIA_DEVICE_PROD_ID12("DYNALINK", "L100C", 0x6a26d1cf, 0xc16ce9c5),
2033 PCMCIA_DEVICE_PROD_ID12("Farallon", "Farallon Enet", 0x58d93fc4, 0x244734e9),
2034 PCMCIA_DEVICE_PROD_ID12("Megahertz", "CC10BT/2", 0x33234748, 0x3c95b953),
2035 PCMCIA_DEVICE_PROD_ID12("MELCO/SMC", "LPC-TX", 0xa2cd8e6d, 0x42da662a),
Komurod277ad02005-07-28 01:07:24 -07002036 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Four of Diamonds Ethernet", 0xc2f80cd, 0xb3466314),
2037 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Seven of Diamonds Ethernet", 0xc2f80cd, 0x194b650a),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002038 PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Fast Ethernet PCCard", 0x281f1c5d, 0xdcea68bc),
2039 PCMCIA_DEVICE_PROD_ID12("Psion", "10Mb Ethernet", 0x4ef00b21, 0x844be9e9),
2040 PCMCIA_DEVICE_PROD_ID12("SMC", "EtherEZ Ethernet 8020", 0xc4f8b18b, 0x4a0eeb2d),
2041 /* These conflict with other cards! */
2042 /* PCMCIA_DEVICE_MANF_CARD(0x0186, 0x0100), */
2043 /* PCMCIA_DEVICE_MANF_CARD(0x8a01, 0xc1ab), */
2044 PCMCIA_DEVICE_NULL,
2045};
2046MODULE_DEVICE_TABLE(pcmcia, smc91c92_ids);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048static struct pcmcia_driver smc91c92_cs_driver = {
2049 .owner = THIS_MODULE,
Dominik Brodowski2e9b9812010-08-08 11:36:26 +02002050 .name = "smc91c92_cs",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002051 .probe = smc91c92_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002052 .remove = smc91c92_detach,
Dominik Brodowski5c672222005-06-27 16:28:27 -07002053 .id_table = smc91c92_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002054 .suspend = smc91c92_suspend,
2055 .resume = smc91c92_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056};
2057
2058static int __init init_smc91c92_cs(void)
2059{
2060 return pcmcia_register_driver(&smc91c92_cs_driver);
2061}
2062
2063static void __exit exit_smc91c92_cs(void)
2064{
2065 pcmcia_unregister_driver(&smc91c92_cs_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068module_init(init_smc91c92_cs);
2069module_exit(exit_smc91c92_cs);