blob: 0c9b5d94154f8d215ddaf40d3fb252764983d73b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * smc91x.c
3 * This is a driver for SMSC's 91C9x/91C1xx single-chip Ethernet devices.
4 *
5 * Copyright (C) 1996 by Erik Stahlman
6 * Copyright (C) 2001 Standard Microsystems Corporation
7 * Developed by Simple Network Magic Corporation
8 * Copyright (C) 2003 Monta Vista Software, Inc.
9 * Unified SMC91x driver by Nicolas Pitre
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * Arguments:
26 * io = for the base address
27 * irq = for the IRQ
28 * nowait = 0 for normal wait states, 1 eliminates additional wait states
29 *
30 * original author:
31 * Erik Stahlman <erik@vt.edu>
32 *
33 * hardware multicast code:
34 * Peter Cammaert <pc@denkart.be>
35 *
36 * contributors:
37 * Daris A Nevil <dnevil@snmc.com>
Nicolas Pitre2f82af02009-09-14 03:25:28 -040038 * Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * Russell King <rmk@arm.linux.org.uk>
40 *
41 * History:
42 * 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
43 * 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
44 * 03/16/01 Daris A Nevil modified smc9194.c for use with LAN91C111
45 * 08/22/01 Scott Anderson merge changes from smc9194 to smc91111
46 * 08/21/01 Pramod B Bhardwaj added support for RevB of LAN91C111
47 * 12/20/01 Jeff Sutherland initial port to Xscale PXA with DMA support
48 * 04/07/03 Nicolas Pitre unified SMC91x driver, killed irq races,
49 * more bus abstraction, big cleanup, etc.
50 * 29/09/03 Russell King - add driver model support
51 * - ethtool support
52 * - convert to use generic MII interface
53 * - add link up/down notification
54 * - don't try to handle full negotiation in
55 * smc_phy_configure
56 * - clean up (and fix stack overrun) in PHY
57 * MII read/write functions
58 * 22/09/04 Nicolas Pitre big update (see commit log for details)
59 */
60static const char version[] =
Ben Boeckel6389aa42013-11-01 08:53:31 -040061 "smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@fluxnic.net>";
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Debugging level */
64#ifndef SMC_DEBUG
65#define SMC_DEBUG 0
66#endif
67
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/init.h>
70#include <linux/module.h>
71#include <linux/kernel.h>
72#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/delay.h>
74#include <linux/interrupt.h>
David Howells476c32c2010-10-07 14:08:54 +010075#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/errno.h>
77#include <linux/ioport.h>
78#include <linux/crc32.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010079#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/spinlock.h>
81#include <linux/ethtool.h>
82#include <linux/mii.h>
83#include <linux/workqueue.h>
Thomas Chou682a1692011-01-25 19:22:05 +000084#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <linux/netdevice.h>
87#include <linux/etherdevice.h>
88#include <linux/skbuff.h>
89
90#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include "smc91x.h"
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#ifndef SMC_NOWAIT
95# define SMC_NOWAIT 0
96#endif
97static int nowait = SMC_NOWAIT;
98module_param(nowait, int, 0400);
99MODULE_PARM_DESC(nowait, "set to 1 for no wait state");
100
101/*
102 * Transmit timeout, default 5 seconds.
103 */
Nicolas Pitreea937562005-04-12 16:26:40 -0400104static int watchdog = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105module_param(watchdog, int, 0400);
106MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
107
108MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -0700109MODULE_ALIAS("platform:smc91x");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * The internal workings of the driver. If you are changing anything
113 * here with the SMC stuff, you should have the datasheet and know
114 * what you are doing.
115 */
116#define CARDNAME "smc91x"
117
118/*
119 * Use power-down feature of the chip
120 */
121#define POWER_DOWN 1
122
123/*
124 * Wait time for memory to be free. This probably shouldn't be
125 * tuned that much, as waiting for this means nothing else happens
126 * in the system
127 */
128#define MEMORY_WAIT_TIME 16
129
130/*
Nicolas Pitre5d0571d2005-11-17 14:02:48 -0500131 * The maximum number of processing loops allowed for each call to the
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400132 * IRQ handler.
Nicolas Pitre5d0571d2005-11-17 14:02:48 -0500133 */
134#define MAX_IRQ_LOOPS 8
135
136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * This selects whether TX packets are sent one by one to the SMC91x internal
138 * memory and throttled until transmission completes. This may prevent
139 * RX overruns a litle by keeping much of the memory free for RX packets
140 * but to the expense of reduced TX throughput and increased IRQ overhead.
141 * Note this is not a cure for a too slow data bus or too high IRQ latency.
142 */
143#define THROTTLE_TX_PKTS 0
144
145/*
146 * The MII clock high/low times. 2x this number gives the MII clock period
147 * in microseconds. (was 50, but this gives 6.4ms for each MII transaction!)
148 */
149#define MII_DELAY 1
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#if SMC_DEBUG > 0
Ben Boeckel6389aa42013-11-01 08:53:31 -0400152#define DBG(n, dev, args...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 do { \
154 if (SMC_DEBUG >= (n)) \
Ben Boeckel6389aa42013-11-01 08:53:31 -0400155 netdev_dbg(dev, args); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 } while (0)
157
Ben Boeckel6389aa42013-11-01 08:53:31 -0400158#define PRINTK(dev, args...) netdev_info(dev, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#else
Ben Boeckel6389aa42013-11-01 08:53:31 -0400160#define DBG(n, dev, args...) do { } while (0)
161#define PRINTK(dev, args...) netdev_dbg(dev, args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#endif
163
164#if SMC_DEBUG > 3
165static void PRINT_PKT(u_char *buf, int length)
166{
167 int i;
168 int remainder;
169 int lines;
170
171 lines = length / 16;
172 remainder = length % 16;
173
174 for (i = 0; i < lines ; i ++) {
175 int cur;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400176 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 for (cur = 0; cur < 8; cur++) {
178 u_char a, b;
179 a = *buf++;
180 b = *buf++;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400181 pr_cont("%02x%02x ", a, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400183 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400185 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 for (i = 0; i < remainder/2 ; i++) {
187 u_char a, b;
188 a = *buf++;
189 b = *buf++;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400190 pr_cont("%02x%02x ", a, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400192 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194#else
Ben Boeckel6389aa42013-11-01 08:53:31 -0400195#define PRINT_PKT(x...) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#endif
197
198
199/* this enables an interrupt in the interrupt mask register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900200#define SMC_ENABLE_INT(lp, x) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned char mask; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700202 unsigned long smc_enable_flags; \
203 spin_lock_irqsave(&lp->lock, smc_enable_flags); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900204 mask = SMC_GET_INT_MASK(lp); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mask |= (x); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900206 SMC_SET_INT_MASK(lp, mask); \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700207 spin_unlock_irqrestore(&lp->lock, smc_enable_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208} while (0)
209
210/* this disables an interrupt from the interrupt mask register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900211#define SMC_DISABLE_INT(lp, x) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 unsigned char mask; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700213 unsigned long smc_disable_flags; \
214 spin_lock_irqsave(&lp->lock, smc_disable_flags); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900215 mask = SMC_GET_INT_MASK(lp); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 mask &= ~(x); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900217 SMC_SET_INT_MASK(lp, mask); \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700218 spin_unlock_irqrestore(&lp->lock, smc_disable_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219} while (0)
220
221/*
222 * Wait while MMU is busy. This is usually in the order of a few nanosecs
223 * if at all, but let's avoid deadlocking the system if the hardware
224 * decides to go south.
225 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900226#define SMC_WAIT_MMU_BUSY(lp) do { \
227 if (unlikely(SMC_GET_MMU_CMD(lp) & MC_BUSY)) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 unsigned long timeout = jiffies + 2; \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900229 while (SMC_GET_MMU_CMD(lp) & MC_BUSY) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (time_after(jiffies, timeout)) { \
Ben Boeckel6389aa42013-11-01 08:53:31 -0400231 netdev_dbg(dev, "timeout %s line %d\n", \
232 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break; \
234 } \
235 cpu_relax(); \
236 } \
237 } \
238} while (0)
239
240
241/*
242 * this does a soft reset on the device
243 */
244static void smc_reset(struct net_device *dev)
245{
246 struct smc_local *lp = netdev_priv(dev);
247 void __iomem *ioaddr = lp->base;
248 unsigned int ctl, cfg;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400249 struct sk_buff *pending_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Ben Boeckel6389aa42013-11-01 08:53:31 -0400251 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Nicolas Pitrebe836682005-06-19 23:56:21 -0400253 /* Disable all interrupts, block TX tasklet */
Russell King76cb4fe2006-08-14 23:00:20 -0700254 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900255 SMC_SELECT_BANK(lp, 2);
256 SMC_SET_INT_MASK(lp, 0);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400257 pending_skb = lp->pending_tx_skb;
258 lp->pending_tx_skb = NULL;
Russell King76cb4fe2006-08-14 23:00:20 -0700259 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Nicolas Pitrebe836682005-06-19 23:56:21 -0400261 /* free any pending tx skb */
262 if (pending_skb) {
263 dev_kfree_skb(pending_skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700264 dev->stats.tx_errors++;
265 dev->stats.tx_aborted_errors++;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /*
269 * This resets the registers mostly to defaults, but doesn't
270 * affect EEPROM. That seems unnecessary
271 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900272 SMC_SELECT_BANK(lp, 0);
273 SMC_SET_RCR(lp, RCR_SOFTRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /*
276 * Setup the Configuration Register
277 * This is necessary because the CONFIG_REG is not affected
278 * by a soft reset
279 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900280 SMC_SELECT_BANK(lp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 cfg = CONFIG_DEFAULT;
283
284 /*
285 * Setup for fast accesses if requested. If the card/system
286 * can't handle it then there will be no recovery except for
287 * a hard reset or power cycle
288 */
Eric Miaoc4f0e762008-06-19 17:39:03 +0800289 if (lp->cfg.flags & SMC91X_NOWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 cfg |= CONFIG_NO_WAIT;
291
292 /*
293 * Release from possible power-down state
294 * Configuration register is not affected by Soft Reset
295 */
296 cfg |= CONFIG_EPH_POWER_EN;
297
Magnus Dammcfdfa862008-02-22 19:55:05 +0900298 SMC_SET_CONFIG(lp, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* this should pause enough for the chip to be happy */
301 /*
302 * elaborate? What does the chip _need_? --jgarzik
303 *
304 * This seems to be undocumented, but something the original
305 * driver(s) have always done. Suspect undocumented timing
306 * info/determined empirically. --rmk
307 */
308 udelay(1);
309
310 /* Disable transmit and receive functionality */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900311 SMC_SELECT_BANK(lp, 0);
312 SMC_SET_RCR(lp, RCR_CLEAR);
313 SMC_SET_TCR(lp, TCR_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Magnus Dammcfdfa862008-02-22 19:55:05 +0900315 SMC_SELECT_BANK(lp, 1);
316 ctl = SMC_GET_CTL(lp) | CTL_LE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /*
319 * Set the control register to automatically release successfully
320 * transmitted packets, to make the best use out of our limited
321 * memory
322 */
323 if(!THROTTLE_TX_PKTS)
324 ctl |= CTL_AUTO_RELEASE;
325 else
326 ctl &= ~CTL_AUTO_RELEASE;
Magnus Dammcfdfa862008-02-22 19:55:05 +0900327 SMC_SET_CTL(lp, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* Reset the MMU */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900330 SMC_SELECT_BANK(lp, 2);
331 SMC_SET_MMU_CMD(lp, MC_RESET);
332 SMC_WAIT_MMU_BUSY(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
336 * Enable Interrupts, Receive, and Transmit
337 */
338static void smc_enable(struct net_device *dev)
339{
340 struct smc_local *lp = netdev_priv(dev);
341 void __iomem *ioaddr = lp->base;
342 int mask;
343
Ben Boeckel6389aa42013-11-01 08:53:31 -0400344 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* see the header file for options in TCR/RCR DEFAULT */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900347 SMC_SELECT_BANK(lp, 0);
348 SMC_SET_TCR(lp, lp->tcr_cur_mode);
349 SMC_SET_RCR(lp, lp->rcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Magnus Dammcfdfa862008-02-22 19:55:05 +0900351 SMC_SELECT_BANK(lp, 1);
352 SMC_SET_MAC_ADDR(lp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* now, enable interrupts */
355 mask = IM_EPH_INT|IM_RX_OVRN_INT|IM_RCV_INT;
356 if (lp->version >= (CHIP_91100 << 4))
357 mask |= IM_MDINT;
Magnus Dammcfdfa862008-02-22 19:55:05 +0900358 SMC_SELECT_BANK(lp, 2);
359 SMC_SET_INT_MASK(lp, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /*
362 * From this point the register bank must _NOT_ be switched away
363 * to something else than bank 2 without proper locking against
364 * races with any tasklet or interrupt handlers until smc_shutdown()
365 * or smc_reset() is called.
366 */
367}
368
369/*
370 * this puts the device in an inactive state
371 */
372static void smc_shutdown(struct net_device *dev)
373{
374 struct smc_local *lp = netdev_priv(dev);
375 void __iomem *ioaddr = lp->base;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400376 struct sk_buff *pending_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Ben Boeckel6389aa42013-11-01 08:53:31 -0400378 DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* no more interrupts for me */
Russell King76cb4fe2006-08-14 23:00:20 -0700381 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900382 SMC_SELECT_BANK(lp, 2);
383 SMC_SET_INT_MASK(lp, 0);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400384 pending_skb = lp->pending_tx_skb;
385 lp->pending_tx_skb = NULL;
Russell King76cb4fe2006-08-14 23:00:20 -0700386 spin_unlock_irq(&lp->lock);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400387 if (pending_skb)
388 dev_kfree_skb(pending_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* and tell the card to stay away from that nasty outside world */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900391 SMC_SELECT_BANK(lp, 0);
392 SMC_SET_RCR(lp, RCR_CLEAR);
393 SMC_SET_TCR(lp, TCR_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395#ifdef POWER_DOWN
396 /* finally, shut the chip down */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900397 SMC_SELECT_BANK(lp, 1);
398 SMC_SET_CONFIG(lp, SMC_GET_CONFIG(lp) & ~CONFIG_EPH_POWER_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#endif
400}
401
402/*
403 * This is the procedure to handle the receipt of a packet.
404 */
405static inline void smc_rcv(struct net_device *dev)
406{
407 struct smc_local *lp = netdev_priv(dev);
408 void __iomem *ioaddr = lp->base;
409 unsigned int packet_number, status, packet_len;
410
Ben Boeckel6389aa42013-11-01 08:53:31 -0400411 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Magnus Dammcfdfa862008-02-22 19:55:05 +0900413 packet_number = SMC_GET_RXFIFO(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (unlikely(packet_number & RXFIFO_REMPTY)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400415 PRINTK(dev, "smc_rcv with nothing on FIFO.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return;
417 }
418
419 /* read from start of packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900420 SMC_SET_PTR(lp, PTR_READ | PTR_RCV | PTR_AUTOINC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 /* First two words are status and packet length */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900423 SMC_GET_PKT_HDR(lp, status, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 packet_len &= 0x07ff; /* mask off top bits */
Ben Boeckel6389aa42013-11-01 08:53:31 -0400425 DBG(2, dev, "RX PNR 0x%x STATUS 0x%04x LENGTH 0x%04x (%d)\n",
426 packet_number, status, packet_len, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 back:
429 if (unlikely(packet_len < 6 || status & RS_ERRORS)) {
430 if (status & RS_TOOLONG && packet_len <= (1514 + 4 + 6)) {
431 /* accept VLAN packets */
432 status &= ~RS_TOOLONG;
433 goto back;
434 }
435 if (packet_len < 6) {
436 /* bloody hardware */
Ben Boeckel6389aa42013-11-01 08:53:31 -0400437 netdev_err(dev, "fubar (rxlen %u status %x\n",
438 packet_len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 status |= RS_TOOSHORT;
440 }
Magnus Dammcfdfa862008-02-22 19:55:05 +0900441 SMC_WAIT_MMU_BUSY(lp);
442 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700443 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (status & RS_ALGNERR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700445 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (status & (RS_TOOSHORT | RS_TOOLONG))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700447 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (status & RS_BADCRC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700449 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 } else {
451 struct sk_buff *skb;
452 unsigned char *data;
453 unsigned int data_len;
454
455 /* set multicast stats */
456 if (status & RS_MULTICAST)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700457 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /*
460 * Actual payload is packet_len - 6 (or 5 if odd byte).
461 * We want skb_reserve(2) and the final ctrl word
462 * (2 bytes, possibly containing the payload odd byte).
463 * Furthermore, we add 2 bytes to allow rounding up to
464 * multiple of 4 bytes on 32 bit buses.
465 * Hence packet_len - 6 + 2 + 2 + 2.
466 */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000467 skb = netdev_alloc_skb(dev, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (unlikely(skb == NULL)) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900469 SMC_WAIT_MMU_BUSY(lp);
470 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700471 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return;
473 }
474
475 /* Align IP header to 32 bits */
476 skb_reserve(skb, 2);
477
478 /* BUG: the LAN91C111 rev A never sets this bit. Force it. */
479 if (lp->version == 0x90)
480 status |= RS_ODDFRAME;
481
482 /*
483 * If odd length: packet_len - 5,
484 * otherwise packet_len - 6.
485 * With the trailing ctrl byte it's packet_len - 4.
486 */
487 data_len = packet_len - ((status & RS_ODDFRAME) ? 5 : 6);
488 data = skb_put(skb, data_len);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900489 SMC_PULL_DATA(lp, data, packet_len - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Magnus Dammcfdfa862008-02-22 19:55:05 +0900491 SMC_WAIT_MMU_BUSY(lp);
492 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 PRINT_PKT(data, packet_len - 4);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 skb->protocol = eth_type_trans(skb, dev);
497 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700498 dev->stats.rx_packets++;
499 dev->stats.rx_bytes += data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501}
502
503#ifdef CONFIG_SMP
504/*
505 * On SMP we have the following problem:
506 *
507 * A = smc_hardware_send_pkt()
508 * B = smc_hard_start_xmit()
509 * C = smc_interrupt()
510 *
511 * A and B can never be executed simultaneously. However, at least on UP,
512 * it is possible (and even desirable) for C to interrupt execution of
513 * A or B in order to have better RX reliability and avoid overruns.
514 * C, just like A and B, must have exclusive access to the chip and
515 * each of them must lock against any other concurrent access.
516 * Unfortunately this is not possible to have C suspend execution of A or
517 * B taking place on another CPU. On UP this is no an issue since A and B
518 * are run from softirq context and C from hard IRQ context, and there is
519 * no other CPU where concurrent access can happen.
520 * If ever there is a way to force at least B and C to always be executed
521 * on the same CPU then we could use read/write locks to protect against
522 * any other concurrent access and C would always interrupt B. But life
523 * isn't that easy in a SMP world...
524 */
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700525#define smc_special_trylock(lock, flags) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526({ \
527 int __ret; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700528 local_irq_save(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 __ret = spin_trylock(lock); \
530 if (!__ret) \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700531 local_irq_restore(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 __ret; \
533})
Alexey Dobriyan0b4f2922009-08-26 12:03:35 -0700534#define smc_special_lock(lock, flags) spin_lock_irqsave(lock, flags)
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700535#define smc_special_unlock(lock, flags) spin_unlock_irqrestore(lock, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#else
Mike Frysingerfd0775b2009-12-09 03:40:04 +0000537#define smc_special_trylock(lock, flags) (flags == flags)
538#define smc_special_lock(lock, flags) do { flags = 0; } while (0)
539#define smc_special_unlock(lock, flags) do { flags = 0; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540#endif
541
542/*
543 * This is called to actually send a packet to the chip.
544 */
545static void smc_hardware_send_pkt(unsigned long data)
546{
547 struct net_device *dev = (struct net_device *)data;
548 struct smc_local *lp = netdev_priv(dev);
549 void __iomem *ioaddr = lp->base;
550 struct sk_buff *skb;
551 unsigned int packet_no, len;
552 unsigned char *buf;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700553 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Ben Boeckel6389aa42013-11-01 08:53:31 -0400555 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700557 if (!smc_special_trylock(&lp->lock, flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 netif_stop_queue(dev);
559 tasklet_schedule(&lp->tx_task);
560 return;
561 }
562
563 skb = lp->pending_tx_skb;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400564 if (unlikely(!skb)) {
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700565 smc_special_unlock(&lp->lock, flags);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400566 return;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 lp->pending_tx_skb = NULL;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400569
Magnus Dammcfdfa862008-02-22 19:55:05 +0900570 packet_no = SMC_GET_AR(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (unlikely(packet_no & AR_FAILED)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400572 netdev_err(dev, "Memory allocation failed.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700573 dev->stats.tx_errors++;
574 dev->stats.tx_fifo_errors++;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700575 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto done;
577 }
578
579 /* point to the beginning of the packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900580 SMC_SET_PN(lp, packet_no);
581 SMC_SET_PTR(lp, PTR_AUTOINC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 buf = skb->data;
584 len = skb->len;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400585 DBG(2, dev, "TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",
586 packet_no, len, len, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 PRINT_PKT(buf, len);
588
589 /*
590 * Send the packet length (+6 for status words, length, and ctl.
591 * The card will pad to 64 bytes with zeroes if packet is too small.
592 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900593 SMC_PUT_PKT_HDR(lp, 0, len + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* send the actual data */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900596 SMC_PUSH_DATA(lp, buf, len & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Send final ctl word with the last byte if there is one */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900599 SMC_outw(((len & 1) ? (0x2000 | buf[len-1]) : 0), ioaddr, DATA_REG(lp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /*
Nicolas Pitreea937562005-04-12 16:26:40 -0400602 * If THROTTLE_TX_PKTS is set, we stop the queue here. This will
603 * have the effect of having at most one packet queued for TX
604 * in the chip's memory at all time.
605 *
606 * If THROTTLE_TX_PKTS is not set then the queue is stopped only
607 * when memory allocation (MC_ALLOC) does not succeed right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
Nicolas Pitreea937562005-04-12 16:26:40 -0400609 if (THROTTLE_TX_PKTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 netif_stop_queue(dev);
611
612 /* queue the packet for TX */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900613 SMC_SET_MMU_CMD(lp, MC_ENQUEUE);
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700614 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700617 dev->stats.tx_packets++;
618 dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Magnus Dammcfdfa862008-02-22 19:55:05 +0900620 SMC_ENABLE_INT(lp, IM_TX_INT | IM_TX_EMPTY_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622done: if (!THROTTLE_TX_PKTS)
623 netif_wake_queue(dev);
624
625 dev_kfree_skb(skb);
626}
627
628/*
629 * Since I am not sure if I will have enough room in the chip's ram
630 * to store the packet, I call this routine which either sends it
631 * now, or set the card to generates an interrupt when ready
632 * for the packet.
633 */
634static int smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
635{
636 struct smc_local *lp = netdev_priv(dev);
637 void __iomem *ioaddr = lp->base;
638 unsigned int numPages, poll_count, status;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700639 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Ben Boeckel6389aa42013-11-01 08:53:31 -0400641 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 BUG_ON(lp->pending_tx_skb != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /*
646 * The MMU wants the number of pages to be the number of 256 bytes
647 * 'pages', minus 1 (since a packet can't ever have 0 pages :))
648 *
649 * The 91C111 ignores the size bits, but earlier models don't.
650 *
651 * Pkt size for allocating is data length +6 (for additional status
652 * words, length and ctl)
653 *
654 * If odd size then last byte is included in ctl word.
655 */
656 numPages = ((skb->len & ~1) + (6 - 1)) >> 8;
657 if (unlikely(numPages > 7)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400658 netdev_warn(dev, "Far too big packet error.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700659 dev->stats.tx_errors++;
660 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000662 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700665 smc_special_lock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* now, try to allocate the memory */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900668 SMC_SET_MMU_CMD(lp, MC_ALLOC | numPages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
671 * Poll the chip for a short amount of time in case the
672 * allocation succeeds quickly.
673 */
674 poll_count = MEMORY_WAIT_TIME;
675 do {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900676 status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (status & IM_ALLOC_INT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900678 SMC_ACK_INT(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 }
681 } while (--poll_count);
682
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700683 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Nicolas Pitrebe836682005-06-19 23:56:21 -0400685 lp->pending_tx_skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!poll_count) {
687 /* oh well, wait until the chip finds memory later */
688 netif_stop_queue(dev);
Ben Boeckel6389aa42013-11-01 08:53:31 -0400689 DBG(2, dev, "TX memory allocation deferred.\n");
Magnus Dammcfdfa862008-02-22 19:55:05 +0900690 SMC_ENABLE_INT(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 } else {
692 /*
693 * Allocation succeeded: push packet to the chip's own memory
694 * immediately.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400695 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 smc_hardware_send_pkt((unsigned long)dev);
697 }
698
Patrick McHardy6ed10652009-06-23 06:03:08 +0000699 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/*
703 * This handles a TX interrupt, which is only called when:
704 * - a TX error occurred, or
705 * - CTL_AUTO_RELEASE is not set and TX of a packet completed.
706 */
707static void smc_tx(struct net_device *dev)
708{
709 struct smc_local *lp = netdev_priv(dev);
710 void __iomem *ioaddr = lp->base;
711 unsigned int saved_packet, packet_no, tx_status, pkt_len;
712
Ben Boeckel6389aa42013-11-01 08:53:31 -0400713 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* If the TX FIFO is empty then nothing to do */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900716 packet_no = SMC_GET_TXFIFO(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (unlikely(packet_no & TXFIFO_TEMPTY)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400718 PRINTK(dev, "smc_tx with nothing on FIFO.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return;
720 }
721
722 /* select packet to read from */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900723 saved_packet = SMC_GET_PN(lp);
724 SMC_SET_PN(lp, packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* read the first word (status word) from this packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900727 SMC_SET_PTR(lp, PTR_AUTOINC | PTR_READ);
728 SMC_GET_PKT_HDR(lp, tx_status, pkt_len);
Ben Boeckel6389aa42013-11-01 08:53:31 -0400729 DBG(2, dev, "TX STATUS 0x%04x PNR 0x%02x\n",
730 tx_status, packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Nicolas Pitre8de90112005-04-12 16:21:11 -0400732 if (!(tx_status & ES_TX_SUC))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700733 dev->stats.tx_errors++;
Nicolas Pitre8de90112005-04-12 16:21:11 -0400734
735 if (tx_status & ES_LOSTCARR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700736 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Nicolas Pitre8de90112005-04-12 16:21:11 -0400738 if (tx_status & (ES_LATCOL | ES_16COL)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400739 PRINTK(dev, "%s occurred on last xmit\n",
Nicolas Pitre8de90112005-04-12 16:21:11 -0400740 (tx_status & ES_LATCOL) ?
741 "late collision" : "too many collisions");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700742 dev->stats.tx_window_errors++;
743 if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400744 netdev_info(dev, "unexpectedly large number of bad collisions. Please check duplex setting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
747
748 /* kill the packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900749 SMC_WAIT_MMU_BUSY(lp);
750 SMC_SET_MMU_CMD(lp, MC_FREEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Don't restore Packet Number Reg until busy bit is cleared */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900753 SMC_WAIT_MMU_BUSY(lp);
754 SMC_SET_PN(lp, saved_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* re-enable transmit */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900757 SMC_SELECT_BANK(lp, 0);
758 SMC_SET_TCR(lp, lp->tcr_cur_mode);
759 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762
763/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
764
765static void smc_mii_out(struct net_device *dev, unsigned int val, int bits)
766{
767 struct smc_local *lp = netdev_priv(dev);
768 void __iomem *ioaddr = lp->base;
769 unsigned int mii_reg, mask;
770
Magnus Dammcfdfa862008-02-22 19:55:05 +0900771 mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 mii_reg |= MII_MDOE;
773
774 for (mask = 1 << (bits - 1); mask; mask >>= 1) {
775 if (val & mask)
776 mii_reg |= MII_MDO;
777 else
778 mii_reg &= ~MII_MDO;
779
Magnus Dammcfdfa862008-02-22 19:55:05 +0900780 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 udelay(MII_DELAY);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900782 SMC_SET_MII(lp, mii_reg | MII_MCLK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 udelay(MII_DELAY);
784 }
785}
786
787static unsigned int smc_mii_in(struct net_device *dev, int bits)
788{
789 struct smc_local *lp = netdev_priv(dev);
790 void __iomem *ioaddr = lp->base;
791 unsigned int mii_reg, mask, val;
792
Magnus Dammcfdfa862008-02-22 19:55:05 +0900793 mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
794 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900797 if (SMC_GET_MII(lp) & MII_MDI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 val |= mask;
799
Magnus Dammcfdfa862008-02-22 19:55:05 +0900800 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 udelay(MII_DELAY);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900802 SMC_SET_MII(lp, mii_reg | MII_MCLK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 udelay(MII_DELAY);
804 }
805
806 return val;
807}
808
809/*
810 * Reads a register from the MII Management serial interface
811 */
812static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg)
813{
814 struct smc_local *lp = netdev_priv(dev);
815 void __iomem *ioaddr = lp->base;
816 unsigned int phydata;
817
Magnus Dammcfdfa862008-02-22 19:55:05 +0900818 SMC_SELECT_BANK(lp, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /* Idle - 32 ones */
821 smc_mii_out(dev, 0xffffffff, 32);
822
823 /* Start code (01) + read (10) + phyaddr + phyreg */
824 smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);
825
826 /* Turnaround (2bits) + phydata */
827 phydata = smc_mii_in(dev, 18);
828
829 /* Return to idle state */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900830 SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Ben Boeckel6389aa42013-11-01 08:53:31 -0400832 DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
833 __func__, phyaddr, phyreg, phydata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Magnus Dammcfdfa862008-02-22 19:55:05 +0900835 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return phydata;
837}
838
839/*
840 * Writes a register to the MII Management serial interface
841 */
842static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,
843 int phydata)
844{
845 struct smc_local *lp = netdev_priv(dev);
846 void __iomem *ioaddr = lp->base;
847
Magnus Dammcfdfa862008-02-22 19:55:05 +0900848 SMC_SELECT_BANK(lp, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /* Idle - 32 ones */
851 smc_mii_out(dev, 0xffffffff, 32);
852
853 /* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */
854 smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);
855
856 /* Return to idle state */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900857 SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Ben Boeckel6389aa42013-11-01 08:53:31 -0400859 DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
860 __func__, phyaddr, phyreg, phydata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Magnus Dammcfdfa862008-02-22 19:55:05 +0900862 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865/*
866 * Finds and reports the PHY address
867 */
868static void smc_phy_detect(struct net_device *dev)
869{
870 struct smc_local *lp = netdev_priv(dev);
871 int phyaddr;
872
Ben Boeckel6389aa42013-11-01 08:53:31 -0400873 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 lp->phy_type = 0;
876
877 /*
878 * Scan all 32 PHY addresses if necessary, starting at
879 * PHY#1 to PHY#31, and then PHY#0 last.
880 */
881 for (phyaddr = 1; phyaddr < 33; ++phyaddr) {
882 unsigned int id1, id2;
883
884 /* Read the PHY identifiers */
885 id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);
886 id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);
887
Ben Boeckel6389aa42013-11-01 08:53:31 -0400888 DBG(3, dev, "phy_id1=0x%x, phy_id2=0x%x\n",
889 id1, id2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /* Make sure it is a valid identifier */
892 if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&
893 id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {
894 /* Save the PHY's address */
895 lp->mii.phy_id = phyaddr & 31;
896 lp->phy_type = id1 << 16 | id2;
897 break;
898 }
899 }
900}
901
902/*
903 * Sets the PHY to a configuration as determined by the user
904 */
905static int smc_phy_fixed(struct net_device *dev)
906{
907 struct smc_local *lp = netdev_priv(dev);
908 void __iomem *ioaddr = lp->base;
909 int phyaddr = lp->mii.phy_id;
910 int bmcr, cfg1;
911
Ben Boeckel6389aa42013-11-01 08:53:31 -0400912 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Enter Link Disable state */
915 cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);
916 cfg1 |= PHY_CFG1_LNKDIS;
917 smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);
918
919 /*
920 * Set our fixed capabilities
921 * Disable auto-negotiation
922 */
923 bmcr = 0;
924
925 if (lp->ctl_rfduplx)
926 bmcr |= BMCR_FULLDPLX;
927
928 if (lp->ctl_rspeed == 100)
929 bmcr |= BMCR_SPEED100;
930
931 /* Write our capabilities to the phy control register */
932 smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);
933
934 /* Re-Configure the Receive/Phy Control register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900935 SMC_SELECT_BANK(lp, 0);
936 SMC_SET_RPC(lp, lp->rpc_cur_mode);
937 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 return 1;
940}
941
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000942/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * smc_phy_reset - reset the phy
944 * @dev: net device
945 * @phy: phy address
946 *
947 * Issue a software reset for the specified PHY and
948 * wait up to 100ms for the reset to complete. We should
949 * not access the PHY for 50ms after issuing the reset.
950 *
951 * The time to wait appears to be dependent on the PHY.
952 *
953 * Must be called with lp->lock locked.
954 */
955static int smc_phy_reset(struct net_device *dev, int phy)
956{
957 struct smc_local *lp = netdev_priv(dev);
958 unsigned int bmcr;
959 int timeout;
960
961 smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);
962
963 for (timeout = 2; timeout; timeout--) {
964 spin_unlock_irq(&lp->lock);
965 msleep(50);
966 spin_lock_irq(&lp->lock);
967
968 bmcr = smc_phy_read(dev, phy, MII_BMCR);
969 if (!(bmcr & BMCR_RESET))
970 break;
971 }
972
973 return bmcr & BMCR_RESET;
974}
975
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000976/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 * smc_phy_powerdown - powerdown phy
978 * @dev: net device
979 *
980 * Power down the specified PHY
981 */
982static void smc_phy_powerdown(struct net_device *dev)
983{
984 struct smc_local *lp = netdev_priv(dev);
985 unsigned int bmcr;
986 int phy = lp->mii.phy_id;
987
988 if (lp->phy_type == 0)
989 return;
990
991 /* We need to ensure that no calls to smc_phy_configure are
992 pending.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 */
David S. Miller4bb073c2008-06-12 02:22:02 -0700994 cancel_work_sync(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 bmcr = smc_phy_read(dev, phy, MII_BMCR);
997 smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);
998}
999
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001000/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * smc_phy_check_media - check the media status and adjust TCR
1002 * @dev: net device
1003 * @init: set true for initialisation
1004 *
1005 * Select duplex mode depending on negotiation state. This
1006 * also updates our carrier state.
1007 */
1008static void smc_phy_check_media(struct net_device *dev, int init)
1009{
1010 struct smc_local *lp = netdev_priv(dev);
1011 void __iomem *ioaddr = lp->base;
1012
1013 if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
1014 /* duplex state has changed */
1015 if (lp->mii.full_duplex) {
1016 lp->tcr_cur_mode |= TCR_SWFDUP;
1017 } else {
1018 lp->tcr_cur_mode &= ~TCR_SWFDUP;
1019 }
1020
Magnus Dammcfdfa862008-02-22 19:55:05 +09001021 SMC_SELECT_BANK(lp, 0);
1022 SMC_SET_TCR(lp, lp->tcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024}
1025
1026/*
1027 * Configures the specified PHY through the MII management interface
1028 * using Autonegotiation.
1029 * Calls smc_phy_fixed() if the user has requested a certain config.
1030 * If RPC ANEG bit is set, the media selection is dependent purely on
1031 * the selection by the MII (either in the MII BMCR reg or the result
1032 * of autonegotiation.) If the RPC ANEG bit is cleared, the selection
1033 * is controlled by the RPC SPEED and RPC DPLX bits.
1034 */
David Howells6d5aefb2006-12-05 19:36:26 +00001035static void smc_phy_configure(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
David Howells6d5aefb2006-12-05 19:36:26 +00001037 struct smc_local *lp =
1038 container_of(work, struct smc_local, phy_configure);
1039 struct net_device *dev = lp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 void __iomem *ioaddr = lp->base;
1041 int phyaddr = lp->mii.phy_id;
1042 int my_phy_caps; /* My PHY capabilities */
1043 int my_ad_caps; /* My Advertised capabilities */
1044 int status;
1045
Ben Boeckel6389aa42013-11-01 08:53:31 -04001046 DBG(3, dev, "smc_program_phy()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 spin_lock_irq(&lp->lock);
1049
1050 /*
1051 * We should not be called if phy_type is zero.
1052 */
1053 if (lp->phy_type == 0)
1054 goto smc_phy_configure_exit;
1055
1056 if (smc_phy_reset(dev, phyaddr)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001057 netdev_info(dev, "PHY reset timed out\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto smc_phy_configure_exit;
1059 }
1060
1061 /*
1062 * Enable PHY Interrupts (for register 18)
1063 * Interrupts listed here are disabled
1064 */
1065 smc_phy_write(dev, phyaddr, PHY_MASK_REG,
1066 PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
1067 PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
1068 PHY_INT_SPDDET | PHY_INT_DPLXDET);
1069
1070 /* Configure the Receive/Phy Control register */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001071 SMC_SELECT_BANK(lp, 0);
1072 SMC_SET_RPC(lp, lp->rpc_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /* If the user requested no auto neg, then go set his request */
1075 if (lp->mii.force_media) {
1076 smc_phy_fixed(dev);
1077 goto smc_phy_configure_exit;
1078 }
1079
1080 /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
1081 my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);
1082
1083 if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001084 netdev_info(dev, "Auto negotiation NOT supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 smc_phy_fixed(dev);
1086 goto smc_phy_configure_exit;
1087 }
1088
1089 my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */
1090
1091 if (my_phy_caps & BMSR_100BASE4)
1092 my_ad_caps |= ADVERTISE_100BASE4;
1093 if (my_phy_caps & BMSR_100FULL)
1094 my_ad_caps |= ADVERTISE_100FULL;
1095 if (my_phy_caps & BMSR_100HALF)
1096 my_ad_caps |= ADVERTISE_100HALF;
1097 if (my_phy_caps & BMSR_10FULL)
1098 my_ad_caps |= ADVERTISE_10FULL;
1099 if (my_phy_caps & BMSR_10HALF)
1100 my_ad_caps |= ADVERTISE_10HALF;
1101
1102 /* Disable capabilities not selected by our user */
1103 if (lp->ctl_rspeed != 100)
1104 my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
1105
1106 if (!lp->ctl_rfduplx)
1107 my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
1108
1109 /* Update our Auto-Neg Advertisement Register */
1110 smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);
1111 lp->mii.advertising = my_ad_caps;
1112
1113 /*
1114 * Read the register back. Without this, it appears that when
1115 * auto-negotiation is restarted, sometimes it isn't ready and
1116 * the link does not come up.
1117 */
1118 status = smc_phy_read(dev, phyaddr, MII_ADVERTISE);
1119
Ben Boeckel6389aa42013-11-01 08:53:31 -04001120 DBG(2, dev, "phy caps=%x\n", my_phy_caps);
1121 DBG(2, dev, "phy advertised caps=%x\n", my_ad_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /* Restart auto-negotiation process in order to advertise my caps */
1124 smc_phy_write(dev, phyaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
1125
1126 smc_phy_check_media(dev, 1);
1127
1128smc_phy_configure_exit:
Magnus Dammcfdfa862008-02-22 19:55:05 +09001129 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/*
1134 * smc_phy_interrupt
1135 *
1136 * Purpose: Handle interrupts relating to PHY register 18. This is
1137 * called from the "hard" interrupt handler under our private spinlock.
1138 */
1139static void smc_phy_interrupt(struct net_device *dev)
1140{
1141 struct smc_local *lp = netdev_priv(dev);
1142 int phyaddr = lp->mii.phy_id;
1143 int phy18;
1144
Ben Boeckel6389aa42013-11-01 08:53:31 -04001145 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 if (lp->phy_type == 0)
1148 return;
1149
1150 for(;;) {
1151 smc_phy_check_media(dev, 0);
1152
1153 /* Read PHY Register 18, Status Output */
1154 phy18 = smc_phy_read(dev, phyaddr, PHY_INT_REG);
1155 if ((phy18 & PHY_INT_INT) == 0)
1156 break;
1157 }
1158}
1159
1160/*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
1161
1162static void smc_10bt_check_media(struct net_device *dev, int init)
1163{
1164 struct smc_local *lp = netdev_priv(dev);
1165 void __iomem *ioaddr = lp->base;
1166 unsigned int old_carrier, new_carrier;
1167
1168 old_carrier = netif_carrier_ok(dev) ? 1 : 0;
1169
Magnus Dammcfdfa862008-02-22 19:55:05 +09001170 SMC_SELECT_BANK(lp, 0);
1171 new_carrier = (SMC_GET_EPH_STATUS(lp) & ES_LINK_OK) ? 1 : 0;
1172 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 if (init || (old_carrier != new_carrier)) {
1175 if (!new_carrier) {
1176 netif_carrier_off(dev);
1177 } else {
1178 netif_carrier_on(dev);
1179 }
1180 if (netif_msg_link(lp))
Ben Boeckel6389aa42013-11-01 08:53:31 -04001181 netdev_info(dev, "link %s\n",
1182 new_carrier ? "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184}
1185
1186static void smc_eph_interrupt(struct net_device *dev)
1187{
1188 struct smc_local *lp = netdev_priv(dev);
1189 void __iomem *ioaddr = lp->base;
1190 unsigned int ctl;
1191
1192 smc_10bt_check_media(dev, 0);
1193
Magnus Dammcfdfa862008-02-22 19:55:05 +09001194 SMC_SELECT_BANK(lp, 1);
1195 ctl = SMC_GET_CTL(lp);
1196 SMC_SET_CTL(lp, ctl & ~CTL_LE_ENABLE);
1197 SMC_SET_CTL(lp, ctl);
1198 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/*
1202 * This is the main routine of the driver, to handle the device when
1203 * it needs some attention.
1204 */
David Howells7d12e782006-10-05 14:55:46 +01001205static irqreturn_t smc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 struct net_device *dev = dev_id;
1208 struct smc_local *lp = netdev_priv(dev);
1209 void __iomem *ioaddr = lp->base;
1210 int status, mask, timeout, card_stats;
1211 int saved_pointer;
1212
Ben Boeckel6389aa42013-11-01 08:53:31 -04001213 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 spin_lock(&lp->lock);
1216
1217 /* A preamble may be used when there is a potential race
1218 * between the interruptible transmit functions and this
1219 * ISR. */
1220 SMC_INTERRUPT_PREAMBLE;
1221
Magnus Dammcfdfa862008-02-22 19:55:05 +09001222 saved_pointer = SMC_GET_PTR(lp);
1223 mask = SMC_GET_INT_MASK(lp);
1224 SMC_SET_INT_MASK(lp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* set a timeout value, so I don't stay here forever */
Nicolas Pitre5d0571d2005-11-17 14:02:48 -05001227 timeout = MAX_IRQ_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 do {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001230 status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Ben Boeckel6389aa42013-11-01 08:53:31 -04001232 DBG(2, dev, "INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x\n",
1233 status, mask,
1234 ({ int meminfo; SMC_SELECT_BANK(lp, 0);
1235 meminfo = SMC_GET_MIR(lp);
1236 SMC_SELECT_BANK(lp, 2); meminfo; }),
1237 SMC_GET_FIFO(lp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 status &= mask;
1240 if (!status)
1241 break;
1242
Nicolas Pitreea937562005-04-12 16:26:40 -04001243 if (status & IM_TX_INT) {
1244 /* do this before RX as it will free memory quickly */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001245 DBG(3, dev, "TX int\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 smc_tx(dev);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001247 SMC_ACK_INT(lp, IM_TX_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (THROTTLE_TX_PKTS)
1249 netif_wake_queue(dev);
Nicolas Pitreea937562005-04-12 16:26:40 -04001250 } else if (status & IM_RCV_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001251 DBG(3, dev, "RX irq\n");
Nicolas Pitreea937562005-04-12 16:26:40 -04001252 smc_rcv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 } else if (status & IM_ALLOC_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001254 DBG(3, dev, "Allocation irq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 tasklet_hi_schedule(&lp->tx_task);
1256 mask &= ~IM_ALLOC_INT;
1257 } else if (status & IM_TX_EMPTY_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001258 DBG(3, dev, "TX empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 mask &= ~IM_TX_EMPTY_INT;
1260
1261 /* update stats */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001262 SMC_SELECT_BANK(lp, 0);
1263 card_stats = SMC_GET_COUNTER(lp);
1264 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* single collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001267 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 card_stats >>= 4;
1269
1270 /* multiple collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001271 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } else if (status & IM_RX_OVRN_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001273 DBG(1, dev, "RX overrun (EPH_ST 0x%04x)\n",
1274 ({ int eph_st; SMC_SELECT_BANK(lp, 0);
1275 eph_st = SMC_GET_EPH_STATUS(lp);
1276 SMC_SELECT_BANK(lp, 2); eph_st; }));
Magnus Dammcfdfa862008-02-22 19:55:05 +09001277 SMC_ACK_INT(lp, IM_RX_OVRN_INT);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001278 dev->stats.rx_errors++;
1279 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else if (status & IM_EPH_INT) {
1281 smc_eph_interrupt(dev);
1282 } else if (status & IM_MDINT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001283 SMC_ACK_INT(lp, IM_MDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 smc_phy_interrupt(dev);
1285 } else if (status & IM_ERCV_INT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001286 SMC_ACK_INT(lp, IM_ERCV_INT);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001287 PRINTK(dev, "UNSUPPORTED: ERCV INTERRUPT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 } while (--timeout);
1290
1291 /* restore register states */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001292 SMC_SET_PTR(lp, saved_pointer);
1293 SMC_SET_INT_MASK(lp, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_unlock(&lp->lock);
1295
Sonic Zhangc500cb22008-04-10 21:30:37 -07001296#ifndef CONFIG_NET_POLL_CONTROLLER
Nicolas Pitre5d0571d2005-11-17 14:02:48 -05001297 if (timeout == MAX_IRQ_LOOPS)
Ben Boeckel6389aa42013-11-01 08:53:31 -04001298 PRINTK(dev, "spurious interrupt (mask = 0x%02x)\n",
1299 mask);
Sonic Zhangc500cb22008-04-10 21:30:37 -07001300#endif
Ben Boeckel6389aa42013-11-01 08:53:31 -04001301 DBG(3, dev, "Interrupt done (%d loops)\n",
1302 MAX_IRQ_LOOPS - timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * We return IRQ_HANDLED unconditionally here even if there was
1306 * nothing to do. There is a possibility that a packet might
1307 * get enqueued into the chip right after TX_EMPTY_INT is raised
1308 * but just before the CPU acknowledges the IRQ.
1309 * Better take an unneeded IRQ in some occasions than complexifying
1310 * the code for all cases.
1311 */
1312 return IRQ_HANDLED;
1313}
1314
1315#ifdef CONFIG_NET_POLL_CONTROLLER
1316/*
1317 * Polling receive - used by netconsole and other diagnostic tools
1318 * to allow network i/o with interrupts disabled.
1319 */
1320static void smc_poll_controller(struct net_device *dev)
1321{
1322 disable_irq(dev->irq);
Al Viro9c8e7f52006-10-07 16:29:18 +01001323 smc_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 enable_irq(dev->irq);
1325}
1326#endif
1327
1328/* Our watchdog timed out. Called by the networking layer */
1329static void smc_timeout(struct net_device *dev)
1330{
1331 struct smc_local *lp = netdev_priv(dev);
1332 void __iomem *ioaddr = lp->base;
Nicolas Pitre8de90112005-04-12 16:21:11 -04001333 int status, mask, eph_st, meminfo, fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Ben Boeckel6389aa42013-11-01 08:53:31 -04001335 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001338 status = SMC_GET_INT(lp);
1339 mask = SMC_GET_INT_MASK(lp);
1340 fifo = SMC_GET_FIFO(lp);
1341 SMC_SELECT_BANK(lp, 0);
1342 eph_st = SMC_GET_EPH_STATUS(lp);
1343 meminfo = SMC_GET_MIR(lp);
1344 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 spin_unlock_irq(&lp->lock);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001346 PRINTK(dev, "TX timeout (INT 0x%02x INTMASK 0x%02x MEM 0x%04x FIFO 0x%04x EPH_ST 0x%04x)\n",
1347 status, mask, meminfo, fifo, eph_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 smc_reset(dev);
1350 smc_enable(dev);
1351
1352 /*
1353 * Reconfiguring the PHY doesn't seem like a bad idea here, but
1354 * smc_phy_configure() calls msleep() which calls schedule_timeout()
1355 * which calls schedule(). Hence we use a work queue.
1356 */
David S. Miller4bb073c2008-06-12 02:22:02 -07001357 if (lp->phy_type != 0)
1358 schedule_work(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001361 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 netif_wake_queue(dev);
1363}
1364
1365/*
1366 * This routine will, depending on the values passed to it,
1367 * either make it accept multicast packets, go into
1368 * promiscuous mode (for TCPDUMP and cousins) or accept
1369 * a select set of multicast packets
1370 */
1371static void smc_set_multicast_list(struct net_device *dev)
1372{
1373 struct smc_local *lp = netdev_priv(dev);
1374 void __iomem *ioaddr = lp->base;
1375 unsigned char multicast_table[8];
1376 int update_multicast = 0;
1377
Ben Boeckel6389aa42013-11-01 08:53:31 -04001378 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 if (dev->flags & IFF_PROMISC) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001381 DBG(2, dev, "RCR_PRMS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 lp->rcr_cur_mode |= RCR_PRMS;
1383 }
1384
1385/* BUG? I never disable promiscuous mode if multicasting was turned on.
1386 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1387 when promiscuous mode is turned on.
1388*/
1389
1390 /*
1391 * Here, I am setting this to accept all multicast packets.
1392 * I don't need to zero the multicast table, because the flag is
1393 * checked before the table is
1394 */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001395 else if (dev->flags & IFF_ALLMULTI || netdev_mc_count(dev) > 16) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001396 DBG(2, dev, "RCR_ALMUL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 lp->rcr_cur_mode |= RCR_ALMUL;
1398 }
1399
1400 /*
1401 * This sets the internal hardware table to filter out unwanted
1402 * multicast packets before they take up memory.
1403 *
1404 * The SMC chip uses a hash table where the high 6 bits of the CRC of
1405 * address are the offset into the table. If that bit is 1, then the
1406 * multicast packet is accepted. Otherwise, it's dropped silently.
1407 *
1408 * To use the 6 bits as an offset into the table, the high 3 bits are
1409 * the number of the 8 bit register, while the low 3 bits are the bit
1410 * within that register.
1411 */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001412 else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001413 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /* table for flipping the order of 3 bits */
1416 static const unsigned char invert3[] = {0, 4, 2, 6, 1, 5, 3, 7};
1417
1418 /* start with a table of all zeros: reject all */
1419 memset(multicast_table, 0, sizeof(multicast_table));
1420
Jiri Pirko22bedad32010-04-01 21:22:57 +00001421 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int position;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* only use the low order bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001425 position = crc32_le(~0, ha->addr, 6) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 /* do some messy swapping to put the bit in the right spot */
1428 multicast_table[invert3[position&7]] |=
1429 (1<<invert3[(position>>3)&7]);
1430 }
1431
1432 /* be sure I get rid of flags I might have set */
1433 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1434
1435 /* now, the table can be loaded into the chipset */
1436 update_multicast = 1;
1437 } else {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001438 DBG(2, dev, "~(RCR_PRMS|RCR_ALMUL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1440
1441 /*
1442 * since I'm disabling all multicast entirely, I need to
1443 * clear the multicast list
1444 */
1445 memset(multicast_table, 0, sizeof(multicast_table));
1446 update_multicast = 1;
1447 }
1448
1449 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001450 SMC_SELECT_BANK(lp, 0);
1451 SMC_SET_RCR(lp, lp->rcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (update_multicast) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001453 SMC_SELECT_BANK(lp, 3);
1454 SMC_SET_MCAST(lp, multicast_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Magnus Dammcfdfa862008-02-22 19:55:05 +09001456 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 spin_unlock_irq(&lp->lock);
1458}
1459
1460
1461/*
1462 * Open and Initialize the board
1463 *
1464 * Set up everything, reset the card, etc..
1465 */
1466static int
1467smc_open(struct net_device *dev)
1468{
1469 struct smc_local *lp = netdev_priv(dev);
1470
Ben Boeckel6389aa42013-11-01 08:53:31 -04001471 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* Setup the default Register Modes */
1474 lp->tcr_cur_mode = TCR_DEFAULT;
1475 lp->rcr_cur_mode = RCR_DEFAULT;
Russell Kingb0dbcf52008-09-04 21:13:37 +01001476 lp->rpc_cur_mode = RPC_DEFAULT |
1477 lp->cfg.leda << RPC_LSXA_SHFT |
1478 lp->cfg.ledb << RPC_LSXB_SHFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /*
1481 * If we are not using a MII interface, we need to
1482 * monitor our own carrier signal to detect faults.
1483 */
1484 if (lp->phy_type == 0)
1485 lp->tcr_cur_mode |= TCR_MON_CSN;
1486
1487 /* reset the hardware */
1488 smc_reset(dev);
1489 smc_enable(dev);
1490
1491 /* Configure the PHY, initialize the link state */
1492 if (lp->phy_type != 0)
David Howells6d5aefb2006-12-05 19:36:26 +00001493 smc_phy_configure(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 else {
1495 spin_lock_irq(&lp->lock);
1496 smc_10bt_check_media(dev, 1);
1497 spin_unlock_irq(&lp->lock);
1498 }
1499
1500 netif_start_queue(dev);
1501 return 0;
1502}
1503
1504/*
1505 * smc_close
1506 *
1507 * this makes the board clean up everything that it can
1508 * and not talk to the outside world. Caused by
1509 * an 'ifconfig ethX down'
1510 */
1511static int smc_close(struct net_device *dev)
1512{
1513 struct smc_local *lp = netdev_priv(dev);
1514
Ben Boeckel6389aa42013-11-01 08:53:31 -04001515 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 netif_stop_queue(dev);
1518 netif_carrier_off(dev);
1519
1520 /* clear everything */
1521 smc_shutdown(dev);
Nicolas Pitrebe836682005-06-19 23:56:21 -04001522 tasklet_kill(&lp->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 smc_phy_powerdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 0;
1525}
1526
1527/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 * Ethtool support
1529 */
1530static int
1531smc_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1532{
1533 struct smc_local *lp = netdev_priv(dev);
1534 int ret;
1535
1536 cmd->maxtxpkt = 1;
1537 cmd->maxrxpkt = 1;
1538
1539 if (lp->phy_type != 0) {
1540 spin_lock_irq(&lp->lock);
1541 ret = mii_ethtool_gset(&lp->mii, cmd);
1542 spin_unlock_irq(&lp->lock);
1543 } else {
1544 cmd->supported = SUPPORTED_10baseT_Half |
1545 SUPPORTED_10baseT_Full |
1546 SUPPORTED_TP | SUPPORTED_AUI;
1547
1548 if (lp->ctl_rspeed == 10)
David Decotigny70739492011-04-27 18:32:40 +00001549 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 else if (lp->ctl_rspeed == 100)
David Decotigny70739492011-04-27 18:32:40 +00001551 ethtool_cmd_speed_set(cmd, SPEED_100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 cmd->autoneg = AUTONEG_DISABLE;
1554 cmd->transceiver = XCVR_INTERNAL;
1555 cmd->port = 0;
1556 cmd->duplex = lp->tcr_cur_mode & TCR_SWFDUP ? DUPLEX_FULL : DUPLEX_HALF;
1557
1558 ret = 0;
1559 }
1560
1561 return ret;
1562}
1563
1564static int
1565smc_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1566{
1567 struct smc_local *lp = netdev_priv(dev);
1568 int ret;
1569
1570 if (lp->phy_type != 0) {
1571 spin_lock_irq(&lp->lock);
1572 ret = mii_ethtool_sset(&lp->mii, cmd);
1573 spin_unlock_irq(&lp->lock);
1574 } else {
1575 if (cmd->autoneg != AUTONEG_DISABLE ||
1576 cmd->speed != SPEED_10 ||
1577 (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
1578 (cmd->port != PORT_TP && cmd->port != PORT_AUI))
1579 return -EINVAL;
1580
1581// lp->port = cmd->port;
1582 lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
1583
1584// if (netif_running(dev))
1585// smc_set_port(dev);
1586
1587 ret = 0;
1588 }
1589
1590 return ret;
1591}
1592
1593static void
1594smc_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1595{
Jiri Pirko7826d432013-01-06 00:44:26 +00001596 strlcpy(info->driver, CARDNAME, sizeof(info->driver));
1597 strlcpy(info->version, version, sizeof(info->version));
1598 strlcpy(info->bus_info, dev_name(dev->dev.parent),
1599 sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602static int smc_ethtool_nwayreset(struct net_device *dev)
1603{
1604 struct smc_local *lp = netdev_priv(dev);
1605 int ret = -EINVAL;
1606
1607 if (lp->phy_type != 0) {
1608 spin_lock_irq(&lp->lock);
1609 ret = mii_nway_restart(&lp->mii);
1610 spin_unlock_irq(&lp->lock);
1611 }
1612
1613 return ret;
1614}
1615
1616static u32 smc_ethtool_getmsglevel(struct net_device *dev)
1617{
1618 struct smc_local *lp = netdev_priv(dev);
1619 return lp->msg_enable;
1620}
1621
1622static void smc_ethtool_setmsglevel(struct net_device *dev, u32 level)
1623{
1624 struct smc_local *lp = netdev_priv(dev);
1625 lp->msg_enable = level;
1626}
1627
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001628static int smc_write_eeprom_word(struct net_device *dev, u16 addr, u16 word)
1629{
1630 u16 ctl;
1631 struct smc_local *lp = netdev_priv(dev);
1632 void __iomem *ioaddr = lp->base;
1633
1634 spin_lock_irq(&lp->lock);
1635 /* load word into GP register */
1636 SMC_SELECT_BANK(lp, 1);
1637 SMC_SET_GP(lp, word);
1638 /* set the address to put the data in EEPROM */
1639 SMC_SELECT_BANK(lp, 2);
1640 SMC_SET_PTR(lp, addr);
1641 /* tell it to write */
1642 SMC_SELECT_BANK(lp, 1);
1643 ctl = SMC_GET_CTL(lp);
1644 SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_STORE));
1645 /* wait for it to finish */
1646 do {
1647 udelay(1);
1648 } while (SMC_GET_CTL(lp) & CTL_STORE);
1649 /* clean up */
1650 SMC_SET_CTL(lp, ctl);
1651 SMC_SELECT_BANK(lp, 2);
1652 spin_unlock_irq(&lp->lock);
1653 return 0;
1654}
1655
1656static int smc_read_eeprom_word(struct net_device *dev, u16 addr, u16 *word)
1657{
1658 u16 ctl;
1659 struct smc_local *lp = netdev_priv(dev);
1660 void __iomem *ioaddr = lp->base;
1661
1662 spin_lock_irq(&lp->lock);
1663 /* set the EEPROM address to get the data from */
1664 SMC_SELECT_BANK(lp, 2);
1665 SMC_SET_PTR(lp, addr | PTR_READ);
1666 /* tell it to load */
1667 SMC_SELECT_BANK(lp, 1);
1668 SMC_SET_GP(lp, 0xffff); /* init to known */
1669 ctl = SMC_GET_CTL(lp);
1670 SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_RELOAD));
1671 /* wait for it to finish */
1672 do {
1673 udelay(1);
1674 } while (SMC_GET_CTL(lp) & CTL_RELOAD);
1675 /* read word from GP register */
1676 *word = SMC_GET_GP(lp);
1677 /* clean up */
1678 SMC_SET_CTL(lp, ctl);
1679 SMC_SELECT_BANK(lp, 2);
1680 spin_unlock_irq(&lp->lock);
1681 return 0;
1682}
1683
1684static int smc_ethtool_geteeprom_len(struct net_device *dev)
1685{
1686 return 0x23 * 2;
1687}
1688
1689static int smc_ethtool_geteeprom(struct net_device *dev,
1690 struct ethtool_eeprom *eeprom, u8 *data)
1691{
1692 int i;
1693 int imax;
1694
Ben Boeckel6389aa42013-11-01 08:53:31 -04001695 DBG(1, dev, "Reading %d bytes at %d(0x%x)\n",
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001696 eeprom->len, eeprom->offset, eeprom->offset);
1697 imax = smc_ethtool_geteeprom_len(dev);
1698 for (i = 0; i < eeprom->len; i += 2) {
1699 int ret;
1700 u16 wbuf;
1701 int offset = i + eeprom->offset;
1702 if (offset > imax)
1703 break;
1704 ret = smc_read_eeprom_word(dev, offset >> 1, &wbuf);
1705 if (ret != 0)
1706 return ret;
Ben Boeckel6389aa42013-11-01 08:53:31 -04001707 DBG(2, dev, "Read 0x%x from 0x%x\n", wbuf, offset >> 1);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001708 data[i] = (wbuf >> 8) & 0xff;
1709 data[i+1] = wbuf & 0xff;
1710 }
1711 return 0;
1712}
1713
1714static int smc_ethtool_seteeprom(struct net_device *dev,
1715 struct ethtool_eeprom *eeprom, u8 *data)
1716{
1717 int i;
1718 int imax;
1719
Ben Boeckel6389aa42013-11-01 08:53:31 -04001720 DBG(1, dev, "Writing %d bytes to %d(0x%x)\n",
1721 eeprom->len, eeprom->offset, eeprom->offset);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001722 imax = smc_ethtool_geteeprom_len(dev);
1723 for (i = 0; i < eeprom->len; i += 2) {
1724 int ret;
1725 u16 wbuf;
1726 int offset = i + eeprom->offset;
1727 if (offset > imax)
1728 break;
1729 wbuf = (data[i] << 8) | data[i + 1];
Ben Boeckel6389aa42013-11-01 08:53:31 -04001730 DBG(2, dev, "Writing 0x%x to 0x%x\n", wbuf, offset >> 1);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001731 ret = smc_write_eeprom_word(dev, offset >> 1, wbuf);
1732 if (ret != 0)
1733 return ret;
1734 }
1735 return 0;
1736}
1737
1738
Jeff Garzik7282d492006-09-13 14:30:00 -04001739static const struct ethtool_ops smc_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .get_settings = smc_ethtool_getsettings,
1741 .set_settings = smc_ethtool_setsettings,
1742 .get_drvinfo = smc_ethtool_getdrvinfo,
1743
1744 .get_msglevel = smc_ethtool_getmsglevel,
1745 .set_msglevel = smc_ethtool_setmsglevel,
1746 .nway_reset = smc_ethtool_nwayreset,
1747 .get_link = ethtool_op_get_link,
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001748 .get_eeprom_len = smc_ethtool_geteeprom_len,
1749 .get_eeprom = smc_ethtool_geteeprom,
1750 .set_eeprom = smc_ethtool_seteeprom,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751};
1752
Magnus Damma5280792009-01-26 21:32:25 -08001753static const struct net_device_ops smc_netdev_ops = {
1754 .ndo_open = smc_open,
1755 .ndo_stop = smc_close,
1756 .ndo_start_xmit = smc_hard_start_xmit,
1757 .ndo_tx_timeout = smc_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001758 .ndo_set_rx_mode = smc_set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001759 .ndo_change_mtu = eth_change_mtu,
Magnus Damma5280792009-01-26 21:32:25 -08001760 .ndo_validate_addr = eth_validate_addr,
1761 .ndo_set_mac_address = eth_mac_addr,
1762#ifdef CONFIG_NET_POLL_CONTROLLER
1763 .ndo_poll_controller = smc_poll_controller,
1764#endif
1765};
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * smc_findirq
1769 *
1770 * This routine has a simple purpose -- make the SMC chip generate an
1771 * interrupt, so an auto-detect routine can detect it, and find the IRQ,
1772 */
1773/*
1774 * does this still work?
1775 *
1776 * I just deleted auto_irq.c, since it was never built...
1777 * --jgarzik
1778 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05001779static int smc_findirq(struct smc_local *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Magnus Dammcfdfa862008-02-22 19:55:05 +09001781 void __iomem *ioaddr = lp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int timeout = 20;
1783 unsigned long cookie;
1784
Ben Boeckel6389aa42013-11-01 08:53:31 -04001785 DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 cookie = probe_irq_on();
1788
1789 /*
1790 * What I try to do here is trigger an ALLOC_INT. This is done
1791 * by allocating a small chunk of memory, which will give an interrupt
1792 * when done.
1793 */
1794 /* enable ALLOCation interrupts ONLY */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001795 SMC_SELECT_BANK(lp, 2);
1796 SMC_SET_INT_MASK(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /*
1799 * Allocate 512 bytes of memory. Note that the chip was just
1800 * reset so all the memory is available
1801 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001802 SMC_SET_MMU_CMD(lp, MC_ALLOC | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 /*
1805 * Wait until positive that the interrupt has been generated
1806 */
1807 do {
1808 int int_status;
1809 udelay(10);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001810 int_status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (int_status & IM_ALLOC_INT)
1812 break; /* got the interrupt */
1813 } while (--timeout);
1814
1815 /*
1816 * there is really nothing that I can do here if timeout fails,
1817 * as autoirq_report will return a 0 anyway, which is what I
1818 * want in this case. Plus, the clean up is needed in both
1819 * cases.
1820 */
1821
1822 /* and disable all interrupts again */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001823 SMC_SET_INT_MASK(lp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* and return what I found */
1826 return probe_irq_off(cookie);
1827}
1828
1829/*
1830 * Function: smc_probe(unsigned long ioaddr)
1831 *
1832 * Purpose:
1833 * Tests to see if a given ioaddr points to an SMC91x chip.
1834 * Returns a 0 on success
1835 *
1836 * Algorithm:
1837 * (1) see if the high byte of BANK_SELECT is 0x33
1838 * (2) compare the ioaddr with the base register's address
1839 * (3) see if I recognize the chip ID in the appropriate register
1840 *
1841 * Here I do typical initialization tasks.
1842 *
1843 * o Initialize the structure if needed
1844 * o print out my vanity message if not done so already
1845 * o print out what type of hardware is detected
1846 * o print out the ethernet address
1847 * o find the IRQ
1848 * o set up my private data
1849 * o configure the dev structure with my subroutines
1850 * o actually GRAB the irq.
1851 * o GRAB the region
1852 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05001853static int smc_probe(struct net_device *dev, void __iomem *ioaddr,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001854 unsigned long irq_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct smc_local *lp = netdev_priv(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001857 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 unsigned int val, revision_register;
1859 const char *version_string;
1860
Ben Boeckel6389aa42013-11-01 08:53:31 -04001861 DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* First, see if the high byte is 0x33 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001864 val = SMC_CURRENT_BANK(lp);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001865 DBG(2, dev, "%s: bank signature probe returned 0x%04x\n",
1866 CARDNAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if ((val & 0xFF00) != 0x3300) {
1868 if ((val & 0xFF) == 0x33) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001869 netdev_warn(dev,
1870 "%s: Detected possible byte-swapped interface at IOADDR %p\n",
1871 CARDNAME, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 retval = -ENODEV;
1874 goto err_out;
1875 }
1876
1877 /*
1878 * The above MIGHT indicate a device, but I need to write to
1879 * further test this.
1880 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001881 SMC_SELECT_BANK(lp, 0);
1882 val = SMC_CURRENT_BANK(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if ((val & 0xFF00) != 0x3300) {
1884 retval = -ENODEV;
1885 goto err_out;
1886 }
1887
1888 /*
1889 * well, we've already written once, so hopefully another
1890 * time won't hurt. This time, I need to switch the bank
1891 * register to bank 1, so I can access the base address
1892 * register
1893 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001894 SMC_SELECT_BANK(lp, 1);
1895 val = SMC_GET_BASE(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 val = ((val & 0x1F00) >> 3) << SMC_IO_SHIFT;
Nicolas Pitre53155102005-05-12 20:18:19 -04001897 if (((unsigned int)ioaddr & (0x3e0 << SMC_IO_SHIFT)) != val) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001898 netdev_warn(dev, "%s: IOADDR %p doesn't match configuration (%x).\n",
1899 CARDNAME, ioaddr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901
1902 /*
1903 * check if the revision register is something that I
1904 * recognize. These might need to be added to later,
1905 * as future revisions could be added.
1906 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001907 SMC_SELECT_BANK(lp, 3);
1908 revision_register = SMC_GET_REV(lp);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001909 DBG(2, dev, "%s: revision = 0x%04x\n", CARDNAME, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 version_string = chip_ids[ (revision_register >> 4) & 0xF];
1911 if (!version_string || (revision_register & 0xff00) != 0x3300) {
1912 /* I don't recognize this chip, so... */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001913 netdev_warn(dev, "%s: IO %p: Unrecognized revision register 0x%04x, Contact author.\n",
1914 CARDNAME, ioaddr, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 retval = -ENODEV;
1917 goto err_out;
1918 }
1919
1920 /* At this point I'll assume that the chip is an SMC91x. */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001921 pr_info_once("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /* fill in some of the fields */
1924 dev->base_addr = (unsigned long)ioaddr;
1925 lp->base = ioaddr;
1926 lp->version = revision_register & 0xff;
1927 spin_lock_init(&lp->lock);
1928
1929 /* Get the MAC address */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001930 SMC_SELECT_BANK(lp, 1);
1931 SMC_GET_MAC_ADDR(lp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* now, reset the chip, and put it into a known state */
1934 smc_reset(dev);
1935
1936 /*
1937 * If dev->irq is 0, then the device has to be banged on to see
1938 * what the IRQ is.
Ben Boeckel6389aa42013-11-01 08:53:31 -04001939 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * This banging doesn't always detect the IRQ, for unknown reasons.
1941 * a workaround is to reset the chip and try again.
1942 *
1943 * Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1944 * be what is requested on the command line. I don't do that, mostly
1945 * because the card that I have uses a non-standard method of accessing
1946 * the IRQs, and because this _should_ work in most configurations.
1947 *
1948 * Specifying an IRQ is done with the assumption that the user knows
1949 * what (s)he is doing. No checking is done!!!!
1950 */
1951 if (dev->irq < 1) {
1952 int trials;
1953
1954 trials = 3;
1955 while (trials--) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001956 dev->irq = smc_findirq(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (dev->irq)
1958 break;
1959 /* kick the card and try again */
1960 smc_reset(dev);
1961 }
1962 }
1963 if (dev->irq == 0) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001964 netdev_warn(dev, "Couldn't autodetect your IRQ. Use irq=xx.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 retval = -ENODEV;
1966 goto err_out;
1967 }
1968 dev->irq = irq_canonicalize(dev->irq);
1969
1970 /* Fill in the fields of the device structure with ethernet values. */
1971 ether_setup(dev);
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
Magnus Damma5280792009-01-26 21:32:25 -08001974 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 dev->ethtool_ops = &smc_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 tasklet_init(&lp->tx_task, smc_hardware_send_pkt, (unsigned long)dev);
David Howells6d5aefb2006-12-05 19:36:26 +00001978 INIT_WORK(&lp->phy_configure, smc_phy_configure);
1979 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 lp->mii.phy_id_mask = 0x1f;
1981 lp->mii.reg_num_mask = 0x1f;
1982 lp->mii.force_media = 0;
1983 lp->mii.full_duplex = 0;
1984 lp->mii.dev = dev;
1985 lp->mii.mdio_read = smc_phy_read;
1986 lp->mii.mdio_write = smc_phy_write;
1987
1988 /*
1989 * Locate the phy, if any.
1990 */
1991 if (lp->version >= (CHIP_91100 << 4))
1992 smc_phy_detect(dev);
1993
Nicolas Pitre99e1baf2005-10-05 11:10:24 -04001994 /* then shut everything down to save power */
1995 smc_shutdown(dev);
1996 smc_phy_powerdown(dev);
1997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /* Set default parameters */
1999 lp->msg_enable = NETIF_MSG_LINK;
2000 lp->ctl_rfduplx = 0;
2001 lp->ctl_rspeed = 10;
2002
2003 if (lp->version >= (CHIP_91100 << 4)) {
2004 lp->ctl_rfduplx = 1;
2005 lp->ctl_rspeed = 100;
2006 }
2007
2008 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -08002009 retval = request_irq(dev->irq, smc_interrupt, irq_flags, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (retval)
2011 goto err_out;
2012
Eric Miao52256c02008-06-24 15:36:05 +08002013#ifdef CONFIG_ARCH_PXA
2014# ifdef SMC_USE_PXA_DMA
2015 lp->cfg.flags |= SMC91X_USE_DMA;
2016# endif
2017 if (lp->cfg.flags & SMC91X_USE_DMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 int dma = pxa_request_dma(dev->name, DMA_PRIO_LOW,
2019 smc_pxa_dma_irq, NULL);
2020 if (dma >= 0)
2021 dev->dma = dma;
2022 }
2023#endif
2024
2025 retval = register_netdev(dev);
2026 if (retval == 0) {
2027 /* now, print out the card info, in a short format.. */
Ben Boeckel6389aa42013-11-01 08:53:31 -04002028 netdev_info(dev, "%s (rev %d) at %p IRQ %d",
2029 version_string, revision_register & 0x0f,
2030 lp->base, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (dev->dma != (unsigned char)-1)
Ben Boeckel6389aa42013-11-01 08:53:31 -04002033 pr_cont(" DMA %d", dev->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Ben Boeckel6389aa42013-11-01 08:53:31 -04002035 pr_cont("%s%s\n",
Magnus Dammd6bc3722008-09-08 14:02:56 +09002036 lp->cfg.flags & SMC91X_NOWAIT ? " [nowait]" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 THROTTLE_TX_PKTS ? " [throttle_tx]" : "");
2038
2039 if (!is_valid_ether_addr(dev->dev_addr)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002040 netdev_warn(dev, "Invalid ethernet MAC address. Please set using ifconfig\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 } else {
2042 /* Print the Ethernet address */
Ben Boeckel6389aa42013-11-01 08:53:31 -04002043 netdev_info(dev, "Ethernet addr: %pM\n",
2044 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046
2047 if (lp->phy_type == 0) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002048 PRINTK(dev, "No PHY found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } else if ((lp->phy_type & 0xfffffff0) == 0x0016f840) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002050 PRINTK(dev, "PHY LAN83C183 (LAN91C111 Internal)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } else if ((lp->phy_type & 0xfffffff0) == 0x02821c50) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002052 PRINTK(dev, "PHY LAN83C180\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054 }
2055
2056err_out:
Eric Miao52256c02008-06-24 15:36:05 +08002057#ifdef CONFIG_ARCH_PXA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (retval && dev->dma != (unsigned char)-1)
2059 pxa_free_dma(dev->dma);
2060#endif
2061 return retval;
2062}
2063
2064static int smc_enable_device(struct platform_device *pdev)
2065{
Magnus Damm3e947942008-02-22 19:55:15 +09002066 struct net_device *ndev = platform_get_drvdata(pdev);
2067 struct smc_local *lp = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 unsigned long flags;
2069 unsigned char ecor, ecsr;
2070 void __iomem *addr;
2071 struct resource * res;
2072
2073 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2074 if (!res)
2075 return 0;
2076
2077 /*
2078 * Map the attribute space. This is overkill, but clean.
2079 */
2080 addr = ioremap(res->start, ATTRIB_SIZE);
2081 if (!addr)
2082 return -ENOMEM;
2083
2084 /*
2085 * Reset the device. We must disable IRQs around this
2086 * since a reset causes the IRQ line become active.
2087 */
2088 local_irq_save(flags);
2089 ecor = readb(addr + (ECOR << SMC_IO_SHIFT)) & ~ECOR_RESET;
2090 writeb(ecor | ECOR_RESET, addr + (ECOR << SMC_IO_SHIFT));
2091 readb(addr + (ECOR << SMC_IO_SHIFT));
2092
2093 /*
2094 * Wait 100us for the chip to reset.
2095 */
2096 udelay(100);
2097
2098 /*
2099 * The device will ignore all writes to the enable bit while
2100 * reset is asserted, even if the reset bit is cleared in the
2101 * same write. Must clear reset first, then enable the device.
2102 */
2103 writeb(ecor, addr + (ECOR << SMC_IO_SHIFT));
2104 writeb(ecor | ECOR_ENABLE, addr + (ECOR << SMC_IO_SHIFT));
2105
2106 /*
2107 * Set the appropriate byte/word mode.
2108 */
2109 ecsr = readb(addr + (ECSR << SMC_IO_SHIFT)) & ~ECSR_IOIS8;
Magnus Damm3e947942008-02-22 19:55:15 +09002110 if (!SMC_16BIT(lp))
Nicolas Pitre09779c62006-03-20 11:54:27 -05002111 ecsr |= ECSR_IOIS8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 writeb(ecsr, addr + (ECSR << SMC_IO_SHIFT));
2113 local_irq_restore(flags);
2114
2115 iounmap(addr);
2116
2117 /*
2118 * Wait for the chip to wake up. We could poll the control
2119 * register in the main register space, but that isn't mapped
2120 * yet. We know this is going to take 750us.
2121 */
2122 msleep(1);
2123
2124 return 0;
2125}
2126
Eric Miao15919882008-06-24 13:38:50 +08002127static int smc_request_attrib(struct platform_device *pdev,
2128 struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
David S. Miller55c8eb62008-11-03 00:04:24 -08002131 struct smc_local *lp __maybe_unused = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 if (!res)
2134 return 0;
2135
2136 if (!request_mem_region(res->start, ATTRIB_SIZE, CARDNAME))
2137 return -EBUSY;
2138
2139 return 0;
2140}
2141
Eric Miao15919882008-06-24 13:38:50 +08002142static void smc_release_attrib(struct platform_device *pdev,
2143 struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
David S. Miller55c8eb62008-11-03 00:04:24 -08002146 struct smc_local *lp __maybe_unused = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (res)
2149 release_mem_region(res->start, ATTRIB_SIZE);
2150}
2151
Nicolas Pitre09779c62006-03-20 11:54:27 -05002152static inline void smc_request_datacs(struct platform_device *pdev, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Nicolas Pitre09779c62006-03-20 11:54:27 -05002154 if (SMC_CAN_USE_DATACS) {
2155 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2156 struct smc_local *lp = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Nicolas Pitre09779c62006-03-20 11:54:27 -05002158 if (!res)
2159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Nicolas Pitre09779c62006-03-20 11:54:27 -05002161 if(!request_mem_region(res->start, SMC_DATA_EXTENT, CARDNAME)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002162 netdev_info(ndev, "%s: failed to request datacs memory region.\n",
2163 CARDNAME);
Nicolas Pitre09779c62006-03-20 11:54:27 -05002164 return;
2165 }
2166
2167 lp->datacs = ioremap(res->start, SMC_DATA_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
2171static void smc_release_datacs(struct platform_device *pdev, struct net_device *ndev)
2172{
Nicolas Pitre09779c62006-03-20 11:54:27 -05002173 if (SMC_CAN_USE_DATACS) {
2174 struct smc_local *lp = netdev_priv(ndev);
2175 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Nicolas Pitre09779c62006-03-20 11:54:27 -05002177 if (lp->datacs)
2178 iounmap(lp->datacs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Nicolas Pitre09779c62006-03-20 11:54:27 -05002180 lp->datacs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Nicolas Pitre09779c62006-03-20 11:54:27 -05002182 if (res)
2183 release_mem_region(res->start, SMC_DATA_EXTENT);
2184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187/*
2188 * smc_init(void)
2189 * Input parameters:
2190 * dev->base_addr == 0, try to find all possible locations
2191 * dev->base_addr > 0x1ff, this is the address to check
2192 * dev->base_addr == <anything else>, return failure code
2193 *
2194 * Output:
2195 * 0 --> there is a device
2196 * anything else, error
2197 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002198static int smc_drv_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Jingoo Hanf64deac2013-08-30 14:01:35 +09002200 struct smc91x_platdata *pd = dev_get_platdata(&pdev->dev);
Magnus Damm3e947942008-02-22 19:55:15 +09002201 struct smc_local *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct net_device *ndev;
Russell Kinge7b3dc72008-01-14 22:30:10 +00002203 struct resource *res, *ires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 unsigned int __iomem *addr;
Eric Miaod280ead2008-06-06 17:13:02 +08002205 unsigned long irq_flags = SMC_IRQ_FLAGS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int ret;
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 ndev = alloc_etherdev(sizeof(struct smc_local));
2209 if (!ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 ret = -ENOMEM;
Eric Miao15919882008-06-24 13:38:50 +08002211 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Russell King3ae5eae2005-11-09 22:32:44 +00002213 SET_NETDEV_DEV(ndev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Magnus Damm3e947942008-02-22 19:55:15 +09002215 /* get configuration from platform data, only allow use of
2216 * bus width if both SMC_CAN_USE_xxx and SMC91X_USE_xxx are set.
2217 */
2218
2219 lp = netdev_priv(ndev);
Magnus Damm3e947942008-02-22 19:55:15 +09002220
Eric Miao15919882008-06-24 13:38:50 +08002221 if (pd) {
Magnus Damm3e947942008-02-22 19:55:15 +09002222 memcpy(&lp->cfg, pd, sizeof(lp->cfg));
Eric Miao15919882008-06-24 13:38:50 +08002223 lp->io_shift = SMC91X_IO_SHIFT(lp->cfg.flags);
2224 } else {
Eric Miaofa6d3be2008-06-19 17:19:57 +08002225 lp->cfg.flags |= (SMC_CAN_USE_8BIT) ? SMC91X_USE_8BIT : 0;
2226 lp->cfg.flags |= (SMC_CAN_USE_16BIT) ? SMC91X_USE_16BIT : 0;
2227 lp->cfg.flags |= (SMC_CAN_USE_32BIT) ? SMC91X_USE_32BIT : 0;
Eric Miaoc4f0e762008-06-19 17:39:03 +08002228 lp->cfg.flags |= (nowait) ? SMC91X_NOWAIT : 0;
Magnus Damm3e947942008-02-22 19:55:15 +09002229 }
2230
Russell Kingb0dbcf52008-09-04 21:13:37 +01002231 if (!lp->cfg.leda && !lp->cfg.ledb) {
2232 lp->cfg.leda = RPC_LSA_DEFAULT;
2233 lp->cfg.ledb = RPC_LSB_DEFAULT;
2234 }
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 ndev->dma = (unsigned char)-1;
Russell Kinge7b3dc72008-01-14 22:30:10 +00002237
Eric Miao15919882008-06-24 13:38:50 +08002238 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2239 if (!res)
2240 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2241 if (!res) {
2242 ret = -ENODEV;
2243 goto out_free_netdev;
2244 }
2245
2246
2247 if (!request_mem_region(res->start, SMC_IO_EXTENT, CARDNAME)) {
2248 ret = -EBUSY;
2249 goto out_free_netdev;
2250 }
2251
Russell Kinge7b3dc72008-01-14 22:30:10 +00002252 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2253 if (!ires) {
David Vrabel48944732006-01-19 17:56:29 +00002254 ret = -ENODEV;
Eric Miao15919882008-06-24 13:38:50 +08002255 goto out_release_io;
David Vrabel48944732006-01-19 17:56:29 +00002256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Russell Kinge7b3dc72008-01-14 22:30:10 +00002258 ndev->irq = ires->start;
Eric Miaod280ead2008-06-06 17:13:02 +08002259
Russell King - ARM Linuxd5ccd672009-11-28 00:13:23 +00002260 if (irq_flags == -1 || ires->flags & IRQF_TRIGGER_MASK)
Eric Miaod280ead2008-06-06 17:13:02 +08002261 irq_flags = ires->flags & IRQF_TRIGGER_MASK;
Russell Kinge7b3dc72008-01-14 22:30:10 +00002262
Eric Miao15919882008-06-24 13:38:50 +08002263 ret = smc_request_attrib(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (ret)
Eric Miao15919882008-06-24 13:38:50 +08002265 goto out_release_io;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266#if defined(CONFIG_SA1100_ASSABET)
Russell King6ad1b612012-01-16 09:31:47 +00002267 neponset_ncr_set(NCR_ENET_OSC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268#endif
Magnus Damm3e947942008-02-22 19:55:15 +09002269 platform_set_drvdata(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 ret = smc_enable_device(pdev);
2271 if (ret)
2272 goto out_release_attrib;
2273
2274 addr = ioremap(res->start, SMC_IO_EXTENT);
2275 if (!addr) {
2276 ret = -ENOMEM;
2277 goto out_release_attrib;
2278 }
2279
Eric Miao52256c02008-06-24 15:36:05 +08002280#ifdef CONFIG_ARCH_PXA
Russell King073ac8f2007-09-01 21:27:18 +01002281 {
2282 struct smc_local *lp = netdev_priv(ndev);
2283 lp->device = &pdev->dev;
2284 lp->physaddr = res->start;
2285 }
2286#endif
2287
Eric Miaod280ead2008-06-06 17:13:02 +08002288 ret = smc_probe(ndev, addr, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (ret != 0)
2290 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 smc_request_datacs(pdev, ndev);
2293
2294 return 0;
2295
2296 out_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 iounmap(addr);
2298 out_release_attrib:
Eric Miao15919882008-06-24 13:38:50 +08002299 smc_release_attrib(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 out_release_io:
2301 release_mem_region(res->start, SMC_IO_EXTENT);
Eric Miao15919882008-06-24 13:38:50 +08002302 out_free_netdev:
2303 free_netdev(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 out:
Ben Boeckel6389aa42013-11-01 08:53:31 -04002305 pr_info("%s: not found (%d).\n", CARDNAME, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 return ret;
2308}
2309
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002310static int smc_drv_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Russell King3ae5eae2005-11-09 22:32:44 +00002312 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct smc_local *lp = netdev_priv(ndev);
2314 struct resource *res;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 unregister_netdev(ndev);
2317
2318 free_irq(ndev->irq, ndev);
2319
Eric Miao52256c02008-06-24 15:36:05 +08002320#ifdef CONFIG_ARCH_PXA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ndev->dma != (unsigned char)-1)
2322 pxa_free_dma(ndev->dma);
2323#endif
2324 iounmap(lp->base);
2325
2326 smc_release_datacs(pdev,ndev);
Eric Miao15919882008-06-24 13:38:50 +08002327 smc_release_attrib(pdev,ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2330 if (!res)
Jeff Garzik6fc30db2008-08-27 05:54:30 -04002331 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 release_mem_region(res->start, SMC_IO_EXTENT);
2333
2334 free_netdev(ndev);
2335
2336 return 0;
2337}
2338
Kevin Hilman9f950f72009-11-24 12:57:47 +00002339static int smc_drv_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Kevin Hilman9f950f72009-11-24 12:57:47 +00002341 struct platform_device *pdev = to_platform_device(dev);
2342 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Russell King9480e302005-10-28 09:52:56 -07002344 if (ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (netif_running(ndev)) {
2346 netif_device_detach(ndev);
2347 smc_shutdown(ndev);
2348 smc_phy_powerdown(ndev);
2349 }
2350 }
2351 return 0;
2352}
2353
Kevin Hilman9f950f72009-11-24 12:57:47 +00002354static int smc_drv_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Kevin Hilman9f950f72009-11-24 12:57:47 +00002356 struct platform_device *pdev = to_platform_device(dev);
2357 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Russell King9480e302005-10-28 09:52:56 -07002359 if (ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct smc_local *lp = netdev_priv(ndev);
Paul Mundt5fc34412009-12-10 20:42:27 +00002361 smc_enable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (netif_running(ndev)) {
2363 smc_reset(ndev);
2364 smc_enable(ndev);
2365 if (lp->phy_type != 0)
David Howells6d5aefb2006-12-05 19:36:26 +00002366 smc_phy_configure(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 netif_device_attach(ndev);
2368 }
2369 }
2370 return 0;
2371}
2372
Thomas Chou682a1692011-01-25 19:22:05 +00002373#ifdef CONFIG_OF
2374static const struct of_device_id smc91x_match[] = {
2375 { .compatible = "smsc,lan91c94", },
2376 { .compatible = "smsc,lan91c111", },
2377 {},
Grant Likely5fb9fb12011-06-03 21:31:27 +00002378};
Thomas Chou682a1692011-01-25 19:22:05 +00002379MODULE_DEVICE_TABLE(of, smc91x_match);
2380#endif
2381
Kevin Hilman9f950f72009-11-24 12:57:47 +00002382static struct dev_pm_ops smc_drv_pm_ops = {
2383 .suspend = smc_drv_suspend,
2384 .resume = smc_drv_resume,
2385};
2386
Russell King3ae5eae2005-11-09 22:32:44 +00002387static struct platform_driver smc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 .probe = smc_drv_probe,
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002389 .remove = smc_drv_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002390 .driver = {
2391 .name = CARDNAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002392 .owner = THIS_MODULE,
Kevin Hilman9f950f72009-11-24 12:57:47 +00002393 .pm = &smc_drv_pm_ops,
Sachin Kamat89ce3762012-12-19 01:17:09 +00002394 .of_match_table = of_match_ptr(smc91x_match),
Russell King3ae5eae2005-11-09 22:32:44 +00002395 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396};
2397
Axel Lindb62f682011-11-27 16:44:17 +00002398module_platform_driver(smc_driver);