blob: 97280daba27f7f8349eaaabc71e4514c33d9fcf3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/string.h>
34#include <linux/timer.h>
35#include <linux/interrupt.h>
36#include <linux/delay.h>
37#include <linux/crc32.h>
38#include <linux/netdevice.h>
39#include <linux/etherdevice.h>
40#include <linux/skbuff.h>
41#include <linux/if_arp.h>
42#include <linux/ioport.h>
43#include <linux/ethtool.h>
44#include <linux/mii.h>
Marcelo Feitoza Parisi4851d3a2005-07-15 10:00:41 -070045#include <linux/jiffies.h>
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <pcmcia/cistpl.h>
49#include <pcmcia/cisreg.h>
50#include <pcmcia/ciscode.h>
51#include <pcmcia/ds.h>
Dominik Brodowski50db3fd2006-01-15 10:05:19 +010052#include <pcmcia/ss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#include <asm/io.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*====================================================================*/
58
Arjan van de Venf71e1302006-03-03 21:33:57 -050059static const char *if_names[] = { "auto", "10baseT", "10base2"};
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053061/* Firmware name */
62#define FIRMWARE_NAME "ositech/Xilinx7OD.bin"
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* Module parameters */
65
66MODULE_DESCRIPTION("SMC 91c92 series PCMCIA ethernet driver");
67MODULE_LICENSE("GPL");
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +053068MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0)
71
72/*
73 Transceiver/media type.
74 0 = auto
75 1 = 10baseT (and autoselect if #define AUTOSELECT),
76 2 = AUI/10base2,
77*/
78INT_MODULE_PARM(if_port, 0);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define DRV_NAME "smc91c92_cs"
Andy Gospodarekd5b20692006-09-11 17:39:18 -040082#define DRV_VERSION "1.123"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*====================================================================*/
85
86/* Operational parameter that usually are not changed. */
87
88/* Time in jiffies before concluding Tx hung */
89#define TX_TIMEOUT ((400*HZ)/1000)
90
91/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
92#define INTR_WORK 4
93
94/* Times to check the check the chip before concluding that it doesn't
95 currently have room for another Tx packet. */
96#define MEMORY_WAIT_TIME 8
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct smc_private {
Dominik Brodowskifd238232006-03-05 10:45:09 +010099 struct pcmcia_device *p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spinlock_t lock;
101 u_short manfid;
102 u_short cardid;
Stephen Hemminger6fb72982009-03-20 19:36:09 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct sk_buff *saved_skb;
105 int packets_waiting;
106 void __iomem *base;
107 u_short cfg;
108 struct timer_list media;
109 int watchdog, tx_err;
110 u_short media_status;
111 u_short fast_poll;
112 u_short link_status;
113 struct mii_if_info mii_if;
114 int duplex;
115 int rx_ovrn;
Tobias Klauser4a7c9722017-01-18 17:45:01 +0100116 unsigned long last_rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
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 Johansson906da809c2008-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_set_mac_address = eth_mac_addr,
299 .ndo_validate_addr = eth_validate_addr,
300};
301
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200302static int smc91c92_probe(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct smc_private *smc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200307 dev_dbg(&link->dev, "smc91c92_attach()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Create new ethernet device */
310 dev = alloc_etherdev(sizeof(struct smc_private));
311 if (!dev)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100312 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200314 smc->p_dev = link;
Dominik Brodowski44b496f2010-06-11 04:44:55 +0000315 link->priv = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 spin_lock_init(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* The SMC91c92-specific entries in the device structure. */
Stephen Hemminger9b31b692009-03-20 19:36:10 +0000320 dev->netdev_ops = &smc_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000321 dev->ethtool_ops = &ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dev->watchdog_timeo = TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 smc->mii_if.dev = dev;
325 smc->mii_if.mdio_read = mdio_read;
326 smc->mii_if.mdio_write = mdio_write;
327 smc->mii_if.phy_id_mask = 0x1f;
328 smc->mii_if.reg_num_mask = 0x1f;
329
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200330 return smc91c92_config(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331} /* smc91c92_attach */
332
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200333static void smc91c92_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200337 dev_dbg(&link->dev, "smc91c92_detach\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dominik Brodowskic7c2fa02010-03-20 19:39:26 +0100339 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100341 smc91c92_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 free_netdev(dev);
344} /* smc91c92_detach */
345
346/*====================================================================*/
347
348static int cvt_ascii_address(struct net_device *dev, char *s)
349{
350 int i, j, da, c;
351
352 if (strlen(s) != 12)
353 return -1;
354 for (i = 0; i < 6; i++) {
355 da = 0;
356 for (j = 0; j < 2; j++) {
357 c = *s++;
358 da <<= 4;
359 da += ((c >= '0') && (c <= '9')) ?
360 (c - '0') : ((c & 0x0f) + 9);
361 }
362 dev->dev_addr[i] = da;
363 }
364 return 0;
365}
366
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200367/*====================================================================
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 Configuration stuff for Megahertz cards
370
371 mhz_3288_power() is used to power up a 3288's ethernet chip.
372 mhz_mfc_config() handles socket setup for multifunction (1144
373 and 3288) cards. mhz_setup() gets a card's hardware ethernet
374 address.
375
376======================================================================*/
377
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200378static int mhz_3288_power(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct net_device *dev = link->priv;
381 struct smc_private *smc = netdev_priv(dev);
382 u_char tmp;
383
384 /* Read the ISR twice... */
385 readb(smc->base+MEGAHERTZ_ISR);
386 udelay(5);
387 readb(smc->base+MEGAHERTZ_ISR);
388
389 /* Pause 200ms... */
390 mdelay(200);
391
392 /* Now read and write the COR... */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200393 tmp = readb(smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 udelay(5);
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200395 writeb(tmp, smc->base + link->config_base + CISREG_COR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 return 0;
398}
399
Dominik Brodowski00990e72010-07-30 13:13:46 +0200400static int mhz_mfc_config_check(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200401{
402 int k;
Dominik Brodowski00990e72010-07-30 13:13:46 +0200403 p_dev->io_lines = 16;
404 p_dev->resource[1]->start = p_dev->resource[0]->start;
405 p_dev->resource[1]->end = 8;
406 p_dev->resource[1]->flags &= ~IO_DATA_PATH_WIDTH;
407 p_dev->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
408 p_dev->resource[0]->end = 16;
409 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
410 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200411 for (k = 0; k < 0x400; k += 0x10) {
412 if (k & 0x80)
413 continue;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200414 p_dev->resource[0]->start = k ^ 0x300;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200415 if (!pcmcia_request_io(p_dev))
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200416 return 0;
417 }
418 return -ENODEV;
419}
420
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200421static int mhz_mfc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct net_device *dev = link->priv;
424 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskib5cb2592010-07-24 18:46:42 +0200425 unsigned int offset;
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200426 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dominik Brodowski00990e72010-07-30 13:13:46 +0200428 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ |
429 CONF_AUTO_SET_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* The Megahertz combo cards have modem-like CIS entries, so
432 we have to explicitly try a bunch of port combinations. */
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200433 if (pcmcia_loop_config(link, mhz_mfc_config_check, NULL))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200434 return -ENODEV;
435
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200436 dev->base_addr = link->resource[0]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* Allocate a memory window, for accessing the ISR */
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200439 link->resource[2]->flags = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
440 link->resource[2]->start = link->resource[2]->end = 0;
441 i = pcmcia_request_window(link, link->resource[2], 0);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200442 if (i != 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200443 return -ENODEV;
444
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200445 smc->base = ioremap(link->resource[2]->start,
446 resource_size(link->resource[2]));
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200447 offset = (smc->manfid == MANFID_MOTOROLA) ? link->config_base : 0;
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200448 i = pcmcia_map_mem_page(link, link->resource[2], offset);
Joe Perches8e95a202009-12-03 07:58:21 +0000449 if ((i == 0) &&
450 (smc->manfid == MANFID_MEGAHERTZ) &&
451 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
452 mhz_3288_power(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200457static int pcmcia_get_versmac(struct pcmcia_device *p_dev,
458 tuple_t *tuple,
459 void *priv)
460{
461 struct net_device *dev = priv;
462 cisparse_t parse;
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700463 u8 *buf;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200464
465 if (pcmcia_parse_tuple(tuple, &parse))
466 return -EINVAL;
467
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700468 buf = parse.version_1.str + parse.version_1.ofs[3];
469
470 if ((parse.version_1.ns > 3) && (cvt_ascii_address(dev, buf) == 0))
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200471 return 0;
472
473 return -EINVAL;
474};
475
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200476static int mhz_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200479 size_t len;
480 u8 *buf;
Yum Rayan4638aef42005-05-05 15:14:10 -0700481 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* Read the station address from the CIS. It is stored as the last
484 (fourth) string in the Version 1 Version/ID tuple. */
Dominik Brodowski7d2e8d02009-10-18 18:22:32 +0200485 if ((link->prod_id[3]) &&
486 (cvt_ascii_address(dev, link->prod_id[3]) == 0))
487 return 0;
488
489 /* Workarounds for broken cards start here. */
Chuck Ebberta1a98b72008-02-13 19:47:11 -0500490 /* Ugh -- the EM1144 card has two VERS_1 tuples!?! */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200491 if (!pcmcia_loop_tuple(link, CISTPL_VERS_1, pcmcia_get_versmac, dev))
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Another possibility: for the EM3288, in a special tuple */
Yum Rayan4638aef42005-05-05 15:14:10 -0700495 rc = -1;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200496 len = pcmcia_get_tuple(link, 0x81, &buf);
497 if (buf && len >= 13) {
498 buf[12] = '\0';
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700499 if (cvt_ascii_address(dev, buf) == 0)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200500 rc = 0;
501 }
502 kfree(buf);
503
504 return rc;
505};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507/*======================================================================
508
509 Configuration stuff for the Motorola Mariner
510
511 mot_config() writes directly to the Mariner configuration
512 registers because the CIS is just bogus.
513
514======================================================================*/
515
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200516static void mot_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct net_device *dev = link->priv;
519 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -0800520 unsigned int ioaddr = dev->base_addr;
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200521 unsigned int iouart = link->resource[1]->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Set UART base address and force map with COR bit 1 */
524 writeb(iouart & 0xff, smc->base + MOT_UART + CISREG_IOBASE_0);
525 writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
526 writeb(MOT_NORMAL, smc->base + MOT_UART + CISREG_COR);
527
528 /* Set SMC base address and force map with COR bit 1 */
529 writeb(ioaddr & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_0);
530 writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
531 writeb(MOT_NORMAL, smc->base + MOT_LAN + CISREG_COR);
532
533 /* Wait for things to settle down */
534 mdelay(100);
535}
536
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200537static int mot_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct net_device *dev = link->priv;
Olof Johansson906da809c2008-02-04 22:27:35 -0800540 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int i, wait, loop;
542 u_int addr;
543
544 /* Read Ethernet address from Serial EEPROM */
545
546 for (i = 0; i < 3; i++) {
547 SMC_SELECT_BANK(2);
548 outw(MOT_EEPROM + i, ioaddr + POINTER);
549 SMC_SELECT_BANK(1);
550 outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
551
552 for (loop = wait = 0; loop < 200; loop++) {
553 udelay(10);
554 wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
555 if (wait == 0) break;
556 }
557
558 if (wait)
559 return -1;
560
561 addr = inw(ioaddr + GENERAL);
562 dev->dev_addr[2*i] = addr & 0xff;
563 dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
564 }
565
566 return 0;
567}
568
569/*====================================================================*/
570
Dominik Brodowski00990e72010-07-30 13:13:46 +0200571static int smc_configcheck(struct pcmcia_device *p_dev, void *priv_data)
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200572{
Dominik Brodowski00990e72010-07-30 13:13:46 +0200573 p_dev->resource[0]->end = 16;
574 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
575 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
576
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200577 return pcmcia_request_io(p_dev);
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200578}
579
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200580static int smc_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int i;
584
Dominik Brodowski00990e72010-07-30 13:13:46 +0200585 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
586
Dominik Brodowskib54bf942008-08-02 14:28:43 +0200587 i = pcmcia_loop_config(link, smc_configcheck, NULL);
588 if (!i)
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200589 dev->base_addr = link->resource[0]->start;
Yum Rayan4638aef42005-05-05 15:14:10 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return i;
592}
593
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200594
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200595static int smc_setup(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct net_device *dev = link->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* Check for a LAN function extension tuple */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200600 if (!pcmcia_get_mac_from_cis(link, dev))
601 return 0;
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* Try the third string in the Version 1 Version/ID tuple. */
Dominik Brodowskia9606fd2006-06-04 18:06:13 +0200604 if (link->prod_id[2]) {
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200605 if (cvt_ascii_address(dev, link->prod_id[2]) == 0)
606 return 0;
Yum Rayan4638aef42005-05-05 15:14:10 -0700607 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200608 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*====================================================================*/
612
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200613static int osi_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct net_device *dev = link->priv;
Olof Johansson906da809c2008-02-04 22:27:35 -0800616 static const unsigned int com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int i, j;
618
Dominik Brodowski00990e72010-07-30 13:13:46 +0200619 link->config_flags |= CONF_ENABLE_SPKR | CONF_ENABLE_IRQ;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200620 link->resource[0]->end = 64;
621 link->resource[1]->flags |= IO_DATA_PATH_WIDTH_8;
622 link->resource[1]->end = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Enable Hard Decode, LAN, Modem */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200625 link->io_lines = 16;
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200626 link->config_index = 0x23;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 for (i = j = 0; j < 4; j++) {
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200629 link->resource[1]->start = com[j];
630 i = pcmcia_request_io(link);
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200631 if (i == 0)
632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Dominik Brodowski4c89e882008-08-03 10:07:45 +0200634 if (i != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Fallback: turn off hard decode */
Dominik Brodowski7feabb62010-07-29 18:35:47 +0200636 link->config_index = 0x03;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200637 link->resource[1]->end = 0;
638 i = pcmcia_request_io(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200640 dev->base_addr = link->resource[0]->start + 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return i;
642}
643
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530644static int osi_load_firmware(struct pcmcia_device *link)
645{
646 const struct firmware *fw;
647 int i, err;
648
649 err = request_firmware(&fw, FIRMWARE_NAME, &link->dev);
650 if (err) {
651 pr_err("Failed to load firmware \"%s\"\n", FIRMWARE_NAME);
652 return err;
653 }
654
655 /* Download the Seven of Diamonds firmware */
656 for (i = 0; i < fw->size; i++) {
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200657 outb(fw->data[i], link->resource[0]->start + 2);
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530658 udelay(50);
659 }
660 release_firmware(fw);
661 return err;
662}
663
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200664static int pcmcia_osi_mac(struct pcmcia_device *p_dev,
665 tuple_t *tuple,
666 void *priv)
667{
668 struct net_device *dev = priv;
669 int i;
670
671 if (tuple->TupleDataLen < 8)
672 return -EINVAL;
673 if (tuple->TupleData[0] != 0x04)
674 return -EINVAL;
675 for (i = 0; i < 6; i++)
676 dev->dev_addr[i] = tuple->TupleData[i+2];
677 return 0;
678};
679
680
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200681static int osi_setup(struct pcmcia_device *link, u_short manfid, u_short cardid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct net_device *dev = link->priv;
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200684 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Read the station address from tuple 0x90, subtuple 0x04 */
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200687 if (pcmcia_loop_tuple(link, 0x90, pcmcia_osi_mac, dev))
688 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (((manfid == MANFID_OSITECH) &&
691 (cardid == PRODID_OSITECH_SEVEN)) ||
692 ((manfid == MANFID_PSION) &&
693 (cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530694 rc = osi_load_firmware(link);
695 if (rc)
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200696 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else if (manfid == MANFID_OSITECH) {
698 /* Make sure both functions are powered up */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200699 set_bits(0x300, link->resource[0]->start + OSITECH_AUI_PWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* Now, turn on the interrupt for both card functions */
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200701 set_bits(0x300, link->resource[0]->start + OSITECH_RESET_ISR);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200702 dev_dbg(&link->dev, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
Dominik Brodowski9a017a92010-07-24 15:58:54 +0200703 inw(link->resource[0]->start + OSITECH_AUI_PWR),
704 inw(link->resource[0]->start + OSITECH_RESET_ISR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Dominik Brodowskidddfbd82009-10-18 23:54:24 +0200706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200709static int smc91c92_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100710{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100711 struct net_device *dev = link->priv;
712
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100713 if (link->open)
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100714 netif_device_detach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100715
716 return 0;
717}
718
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200719static int smc91c92_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100720{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100721 struct net_device *dev = link->priv;
722 struct smc_private *smc = netdev_priv(dev);
723 int i;
724
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100725 if ((smc->manfid == MANFID_MEGAHERTZ) &&
726 (smc->cardid == PRODID_MEGAHERTZ_EM3288))
727 mhz_3288_power(link);
728 if (smc->manfid == MANFID_MOTOROLA)
729 mot_config(link);
730 if ((smc->manfid == MANFID_OSITECH) &&
731 (smc->cardid != PRODID_OSITECH_SEVEN)) {
732 /* Power up the card and enable interrupts */
733 set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
734 set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
735 }
736 if (((smc->manfid == MANFID_OSITECH) &&
737 (smc->cardid == PRODID_OSITECH_SEVEN)) ||
738 ((smc->manfid == MANFID_PSION) &&
739 (smc->cardid == PRODID_PSION_NET100))) {
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530740 i = osi_load_firmware(link);
741 if (i) {
Ben Boeckelc501b1f2013-11-01 08:53:35 -0400742 netdev_err(dev, "Failed to load firmware\n");
Jaswinder Singh Rajput75bf7582009-03-30 20:19:54 +0530743 return i;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100744 }
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100745 }
746 if (link->open) {
747 smc_reset(dev);
748 netif_device_attach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100749 }
750
751 return 0;
752}
753
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*======================================================================
756
757 This verifies that the chip is some SMC91cXX variant, and returns
758 the revision code if successful. Otherwise, it returns -ENODEV.
759
760======================================================================*/
761
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200762static int check_sig(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct net_device *dev = link->priv;
Olof Johansson906da809c2008-02-04 22:27:35 -0800765 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 int width;
767 u_short s;
768
769 SMC_SELECT_BANK(1);
770 if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
771 /* Try powering up the chip */
772 outw(0, ioaddr + CONTROL);
773 mdelay(55);
774 }
775
776 /* Try setting bus width */
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200777 width = (link->resource[0]->flags == IO_DATA_PATH_WIDTH_AUTO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 s = inb(ioaddr + CONFIG);
779 if (width)
780 s |= CFG_16BIT;
781 else
782 s &= ~CFG_16BIT;
783 outb(s, ioaddr + CONFIG);
784
785 /* Check Base Address Register to make sure bus width is OK */
786 s = inw(ioaddr + BASE_ADDR);
787 if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
788 ((s >> 8) != (s & 0xff))) {
789 SMC_SELECT_BANK(3);
790 s = inw(ioaddr + REVISION);
Eric Dumazet807540b2010-09-23 05:40:09 +0000791 return s & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 if (width) {
Ben Boeckelc501b1f2013-11-01 08:53:35 -0400795 netdev_info(dev, "using 8-bit IO window\n");
Dominik Brodowski50db3fd2006-01-15 10:05:19 +0100796
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200797 smc91c92_suspend(link);
Dominik Brodowskifb49fa52010-07-29 14:06:42 +0200798 pcmcia_fixup_iowidth(link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200799 smc91c92_resume(link);
Dominik Brodowski4bbed522006-01-15 11:18:12 +0100800 return check_sig(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 return -ENODEV;
803}
804
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200805static int smc91c92_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct net_device *dev = link->priv;
808 struct smc_private *smc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 char *name;
Dominik Brodowski74411c02011-07-29 18:27:34 +0200810 int i, rev, j = 0;
Olof Johansson906da809c2008-02-04 22:27:35 -0800811 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 u_long mir;
813
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200814 dev_dbg(&link->dev, "smc91c92_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Dominik Brodowskiefd50582006-10-25 21:28:53 -0400816 smc->manfid = link->manf_id;
817 smc->cardid = link->card_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if ((smc->manfid == MANFID_OSITECH) &&
820 (smc->cardid != PRODID_OSITECH_SEVEN)) {
821 i = osi_config(link);
822 } else if ((smc->manfid == MANFID_MOTOROLA) ||
823 ((smc->manfid == MANFID_MEGAHERTZ) &&
824 ((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
825 (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
826 i = mhz_mfc_config(link);
827 } else {
828 i = smc_config(link);
829 }
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200830 if (i)
831 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dominik Brodowskieb141202010-03-07 12:21:16 +0100833 i = pcmcia_request_irq(link, smc_interrupt);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200834 if (i)
835 goto config_failed;
Dominik Brodowski1ac71e52010-07-29 19:27:09 +0200836 i = pcmcia_enable_device(link);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200837 if (i)
838 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (smc->manfid == MANFID_MOTOROLA)
841 mot_config(link);
842
Dominik Brodowskieb141202010-03-07 12:21:16 +0100843 dev->irq = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if ((if_port >= 0) && (if_port <= 2))
846 dev->if_port = if_port;
847 else
Joe Perches636b8112010-08-12 12:22:51 +0000848 dev_notice(&link->dev, "invalid if_port requested\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 switch (smc->manfid) {
851 case MANFID_OSITECH:
852 case MANFID_PSION:
853 i = osi_setup(link, smc->manfid, smc->cardid); break;
854 case MANFID_SMC:
855 case MANFID_NEW_MEDIA:
856 i = smc_setup(link); break;
857 case 0x128: /* For broken Megahertz cards */
858 case MANFID_MEGAHERTZ:
859 i = mhz_setup(link); break;
860 case MANFID_MOTOROLA:
861 default: /* get the hw address from EEPROM */
862 i = mot_setup(link); break;
863 }
864
865 if (i != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000866 dev_notice(&link->dev, "Unable to find hardware address.\n");
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700867 goto config_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
870 smc->duplex = 0;
871 smc->rx_ovrn = 0;
872
873 rev = check_sig(link);
874 name = "???";
875 if (rev > 0)
876 switch (rev >> 4) {
877 case 3: name = "92"; break;
878 case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
879 case 5: name = "95"; break;
880 case 7: name = "100"; break;
881 case 8: name = "100-FD"; break;
882 case 9: name = "110"; break;
883 }
884
885 ioaddr = dev->base_addr;
886 if (rev > 0) {
887 u_long mcr;
888 SMC_SELECT_BANK(0);
889 mir = inw(ioaddr + MEMINFO) & 0xff;
890 if (mir == 0xff) mir++;
891 /* Get scale factor for memory size */
892 mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
893 mir *= 128 * (1<<((mcr >> 9) & 7));
894 SMC_SELECT_BANK(1);
895 smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
896 smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
897 if (smc->manfid == MANFID_OSITECH)
898 smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
899 if ((rev >> 4) >= 7)
900 smc->cfg |= CFG_MII_SELECT;
901 } else
902 mir = 0;
903
904 if (smc->cfg & CFG_MII_SELECT) {
905 SMC_SELECT_BANK(3);
906
907 for (i = 0; i < 32; i++) {
908 j = mdio_read(dev, i, 1);
909 if ((j != 0) && (j != 0xffff)) break;
910 }
911 smc->mii_if.phy_id = (i < 32) ? i : -1;
912
913 SMC_SELECT_BANK(0);
914 }
915
Dominik Brodowskidd2e5a12009-11-03 10:27:34 +0100916 SET_NETDEV_DEV(dev, &link->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (register_netdev(dev) != 0) {
Joe Perches636b8112010-08-12 12:22:51 +0000919 dev_err(&link->dev, "register_netdev() failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 goto config_undo;
921 }
922
Joe Perches636b8112010-08-12 12:22:51 +0000923 netdev_info(dev, "smc91c%s rev %d: io %#3lx, irq %d, hw_addr %pM\n",
924 name, (rev & 0x0f), dev->base_addr, dev->irq, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (rev > 0) {
927 if (mir & 0x3ff)
Joe Perches636b8112010-08-12 12:22:51 +0000928 netdev_info(dev, " %lu byte", mir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 else
Joe Perches636b8112010-08-12 12:22:51 +0000930 netdev_info(dev, " %lu kb", mir>>10);
931 pr_cont(" buffer, %s xcvr\n",
932 (smc->cfg & CFG_MII_SELECT) ? "MII" : if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934
935 if (smc->cfg & CFG_MII_SELECT) {
936 if (smc->mii_if.phy_id != -1) {
Joe Perches636b8112010-08-12 12:22:51 +0000937 netdev_dbg(dev, " MII transceiver at index %d, status %x\n",
938 smc->mii_if.phy_id, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else {
Joe Perches636b8112010-08-12 12:22:51 +0000940 netdev_notice(dev, " No MII transceivers found!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945config_undo:
946 unregister_netdev(dev);
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200947config_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 smc91c92_release(link);
Ken Kawasakifb9e2d82010-04-03 15:07:10 -0700949 free_netdev(dev);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200950 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951} /* smc91c92_config */
952
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200953static void smc91c92_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200955 dev_dbg(&link->dev, "smc91c92_release\n");
Dominik Brodowskicdb13802010-07-28 10:59:06 +0200956 if (link->resource[2]->end) {
Dominik Brodowski5f2a71f2006-01-15 09:32:39 +0100957 struct net_device *dev = link->priv;
958 struct smc_private *smc = netdev_priv(dev);
959 iounmap(smc->base);
960 }
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200961 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*======================================================================
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 MII interface support for SMC91cXX based cards
967======================================================================*/
968
969#define MDIO_SHIFT_CLK 0x04
970#define MDIO_DATA_OUT 0x01
971#define MDIO_DIR_WRITE 0x08
972#define MDIO_DATA_WRITE0 (MDIO_DIR_WRITE)
973#define MDIO_DATA_WRITE1 (MDIO_DIR_WRITE | MDIO_DATA_OUT)
974#define MDIO_DATA_READ 0x02
975
Olof Johansson906da809c2008-02-04 22:27:35 -0800976static void mdio_sync(unsigned int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 int bits;
979 for (bits = 0; bits < 32; bits++) {
980 outb(MDIO_DATA_WRITE1, addr);
981 outb(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
982 }
983}
984
985static int mdio_read(struct net_device *dev, int phy_id, int loc)
986{
Olof Johansson906da809c2008-02-04 22:27:35 -0800987 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
989 int i, retval = 0;
990
991 mdio_sync(addr);
992 for (i = 13; i >= 0; i--) {
993 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
994 outb(dat, addr);
995 outb(dat | MDIO_SHIFT_CLK, addr);
996 }
997 for (i = 19; i > 0; i--) {
998 outb(0, addr);
999 retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
1000 outb(MDIO_SHIFT_CLK, addr);
1001 }
1002 return (retval>>1) & 0xffff;
1003}
1004
1005static void mdio_write(struct net_device *dev, int phy_id, int loc, int value)
1006{
Olof Johansson906da809c2008-02-04 22:27:35 -08001007 unsigned int addr = dev->base_addr + MGMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
1009 int i;
1010
1011 mdio_sync(addr);
1012 for (i = 31; i >= 0; i--) {
1013 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
1014 outb(dat, addr);
1015 outb(dat | MDIO_SHIFT_CLK, addr);
1016 }
1017 for (i = 1; i >= 0; i--) {
1018 outb(0, addr);
1019 outb(MDIO_SHIFT_CLK, addr);
1020 }
1021}
1022
1023/*======================================================================
1024
1025 The driver core code, most of which should be common with a
1026 non-PCMCIA implementation.
1027
1028======================================================================*/
1029
1030#ifdef PCMCIA_DEBUG
1031static void smc_dump(struct net_device *dev)
1032{
Olof Johansson906da809c2008-02-04 22:27:35 -08001033 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 u_short i, w, save;
1035 save = inw(ioaddr + BANK_SELECT);
1036 for (w = 0; w < 4; w++) {
1037 SMC_SELECT_BANK(w);
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001038 netdev_dbg(dev, "bank %d: ", w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 for (i = 0; i < 14; i += 2)
Joe Perches636b8112010-08-12 12:22:51 +00001040 pr_cont(" %04x", inw(ioaddr + i));
1041 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 outw(save, ioaddr + BANK_SELECT);
1044}
1045#endif
1046
1047static int smc_open(struct net_device *dev)
1048{
1049 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001050 struct pcmcia_device *link = smc->p_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001052 dev_dbg(&link->dev, "%s: smc_open(%p), ID/Window %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 dev->name, dev, inw(dev->base_addr + BANK_SELECT));
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001054#ifdef PCMCIA_DEBUG
1055 smc_dump(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#endif
1057
1058 /* Check that the PCMCIA card is still here. */
Dominik Brodowski9940ec32006-03-05 11:04:33 +01001059 if (!pcmcia_dev_present(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return -ENODEV;
1061 /* Physical device present signature. */
1062 if (check_sig(link) < 0) {
Joe Perches636b8112010-08-12 12:22:51 +00001063 netdev_info(dev, "Yikes! Bad chip signature!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -ENODEV;
1065 }
1066 link->open++;
1067
1068 netif_start_queue(dev);
1069 smc->saved_skb = NULL;
1070 smc->packets_waiting = 0;
1071
1072 smc_reset(dev);
Vaishali Thakkarb8b01342015-02-27 23:40:24 +05301073 setup_timer(&smc->media, media_check, (u_long)dev);
1074 mod_timer(&smc->media, jiffies + HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return 0;
1077} /* smc_open */
1078
1079/*====================================================================*/
1080
1081static int smc_close(struct net_device *dev)
1082{
1083 struct smc_private *smc = netdev_priv(dev);
Dominik Brodowskifba395e2006-03-31 17:21:06 +02001084 struct pcmcia_device *link = smc->p_dev;
Olof Johansson906da809c2008-02-04 22:27:35 -08001085 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Dominik Brodowskidd0fab52009-10-24 15:51:05 +02001087 dev_dbg(&link->dev, "%s: smc_close(), status %4.4x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dev->name, inw(ioaddr + BANK_SELECT));
1089
1090 netif_stop_queue(dev);
1091
1092 /* Shut off all interrupts, and turn off the Tx and Rx sections.
1093 Don't bother to check for chip present. */
1094 SMC_SELECT_BANK(2); /* Nominally paranoia, but do no assume... */
1095 outw(0, ioaddr + INTERRUPT);
1096 SMC_SELECT_BANK(0);
1097 mask_bits(0xff00, ioaddr + RCR);
1098 mask_bits(0xff00, ioaddr + TCR);
1099
1100 /* Put the chip into power-down mode. */
1101 SMC_SELECT_BANK(1);
1102 outw(CTL_POWERDOWN, ioaddr + CONTROL );
1103
1104 link->open--;
1105 del_timer_sync(&smc->media);
1106
1107 return 0;
1108} /* smc_close */
1109
1110/*======================================================================
1111
1112 Transfer a packet to the hardware and trigger the packet send.
1113 This may be called at either from either the Tx queue code
1114 or the interrupt handler.
1115
1116======================================================================*/
1117
1118static void smc_hardware_send_packet(struct net_device * dev)
1119{
1120 struct smc_private *smc = netdev_priv(dev);
1121 struct sk_buff *skb = smc->saved_skb;
Olof Johansson906da809c2008-02-04 22:27:35 -08001122 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 u_char packet_no;
1124
1125 if (!skb) {
Joe Perches636b8112010-08-12 12:22:51 +00001126 netdev_err(dev, "In XMIT with no packet to send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128 }
1129
1130 /* There should be a packet slot waiting. */
1131 packet_no = inw(ioaddr + PNR_ARR) >> 8;
1132 if (packet_no & 0x80) {
1133 /* If not, there is a hardware problem! Likely an ejected card. */
Joe Perches636b8112010-08-12 12:22:51 +00001134 netdev_warn(dev, "hardware Tx buffer allocation failed, status %#2.2x\n",
1135 packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 dev_kfree_skb_irq(skb);
1137 smc->saved_skb = NULL;
1138 netif_start_queue(dev);
1139 return;
1140 }
1141
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001142 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 /* The card should use the just-allocated buffer. */
1144 outw(packet_no, ioaddr + PNR_ARR);
1145 /* point to the beginning of the packet */
1146 outw(PTR_AUTOINC , ioaddr + POINTER);
1147
1148 /* Send the packet length (+6 for status, length and ctl byte)
1149 and the status word (set to zeros). */
1150 {
1151 u_char *buf = skb->data;
1152 u_int length = skb->len; /* The chip will pad to ethernet min. */
1153
Joe Perches636b8112010-08-12 12:22:51 +00001154 netdev_dbg(dev, "Trying to xmit packet of length %d\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* send the packet length: +6 for status word, length, and ctl */
1157 outw(0, ioaddr + DATA_1);
1158 outw(length + 6, ioaddr + DATA_1);
1159 outsw(ioaddr + DATA_1, buf, length >> 1);
1160
1161 /* The odd last byte, if there is one, goes in the control word. */
1162 outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
1163 }
1164
1165 /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
1166 outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
1167 (inw(ioaddr + INTERRUPT) & 0xff00),
1168 ioaddr + INTERRUPT);
1169
1170 /* The chip does the rest of the work. */
1171 outw(MC_ENQUEUE , ioaddr + MMU_CMD);
1172
1173 smc->saved_skb = NULL;
1174 dev_kfree_skb_irq(skb);
Florian Westphal860e9532016-05-03 16:33:13 +02001175 netif_trans_update(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 netif_start_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179/*====================================================================*/
1180
1181static void smc_tx_timeout(struct net_device *dev)
1182{
1183 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001184 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Joe Perches636b8112010-08-12 12:22:51 +00001186 netdev_notice(dev, "transmit timed out, Tx_status %2.2x status %4.4x.\n",
1187 inw(ioaddr)&0xff, inw(ioaddr + 2));
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001188 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 smc_reset(dev);
Florian Westphal860e9532016-05-03 16:33:13 +02001190 netif_trans_update(dev); /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 smc->saved_skb = NULL;
1192 netif_wake_queue(dev);
1193}
1194
Stephen Hemmingerdbf02fa2009-08-31 19:50:49 +00001195static netdev_tx_t smc_start_xmit(struct sk_buff *skb,
1196 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001199 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 u_short num_pages;
1201 short time_out, ir;
Komuro85e27832007-07-23 21:36:06 +09001202 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 netif_stop_queue(dev);
1205
Joe Perches636b8112010-08-12 12:22:51 +00001206 netdev_dbg(dev, "smc_start_xmit(length = %d) called, status %04x\n",
1207 skb->len, inw(ioaddr + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (smc->saved_skb) {
1210 /* THIS SHOULD NEVER HAPPEN. */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001211 dev->stats.tx_aborted_errors++;
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001212 netdev_dbg(dev, "Internal error -- sent packet while busy\n");
Patrick McHardy5b548142009-06-12 06:22:29 +00001213 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 smc->saved_skb = skb;
1216
1217 num_pages = skb->len >> 8;
1218
1219 if (num_pages > 7) {
Joe Perches636b8112010-08-12 12:22:51 +00001220 netdev_err(dev, "Far too big packet error: %d pages\n", num_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 dev_kfree_skb (skb);
1222 smc->saved_skb = NULL;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001223 dev->stats.tx_dropped++;
Patrick McHardy6ed10652009-06-23 06:03:08 +00001224 return NETDEV_TX_OK; /* Do not re-queue this packet. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226 /* A packet is now waiting. */
1227 smc->packets_waiting++;
1228
Komuro85e27832007-07-23 21:36:06 +09001229 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 SMC_SELECT_BANK(2); /* Paranoia, we should always be in window 2 */
1231
1232 /* need MC_RESET to keep the memory consistent. errata? */
1233 if (smc->rx_ovrn) {
1234 outw(MC_RESET, ioaddr + MMU_CMD);
1235 smc->rx_ovrn = 0;
1236 }
1237
1238 /* Allocate the memory; send the packet now if we win. */
1239 outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
1240 for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
1241 ir = inw(ioaddr+INTERRUPT);
1242 if (ir & IM_ALLOC_INT) {
1243 /* Acknowledge the interrupt, send the packet. */
1244 outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
1245 smc_hardware_send_packet(dev); /* Send the packet now.. */
Komuro85e27832007-07-23 21:36:06 +09001246 spin_unlock_irqrestore(&smc->lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001247 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249 }
1250
1251 /* Otherwise defer until the Tx-space-allocated interrupt. */
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001252 netdev_dbg(dev, "memory allocation deferred.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
Komuro85e27832007-07-23 21:36:06 +09001254 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Patrick McHardy6ed10652009-06-23 06:03:08 +00001256 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259/*======================================================================
1260
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001261 Handle a Tx anomalous event. Entered while in Window 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263======================================================================*/
1264
1265static void smc_tx_err(struct net_device * dev)
1266{
1267 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001268 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
1270 int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
1271 int tx_status;
1272
1273 /* select this as the packet to read from */
1274 outw(packet_no, ioaddr + PNR_ARR);
1275
1276 /* read the first word from this packet */
1277 outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
1278
1279 tx_status = inw(ioaddr + DATA_1);
1280
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001281 dev->stats.tx_errors++;
1282 if (tx_status & TS_LOSTCAR) dev->stats.tx_carrier_errors++;
1283 if (tx_status & TS_LATCOL) dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (tx_status & TS_16COL) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001285 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 smc->tx_err++;
1287 }
1288
1289 if (tx_status & TS_SUCCESS) {
Joe Perches636b8112010-08-12 12:22:51 +00001290 netdev_notice(dev, "Successful packet caused error interrupt?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 /* re-enable transmit */
1293 SMC_SELECT_BANK(0);
1294 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1295 SMC_SELECT_BANK(2);
1296
1297 outw(MC_FREEPKT, ioaddr + MMU_CMD); /* Free the packet memory. */
1298
1299 /* one less packet waiting for me */
1300 smc->packets_waiting--;
1301
1302 outw(saved_packet, ioaddr + PNR_ARR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305/*====================================================================*/
1306
1307static void smc_eph_irq(struct net_device *dev)
1308{
1309 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001310 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 u_short card_stats, ephs;
1312
1313 SMC_SELECT_BANK(0);
1314 ephs = inw(ioaddr + EPH);
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001315 netdev_dbg(dev, "Ethernet protocol handler interrupt, status %4.4x.\n",
1316 ephs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* Could be a counter roll-over warning: update stats. */
1318 card_stats = inw(ioaddr + COUNTER);
1319 /* single collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001320 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 card_stats >>= 4;
1322 /* multiple collisions */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001323 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324#if 0 /* These are for when linux supports these statistics */
1325 card_stats >>= 4; /* deferred */
1326 card_stats >>= 4; /* excess deferred */
1327#endif
1328 /* If we had a transmit error we must re-enable the transmitter. */
1329 outw(inw(ioaddr + TCR) | TCR_ENABLE | smc->duplex, ioaddr + TCR);
1330
1331 /* Clear a link error interrupt. */
1332 SMC_SELECT_BANK(1);
1333 outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
1334 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1335 ioaddr + CONTROL);
1336 SMC_SELECT_BANK(2);
1337}
1338
1339/*====================================================================*/
1340
David Howells7d12e782006-10-05 14:55:46 +01001341static irqreturn_t smc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct net_device *dev = dev_id;
1344 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001345 unsigned int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 u_short saved_bank, saved_pointer, mask, status;
1347 unsigned int handled = 1;
1348 char bogus_cnt = INTR_WORK; /* Work we are willing to do. */
1349
1350 if (!netif_device_present(dev))
1351 return IRQ_NONE;
1352
1353 ioaddr = dev->base_addr;
1354
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001355 netdev_dbg(dev, "SMC91c92 interrupt %d at %#x.\n",
1356 irq, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Komuro85e27832007-07-23 21:36:06 +09001358 spin_lock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 smc->watchdog = 0;
1360 saved_bank = inw(ioaddr + BANK_SELECT);
1361 if ((saved_bank & 0xff00) != 0x3300) {
1362 /* The device does not exist -- the card could be off-line, or
1363 maybe it has been ejected. */
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001364 netdev_dbg(dev, "SMC91c92 interrupt %d for non-existent/ejected device.\n",
1365 irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 handled = 0;
1367 goto irq_done;
1368 }
1369
1370 SMC_SELECT_BANK(2);
1371 saved_pointer = inw(ioaddr + POINTER);
1372 mask = inw(ioaddr + INTERRUPT) >> 8;
1373 /* clear all interrupts */
1374 outw(0, ioaddr + INTERRUPT);
1375
1376 do { /* read the status flag, and mask it */
1377 status = inw(ioaddr + INTERRUPT) & 0xff;
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001378 netdev_dbg(dev, "Status is %#2.2x (mask %#2.2x).\n",
1379 status, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if ((status & mask) == 0) {
1381 if (bogus_cnt == INTR_WORK)
1382 handled = 0;
1383 break;
1384 }
1385 if (status & IM_RCV_INT) {
1386 /* Got a packet(s). */
1387 smc_rx(dev);
1388 }
1389 if (status & IM_TX_INT) {
1390 smc_tx_err(dev);
1391 outw(IM_TX_INT, ioaddr + INTERRUPT);
1392 }
1393 status &= mask;
1394 if (status & IM_TX_EMPTY_INT) {
1395 outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
1396 mask &= ~IM_TX_EMPTY_INT;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001397 dev->stats.tx_packets += smc->packets_waiting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 smc->packets_waiting = 0;
1399 }
1400 if (status & IM_ALLOC_INT) {
1401 /* Clear this interrupt so it doesn't happen again */
1402 mask &= ~IM_ALLOC_INT;
1403
1404 smc_hardware_send_packet(dev);
1405
1406 /* enable xmit interrupts based on this */
1407 mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
1408
1409 /* and let the card send more packets to me */
1410 netif_wake_queue(dev);
1411 }
1412 if (status & IM_RX_OVRN_INT) {
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001413 dev->stats.rx_errors++;
1414 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (smc->duplex)
1416 smc->rx_ovrn = 1; /* need MC_RESET outside smc_interrupt */
1417 outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
1418 }
1419 if (status & IM_EPH_INT)
1420 smc_eph_irq(dev);
1421 } while (--bogus_cnt);
1422
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001423 netdev_dbg(dev, " Restoring saved registers mask %2.2x bank %4.4x pointer %4.4x.\n",
1424 mask, saved_bank, saved_pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* restore state register */
1427 outw((mask<<8), ioaddr + INTERRUPT);
1428 outw(saved_pointer, ioaddr + POINTER);
1429 SMC_SELECT_BANK(saved_bank);
1430
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001431 netdev_dbg(dev, "Exiting interrupt IRQ%d.\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433irq_done:
1434
1435 if ((smc->manfid == MANFID_OSITECH) &&
1436 (smc->cardid != PRODID_OSITECH_SEVEN)) {
1437 /* Retrigger interrupt if needed */
1438 mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
1439 set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
1440 }
1441 if (smc->manfid == MANFID_MOTOROLA) {
1442 u_char cor;
1443 cor = readb(smc->base + MOT_UART + CISREG_COR);
1444 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
1445 writeb(cor, smc->base + MOT_UART + CISREG_COR);
1446 cor = readb(smc->base + MOT_LAN + CISREG_COR);
1447 writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
1448 writeb(cor, smc->base + MOT_LAN + CISREG_COR);
1449 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001450
1451 if ((smc->base != NULL) && /* Megahertz MFC's */
1452 (smc->manfid == MANFID_MEGAHERTZ) &&
1453 (smc->cardid == PRODID_MEGAHERTZ_EM3288)) {
1454
1455 u_char tmp;
1456 tmp = readb(smc->base+MEGAHERTZ_ISR);
1457 tmp = readb(smc->base+MEGAHERTZ_ISR);
1458
1459 /* Retrigger interrupt if needed */
1460 writeb(tmp, smc->base + MEGAHERTZ_ISR);
1461 writeb(tmp, smc->base + MEGAHERTZ_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Ken Kawasaki9735b7e2010-06-19 15:24:27 +00001463
Komuro85e27832007-07-23 21:36:06 +09001464 spin_unlock(&smc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return IRQ_RETVAL(handled);
1466}
1467
1468/*====================================================================*/
1469
1470static void smc_rx(struct net_device *dev)
1471{
Olof Johansson906da809c2008-02-04 22:27:35 -08001472 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int rx_status;
1474 int packet_length; /* Caution: not frame length, rather words
1475 to transfer from the chip. */
1476
1477 /* Assertion: we are in Window 2. */
1478
1479 if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
Joe Perches636b8112010-08-12 12:22:51 +00001480 netdev_err(dev, "smc_rx() with nothing on Rx FIFO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return;
1482 }
1483
1484 /* Reset the read pointer, and read the status and packet length. */
1485 outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
1486 rx_status = inw(ioaddr + DATA_1);
1487 packet_length = inw(ioaddr + DATA_1) & 0x07ff;
1488
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001489 netdev_dbg(dev, "Receive status %4.4x length %d.\n",
1490 rx_status, packet_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001492 if (!(rx_status & RS_ERRORS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /* do stuff to make a new packet */
1494 struct sk_buff *skb;
Tobias Klauser4a7c9722017-01-18 17:45:01 +01001495 struct smc_private *smc = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /* Note: packet_length adds 5 or 6 extra bytes here! */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +00001498 skb = netdev_alloc_skb(dev, packet_length+2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (skb == NULL) {
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001501 netdev_dbg(dev, "Low memory, packet dropped.\n");
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001502 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 outw(MC_RELEASE, ioaddr + MMU_CMD);
1504 return;
1505 }
1506
1507 packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
1508 skb_reserve(skb, 2);
1509 insw(ioaddr+DATA_1, skb_put(skb, packet_length),
1510 (packet_length+1)>>1);
1511 skb->protocol = eth_type_trans(skb, dev);
1512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 netif_rx(skb);
Tobias Klauser4a7c9722017-01-18 17:45:01 +01001514 smc->last_rx = jiffies;
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001515 dev->stats.rx_packets++;
1516 dev->stats.rx_bytes += packet_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (rx_status & RS_MULTICAST)
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001518 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 } else {
1520 /* error ... */
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001521 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001523 if (rx_status & RS_ALGNERR) dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
Stephen Hemminger6fb72982009-03-20 19:36:09 +00001525 dev->stats.rx_length_errors++;
1526 if (rx_status & RS_BADCRC) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 /* Let the MMU free the memory of this packet. */
1529 outw(MC_RELEASE, ioaddr + MMU_CMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/*======================================================================
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 Set the receive mode.
1535
1536 This routine is used by both the protocol level to notify us of
1537 promiscuous/multicast mode changes, and by the open/reset code to
1538 initialize the Rx registers. We always set the multicast list and
1539 leave the receiver running.
1540
1541======================================================================*/
1542
1543static void set_rx_mode(struct net_device *dev)
1544{
Olof Johansson906da809c2008-02-04 22:27:35 -08001545 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 struct smc_private *smc = netdev_priv(dev);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001547 unsigned char multicast_table[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 unsigned long flags;
1549 u_short rx_cfg_setting;
Ken Kawasakia6d37022010-04-10 12:50:14 +00001550 int i;
1551
1552 memset(multicast_table, 0, sizeof(multicast_table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 rx_cfg_setting = RxStripCRC | RxEnable | RxPromisc | RxAllMulti;
1556 } else if (dev->flags & IFF_ALLMULTI)
1557 rx_cfg_setting = RxStripCRC | RxEnable | RxAllMulti;
1558 else {
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001559 if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001560 struct netdev_hw_addr *ha;
Jiri Pirko91fea582010-02-19 08:48:47 +00001561
Jiri Pirko22bedad32010-04-01 21:22:57 +00001562 netdev_for_each_mc_addr(ha, dev) {
1563 u_int position = ether_crc(6, ha->addr);
Jiri Pirko91fea582010-02-19 08:48:47 +00001564 multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
1565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 rx_cfg_setting = RxStripCRC | RxEnable;
1568 }
1569
1570 /* Load MC table and Rx setting into the chip without interrupts. */
1571 spin_lock_irqsave(&smc->lock, flags);
1572 SMC_SELECT_BANK(3);
Ken Kawasakia6d37022010-04-10 12:50:14 +00001573 for (i = 0; i < 8; i++)
1574 outb(multicast_table[i], ioaddr + MULTICAST0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 SMC_SELECT_BANK(0);
1576 outw(rx_cfg_setting, ioaddr + RCR);
1577 SMC_SELECT_BANK(2);
1578 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
1581/*======================================================================
1582
1583 Senses when a card's config changes. Here, it's coax or TP.
1584
1585======================================================================*/
1586
1587static int s9k_config(struct net_device *dev, struct ifmap *map)
1588{
1589 struct smc_private *smc = netdev_priv(dev);
1590 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1591 if (smc->cfg & CFG_MII_SELECT)
1592 return -EOPNOTSUPP;
1593 else if (map->port > 2)
1594 return -EINVAL;
1595 dev->if_port = map->port;
Joe Perches636b8112010-08-12 12:22:51 +00001596 netdev_info(dev, "switched to %s port\n", if_names[dev->if_port]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 smc_reset(dev);
1598 }
1599 return 0;
1600}
1601
1602/*======================================================================
1603
1604 Reset the chip, reloading every register that might be corrupted.
1605
1606======================================================================*/
1607
1608/*
1609 Set transceiver type, perhaps to something other than what the user
1610 specified in dev->if_port.
1611*/
1612static void smc_set_xcvr(struct net_device *dev, int if_port)
1613{
1614 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001615 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 u_short saved_bank;
1617
1618 saved_bank = inw(ioaddr + BANK_SELECT);
1619 SMC_SELECT_BANK(1);
1620 if (if_port == 2) {
1621 outw(smc->cfg | CFG_AUI_SELECT, ioaddr + CONFIG);
1622 if ((smc->manfid == MANFID_OSITECH) &&
1623 (smc->cardid != PRODID_OSITECH_SEVEN))
1624 set_bits(OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1625 smc->media_status = ((dev->if_port == 0) ? 0x0001 : 0x0002);
1626 } else {
1627 outw(smc->cfg, ioaddr + CONFIG);
1628 if ((smc->manfid == MANFID_OSITECH) &&
1629 (smc->cardid != PRODID_OSITECH_SEVEN))
1630 mask_bits(~OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1631 smc->media_status = ((dev->if_port == 0) ? 0x0012 : 0x4001);
1632 }
1633 SMC_SELECT_BANK(saved_bank);
1634}
1635
1636static void smc_reset(struct net_device *dev)
1637{
Olof Johansson906da809c2008-02-04 22:27:35 -08001638 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct smc_private *smc = netdev_priv(dev);
1640 int i;
1641
Ben Boeckelc501b1f2013-11-01 08:53:35 -04001642 netdev_dbg(dev, "smc91c92 reset called.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /* The first interaction must be a write to bring the chip out
1645 of sleep mode. */
1646 SMC_SELECT_BANK(0);
1647 /* Reset the chip. */
1648 outw(RCR_SOFTRESET, ioaddr + RCR);
1649 udelay(10);
1650
1651 /* Clear the transmit and receive configuration registers. */
1652 outw(RCR_CLEAR, ioaddr + RCR);
1653 outw(TCR_CLEAR, ioaddr + TCR);
1654
1655 /* Set the Window 1 control, configuration and station addr registers.
1656 No point in writing the I/O base register ;-> */
1657 SMC_SELECT_BANK(1);
Andreas Mohrd6e05ed2006-06-26 18:35:02 +02001658 /* Automatically release successfully transmitted packets,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 Accept link errors, counter and Tx error interrupts. */
1660 outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1661 ioaddr + CONTROL);
1662 smc_set_xcvr(dev, dev->if_port);
1663 if ((smc->manfid == MANFID_OSITECH) &&
1664 (smc->cardid != PRODID_OSITECH_SEVEN))
1665 outw((dev->if_port == 2 ? OSI_AUI_PWR : 0) |
1666 (inw(ioaddr-0x10+OSITECH_AUI_PWR) & 0xff00),
1667 ioaddr - 0x10 + OSITECH_AUI_PWR);
1668
1669 /* Fill in the physical address. The databook is wrong about the order! */
1670 for (i = 0; i < 6; i += 2)
1671 outw((dev->dev_addr[i+1]<<8)+dev->dev_addr[i],
1672 ioaddr + ADDR0 + i);
1673
1674 /* Reset the MMU */
1675 SMC_SELECT_BANK(2);
1676 outw(MC_RESET, ioaddr + MMU_CMD);
1677 outw(0, ioaddr + INTERRUPT);
1678
1679 /* Re-enable the chip. */
1680 SMC_SELECT_BANK(0);
1681 outw(((smc->cfg & CFG_MII_SELECT) ? 0 : TCR_MONCSN) |
1682 TCR_ENABLE | TCR_PAD_EN | smc->duplex, ioaddr + TCR);
1683 set_rx_mode(dev);
1684
1685 if (smc->cfg & CFG_MII_SELECT) {
1686 SMC_SELECT_BANK(3);
1687
1688 /* Reset MII */
1689 mdio_write(dev, smc->mii_if.phy_id, 0, 0x8000);
1690
1691 /* Advertise 100F, 100H, 10F, 10H */
1692 mdio_write(dev, smc->mii_if.phy_id, 4, 0x01e1);
1693
1694 /* Restart MII autonegotiation */
1695 mdio_write(dev, smc->mii_if.phy_id, 0, 0x0000);
1696 mdio_write(dev, smc->mii_if.phy_id, 0, 0x1200);
1697 }
1698
1699 /* Enable interrupts. */
1700 SMC_SELECT_BANK(2);
1701 outw((IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT) << 8,
1702 ioaddr + INTERRUPT);
1703}
1704
1705/*======================================================================
1706
1707 Media selection timer routine
1708
1709======================================================================*/
1710
1711static void media_check(u_long arg)
1712{
1713 struct net_device *dev = (struct net_device *) arg;
1714 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001715 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 u_short i, media, saved_bank;
1717 u_short link;
Komuro85e27832007-07-23 21:36:06 +09001718 unsigned long flags;
1719
1720 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 saved_bank = inw(ioaddr + BANK_SELECT);
1723
1724 if (!netif_device_present(dev))
1725 goto reschedule;
1726
1727 SMC_SELECT_BANK(2);
1728
1729 /* need MC_RESET to keep the memory consistent. errata? */
1730 if (smc->rx_ovrn) {
1731 outw(MC_RESET, ioaddr + MMU_CMD);
1732 smc->rx_ovrn = 0;
1733 }
1734 i = inw(ioaddr + INTERRUPT);
1735 SMC_SELECT_BANK(0);
1736 media = inw(ioaddr + EPH) & EPH_LINK_OK;
1737 SMC_SELECT_BANK(1);
1738 media |= (inw(ioaddr + CONFIG) & CFG_AUI_SELECT) ? 2 : 1;
1739
Ken Kawasaki2a915152010-04-24 10:37:09 +00001740 SMC_SELECT_BANK(saved_bank);
1741 spin_unlock_irqrestore(&smc->lock, flags);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /* Check for pending interrupt with watchdog flag set: with
1744 this, we can limp along even if the interrupt is blocked */
1745 if (smc->watchdog++ && ((i>>8) & i)) {
1746 if (!smc->fast_poll)
Joe Perches636b8112010-08-12 12:22:51 +00001747 netdev_info(dev, "interrupt(s) dropped!\n");
Ken Kawasaki2a915152010-04-24 10:37:09 +00001748 local_irq_save(flags);
Komuroe363d1382007-02-10 11:57:35 +09001749 smc_interrupt(dev->irq, dev);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001750 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 smc->fast_poll = HZ;
1752 }
1753 if (smc->fast_poll) {
1754 smc->fast_poll--;
1755 smc->media.expires = jiffies + HZ/100;
1756 add_timer(&smc->media);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return;
1758 }
1759
Ken Kawasaki2a915152010-04-24 10:37:09 +00001760 spin_lock_irqsave(&smc->lock, flags);
1761
1762 saved_bank = inw(ioaddr + BANK_SELECT);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (smc->cfg & CFG_MII_SELECT) {
1765 if (smc->mii_if.phy_id < 0)
1766 goto reschedule;
1767
1768 SMC_SELECT_BANK(3);
1769 link = mdio_read(dev, smc->mii_if.phy_id, 1);
1770 if (!link || (link == 0xffff)) {
Joe Perches636b8112010-08-12 12:22:51 +00001771 netdev_info(dev, "MII is missing!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 smc->mii_if.phy_id = -1;
1773 goto reschedule;
1774 }
1775
1776 link &= 0x0004;
1777 if (link != smc->link_status) {
1778 u_short p = mdio_read(dev, smc->mii_if.phy_id, 5);
Joe Perches636b8112010-08-12 12:22:51 +00001779 netdev_info(dev, "%s link beat\n", link ? "found" : "lost");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 smc->duplex = (((p & 0x0100) || ((p & 0x1c0) == 0x40))
1781 ? TCR_FDUPLX : 0);
1782 if (link) {
Joe Perches636b8112010-08-12 12:22:51 +00001783 netdev_info(dev, "autonegotiation complete: "
1784 "%dbaseT-%cD selected\n",
1785 (p & 0x0180) ? 100 : 10, smc->duplex ? 'F' : 'H');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 SMC_SELECT_BANK(0);
1788 outw(inw(ioaddr + TCR) | smc->duplex, ioaddr + TCR);
1789 smc->link_status = link;
1790 }
1791 goto reschedule;
1792 }
1793
1794 /* Ignore collisions unless we've had no rx's recently */
Tobias Klauser4a7c9722017-01-18 17:45:01 +01001795 if (time_after(jiffies, smc->last_rx + HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (smc->tx_err || (smc->media_status & EPH_16COL))
1797 media |= EPH_16COL;
1798 }
1799 smc->tx_err = 0;
1800
1801 if (media != smc->media_status) {
1802 if ((media & smc->media_status & 1) &&
1803 ((smc->media_status ^ media) & EPH_LINK_OK))
Joe Perches636b8112010-08-12 12:22:51 +00001804 netdev_info(dev, "%s link beat\n",
1805 smc->media_status & EPH_LINK_OK ? "lost" : "found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 else if ((media & smc->media_status & 2) &&
1807 ((smc->media_status ^ media) & EPH_16COL))
Joe Perches636b8112010-08-12 12:22:51 +00001808 netdev_info(dev, "coax cable %s\n",
1809 media & EPH_16COL ? "problem" : "ok");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (dev->if_port == 0) {
1811 if (media & 1) {
1812 if (media & EPH_LINK_OK)
Joe Perches636b8112010-08-12 12:22:51 +00001813 netdev_info(dev, "flipped to 10baseT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 else
1815 smc_set_xcvr(dev, 2);
1816 } else {
1817 if (media & EPH_16COL)
1818 smc_set_xcvr(dev, 1);
1819 else
Joe Perches636b8112010-08-12 12:22:51 +00001820 netdev_info(dev, "flipped to 10base2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 }
1823 smc->media_status = media;
1824 }
1825
1826reschedule:
1827 smc->media.expires = jiffies + HZ;
1828 add_timer(&smc->media);
1829 SMC_SELECT_BANK(saved_bank);
Komuro85e27832007-07-23 21:36:06 +09001830 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833static int smc_link_ok(struct net_device *dev)
1834{
Olof Johansson906da809c2008-02-04 22:27:35 -08001835 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct smc_private *smc = netdev_priv(dev);
1837
1838 if (smc->cfg & CFG_MII_SELECT) {
1839 return mii_link_ok(&smc->mii_if);
1840 } else {
1841 SMC_SELECT_BANK(0);
1842 return inw(ioaddr + EPH) & EPH_LINK_OK;
1843 }
1844}
1845
1846static int smc_netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1847{
1848 u16 tmp;
Olof Johansson906da809c2008-02-04 22:27:35 -08001849 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 ecmd->supported = (SUPPORTED_TP | SUPPORTED_AUI |
1852 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full);
1853
1854 SMC_SELECT_BANK(1);
1855 tmp = inw(ioaddr + CONFIG);
1856 ecmd->port = (tmp & CFG_AUI_SELECT) ? PORT_AUI : PORT_TP;
1857 ecmd->transceiver = XCVR_INTERNAL;
David Decotigny70739492011-04-27 18:32:40 +00001858 ethtool_cmd_speed_set(ecmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 ecmd->phy_address = ioaddr + MGMT;
1860
1861 SMC_SELECT_BANK(0);
1862 tmp = inw(ioaddr + TCR);
1863 ecmd->duplex = (tmp & TCR_FDUPLX) ? DUPLEX_FULL : DUPLEX_HALF;
1864
1865 return 0;
1866}
1867
1868static int smc_netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1869{
1870 u16 tmp;
Olof Johansson906da809c2008-02-04 22:27:35 -08001871 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
David Decotigny25db0332011-04-27 18:32:39 +00001873 if (ethtool_cmd_speed(ecmd) != SPEED_10)
1874 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1876 return -EINVAL;
1877 if (ecmd->port != PORT_TP && ecmd->port != PORT_AUI)
1878 return -EINVAL;
1879 if (ecmd->transceiver != XCVR_INTERNAL)
1880 return -EINVAL;
1881
1882 if (ecmd->port == PORT_AUI)
1883 smc_set_xcvr(dev, 1);
1884 else
1885 smc_set_xcvr(dev, 0);
1886
1887 SMC_SELECT_BANK(0);
1888 tmp = inw(ioaddr + TCR);
1889 if (ecmd->duplex == DUPLEX_FULL)
1890 tmp |= TCR_FDUPLX;
1891 else
1892 tmp &= ~TCR_FDUPLX;
1893 outw(tmp, ioaddr + TCR);
1894
1895 return 0;
1896}
1897
1898static int check_if_running(struct net_device *dev)
1899{
1900 if (!netif_running(dev))
1901 return -EINVAL;
1902 return 0;
1903}
1904
1905static void smc_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1906{
Rick Jones33a5ba12011-11-15 14:59:53 +00001907 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1908 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static int smc_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1912{
1913 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001914 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1916 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001917 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Ken Kawasaki2a915152010-04-24 10:37:09 +00001919 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001920 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (smc->cfg & CFG_MII_SELECT)
1922 ret = mii_ethtool_gset(&smc->mii_if, ecmd);
1923 else
1924 ret = smc_netdev_get_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001926 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return ret;
1928}
1929
1930static int smc_set_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
1931{
1932 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001933 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1935 int ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001936 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Ken Kawasaki2a915152010-04-24 10:37:09 +00001938 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001939 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (smc->cfg & CFG_MII_SELECT)
1941 ret = mii_ethtool_sset(&smc->mii_if, ecmd);
1942 else
1943 ret = smc_netdev_set_ecmd(dev, ecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001945 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return ret;
1947}
1948
1949static u32 smc_get_link(struct net_device *dev)
1950{
1951 struct smc_private *smc = netdev_priv(dev);
Olof Johansson906da809c2008-02-04 22:27:35 -08001952 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1954 u32 ret;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001955 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Ken Kawasaki2a915152010-04-24 10:37:09 +00001957 spin_lock_irqsave(&smc->lock, flags);
Komuro85e27832007-07-23 21:36:06 +09001958 SMC_SELECT_BANK(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 ret = smc_link_ok(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00001961 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return ret;
1963}
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965static int smc_nway_reset(struct net_device *dev)
1966{
1967 struct smc_private *smc = netdev_priv(dev);
1968 if (smc->cfg & CFG_MII_SELECT) {
Olof Johansson906da809c2008-02-04 22:27:35 -08001969 unsigned int ioaddr = dev->base_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 u16 saved_bank = inw(ioaddr + BANK_SELECT);
1971 int res;
1972
1973 SMC_SELECT_BANK(3);
1974 res = mii_nway_restart(&smc->mii_if);
1975 SMC_SELECT_BANK(saved_bank);
1976
1977 return res;
1978 } else
1979 return -EOPNOTSUPP;
1980}
1981
Jeff Garzik7282d492006-09-13 14:30:00 -04001982static const struct ethtool_ops ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 .begin = check_if_running,
1984 .get_drvinfo = smc_get_drvinfo,
1985 .get_settings = smc_get_settings,
1986 .set_settings = smc_set_settings,
1987 .get_link = smc_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 .nway_reset = smc_nway_reset,
1989};
1990
1991static int smc_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1992{
1993 struct smc_private *smc = netdev_priv(dev);
1994 struct mii_ioctl_data *mii = if_mii(rq);
1995 int rc = 0;
1996 u16 saved_bank;
Olof Johansson906da809c2008-02-04 22:27:35 -08001997 unsigned int ioaddr = dev->base_addr;
Ken Kawasaki2a915152010-04-24 10:37:09 +00001998 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 if (!netif_running(dev))
2001 return -EINVAL;
2002
Ken Kawasaki2a915152010-04-24 10:37:09 +00002003 spin_lock_irqsave(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 saved_bank = inw(ioaddr + BANK_SELECT);
2005 SMC_SELECT_BANK(3);
2006 rc = generic_mii_ioctl(&smc->mii_if, mii, cmd, NULL);
2007 SMC_SELECT_BANK(saved_bank);
Ken Kawasaki2a915152010-04-24 10:37:09 +00002008 spin_unlock_irqrestore(&smc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return rc;
2010}
2011
Joe Perches25f8f542011-05-03 19:29:01 -07002012static const struct pcmcia_device_id smc91c92_ids[] = {
Dominik Brodowski5c672222005-06-27 16:28:27 -07002013 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0109, 0x0501),
2014 PCMCIA_PFC_DEVICE_MANF_CARD(0, 0x0140, 0x000a),
2015 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3288", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x04cd2988, 0x46a52d63),
2016 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "CC/XJEM3336", "DATA/FAX/CELL ETHERNET MODEM", 0xf510db04, 0x0143b773, 0x46a52d63),
2017 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "EM1144T", "PCMCIA MODEM", 0xf510db04, 0x856d66c8, 0xbd6c43ef),
2018 PCMCIA_PFC_DEVICE_PROD_ID123(0, "MEGAHERTZ", "XJEM1144/CCEM1144", "PCMCIA MODEM", 0xf510db04, 0x52d21e1e, 0xbd6c43ef),
2019 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Gateway 2000", "XJEM3336", 0xdd9989be, 0x662c394c),
2020 PCMCIA_PFC_DEVICE_PROD_ID12(0, "MEGAHERTZ", "XJEM1144/CCEM1144", 0xf510db04, 0x52d21e1e),
Komurod277ad02005-07-28 01:07:24 -07002021 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Diamonds Modem+Ethernet", 0xc2f80cd, 0x656947b9),
2022 PCMCIA_PFC_DEVICE_PROD_ID12(0, "Ositech", "Trumpcard:Jack of Hearts Modem+Ethernet", 0xc2f80cd, 0xdc9ba5ed),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002023 PCMCIA_MFC_DEVICE_MANF_CARD(0, 0x016c, 0x0020),
2024 PCMCIA_DEVICE_MANF_CARD(0x016c, 0x0023),
2025 PCMCIA_DEVICE_PROD_ID123("BASICS by New Media Corporation", "Ethernet", "SMC91C94", 0x23c78a9d, 0x00b2e941, 0xcef397fb),
2026 PCMCIA_DEVICE_PROD_ID12("ARGOSY", "Fast Ethernet PCCard", 0x78f308dc, 0xdcea68bc),
2027 PCMCIA_DEVICE_PROD_ID12("dit Co., Ltd.", "PC Card-10/100BTX", 0xe59365c8, 0x6a2161d1),
2028 PCMCIA_DEVICE_PROD_ID12("DYNALINK", "L100C", 0x6a26d1cf, 0xc16ce9c5),
2029 PCMCIA_DEVICE_PROD_ID12("Farallon", "Farallon Enet", 0x58d93fc4, 0x244734e9),
2030 PCMCIA_DEVICE_PROD_ID12("Megahertz", "CC10BT/2", 0x33234748, 0x3c95b953),
2031 PCMCIA_DEVICE_PROD_ID12("MELCO/SMC", "LPC-TX", 0xa2cd8e6d, 0x42da662a),
Komurod277ad02005-07-28 01:07:24 -07002032 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Four of Diamonds Ethernet", 0xc2f80cd, 0xb3466314),
2033 PCMCIA_DEVICE_PROD_ID12("Ositech", "Trumpcard:Seven of Diamonds Ethernet", 0xc2f80cd, 0x194b650a),
Dominik Brodowski5c672222005-06-27 16:28:27 -07002034 PCMCIA_DEVICE_PROD_ID12("PCMCIA", "Fast Ethernet PCCard", 0x281f1c5d, 0xdcea68bc),
2035 PCMCIA_DEVICE_PROD_ID12("Psion", "10Mb Ethernet", 0x4ef00b21, 0x844be9e9),
2036 PCMCIA_DEVICE_PROD_ID12("SMC", "EtherEZ Ethernet 8020", 0xc4f8b18b, 0x4a0eeb2d),
2037 /* These conflict with other cards! */
2038 /* PCMCIA_DEVICE_MANF_CARD(0x0186, 0x0100), */
2039 /* PCMCIA_DEVICE_MANF_CARD(0x8a01, 0xc1ab), */
2040 PCMCIA_DEVICE_NULL,
2041};
2042MODULE_DEVICE_TABLE(pcmcia, smc91c92_ids);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044static struct pcmcia_driver smc91c92_cs_driver = {
2045 .owner = THIS_MODULE,
Dominik Brodowski2e9b9812010-08-08 11:36:26 +02002046 .name = "smc91c92_cs",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +02002047 .probe = smc91c92_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +01002048 .remove = smc91c92_detach,
Dominik Brodowski5c672222005-06-27 16:28:27 -07002049 .id_table = smc91c92_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +01002050 .suspend = smc91c92_suspend,
2051 .resume = smc91c92_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052};
H Hartley Sweetenfdd3f292013-03-06 11:27:43 -07002053module_pcmcia_driver(smc91c92_cs_driver);