blob: 209ee1b27f8d75aa2a3efa12c74ae28484f465c5 [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
Jeff Kirsher0ab75ae2013-12-06 06:28:43 -080022 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
24 * Arguments:
25 * io = for the base address
26 * irq = for the IRQ
27 * nowait = 0 for normal wait states, 1 eliminates additional wait states
28 *
29 * original author:
30 * Erik Stahlman <erik@vt.edu>
31 *
32 * hardware multicast code:
33 * Peter Cammaert <pc@denkart.be>
34 *
35 * contributors:
36 * Daris A Nevil <dnevil@snmc.com>
Nicolas Pitre2f82af02009-09-14 03:25:28 -040037 * Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * Russell King <rmk@arm.linux.org.uk>
39 *
40 * History:
41 * 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
42 * 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
43 * 03/16/01 Daris A Nevil modified smc9194.c for use with LAN91C111
44 * 08/22/01 Scott Anderson merge changes from smc9194 to smc91111
45 * 08/21/01 Pramod B Bhardwaj added support for RevB of LAN91C111
46 * 12/20/01 Jeff Sutherland initial port to Xscale PXA with DMA support
47 * 04/07/03 Nicolas Pitre unified SMC91x driver, killed irq races,
48 * more bus abstraction, big cleanup, etc.
49 * 29/09/03 Russell King - add driver model support
50 * - ethtool support
51 * - convert to use generic MII interface
52 * - add link up/down notification
53 * - don't try to handle full negotiation in
54 * smc_phy_configure
55 * - clean up (and fix stack overrun) in PHY
56 * MII read/write functions
57 * 22/09/04 Nicolas Pitre big update (see commit log for details)
58 */
59static const char version[] =
Ben Boeckel6389aa42013-11-01 08:53:31 -040060 "smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@fluxnic.net>";
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* Debugging level */
63#ifndef SMC_DEBUG
64#define SMC_DEBUG 0
65#endif
66
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/module.h>
69#include <linux/kernel.h>
70#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/delay.h>
72#include <linux/interrupt.h>
David Howells476c32c2010-10-07 14:08:54 +010073#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/errno.h>
75#include <linux/ioport.h>
76#include <linux/crc32.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010077#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/ethtool.h>
80#include <linux/mii.h>
81#include <linux/workqueue.h>
Thomas Chou682a1692011-01-25 19:22:05 +000082#include <linux/of.h>
Tony Lindgren3f823c12013-12-11 13:04:27 -080083#include <linux/of_device.h>
Tony Lindgren7d2911c2014-10-30 09:59:27 -070084#include <linux/of_gpio.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
Arnd Bergmannb70661c2015-02-25 16:31:57 +010094#if defined(CONFIG_ASSABET_NEPONSET)
95#include <mach/neponset.h>
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#ifndef SMC_NOWAIT
99# define SMC_NOWAIT 0
100#endif
101static int nowait = SMC_NOWAIT;
102module_param(nowait, int, 0400);
103MODULE_PARM_DESC(nowait, "set to 1 for no wait state");
104
105/*
106 * Transmit timeout, default 5 seconds.
107 */
Nicolas Pitreea937562005-04-12 16:26:40 -0400108static int watchdog = 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109module_param(watchdog, int, 0400);
110MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
111
112MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -0700113MODULE_ALIAS("platform:smc91x");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115/*
116 * The internal workings of the driver. If you are changing anything
117 * here with the SMC stuff, you should have the datasheet and know
118 * what you are doing.
119 */
120#define CARDNAME "smc91x"
121
122/*
123 * Use power-down feature of the chip
124 */
125#define POWER_DOWN 1
126
127/*
128 * Wait time for memory to be free. This probably shouldn't be
129 * tuned that much, as waiting for this means nothing else happens
130 * in the system
131 */
132#define MEMORY_WAIT_TIME 16
133
134/*
Nicolas Pitre5d0571d2005-11-17 14:02:48 -0500135 * The maximum number of processing loops allowed for each call to the
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400136 * IRQ handler.
Nicolas Pitre5d0571d2005-11-17 14:02:48 -0500137 */
138#define MAX_IRQ_LOOPS 8
139
140/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * This selects whether TX packets are sent one by one to the SMC91x internal
142 * memory and throttled until transmission completes. This may prevent
143 * RX overruns a litle by keeping much of the memory free for RX packets
144 * but to the expense of reduced TX throughput and increased IRQ overhead.
145 * Note this is not a cure for a too slow data bus or too high IRQ latency.
146 */
147#define THROTTLE_TX_PKTS 0
148
149/*
150 * The MII clock high/low times. 2x this number gives the MII clock period
151 * in microseconds. (was 50, but this gives 6.4ms for each MII transaction!)
152 */
153#define MII_DELAY 1
154
Zi Shen Lima450a682014-04-22 20:04:42 -0700155#define DBG(n, dev, fmt, ...) \
156 do { \
157 if (SMC_DEBUG >= (n)) \
158 netdev_dbg(dev, fmt, ##__VA_ARGS__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } while (0)
160
Zi Shen Lima450a682014-04-22 20:04:42 -0700161#define PRINTK(dev, fmt, ...) \
162 do { \
163 if (SMC_DEBUG > 0) \
164 netdev_info(dev, fmt, ##__VA_ARGS__); \
165 else \
166 netdev_dbg(dev, fmt, ##__VA_ARGS__); \
167 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169#if SMC_DEBUG > 3
170static void PRINT_PKT(u_char *buf, int length)
171{
172 int i;
173 int remainder;
174 int lines;
175
176 lines = length / 16;
177 remainder = length % 16;
178
179 for (i = 0; i < lines ; i ++) {
180 int cur;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400181 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 for (cur = 0; cur < 8; cur++) {
183 u_char a, b;
184 a = *buf++;
185 b = *buf++;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400186 pr_cont("%02x%02x ", a, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400188 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400190 printk(KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 for (i = 0; i < remainder/2 ; i++) {
192 u_char a, b;
193 a = *buf++;
194 b = *buf++;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400195 pr_cont("%02x%02x ", a, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Ben Boeckel6389aa42013-11-01 08:53:31 -0400197 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199#else
Zi Shen Lima450a682014-04-22 20:04:42 -0700200static inline void PRINT_PKT(u_char *buf, int length) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201#endif
202
203
204/* this enables an interrupt in the interrupt mask register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900205#define SMC_ENABLE_INT(lp, x) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 unsigned char mask; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700207 unsigned long smc_enable_flags; \
208 spin_lock_irqsave(&lp->lock, smc_enable_flags); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900209 mask = SMC_GET_INT_MASK(lp); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 mask |= (x); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900211 SMC_SET_INT_MASK(lp, mask); \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700212 spin_unlock_irqrestore(&lp->lock, smc_enable_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213} while (0)
214
215/* this disables an interrupt from the interrupt mask register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900216#define SMC_DISABLE_INT(lp, x) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned char mask; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700218 unsigned long smc_disable_flags; \
219 spin_lock_irqsave(&lp->lock, smc_disable_flags); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900220 mask = SMC_GET_INT_MASK(lp); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mask &= ~(x); \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900222 SMC_SET_INT_MASK(lp, mask); \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700223 spin_unlock_irqrestore(&lp->lock, smc_disable_flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224} while (0)
225
226/*
227 * Wait while MMU is busy. This is usually in the order of a few nanosecs
228 * if at all, but let's avoid deadlocking the system if the hardware
229 * decides to go south.
230 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900231#define SMC_WAIT_MMU_BUSY(lp) do { \
232 if (unlikely(SMC_GET_MMU_CMD(lp) & MC_BUSY)) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned long timeout = jiffies + 2; \
Magnus Dammcfdfa862008-02-22 19:55:05 +0900234 while (SMC_GET_MMU_CMD(lp) & MC_BUSY) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (time_after(jiffies, timeout)) { \
Ben Boeckel6389aa42013-11-01 08:53:31 -0400236 netdev_dbg(dev, "timeout %s line %d\n", \
237 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 break; \
239 } \
240 cpu_relax(); \
241 } \
242 } \
243} while (0)
244
245
246/*
247 * this does a soft reset on the device
248 */
249static void smc_reset(struct net_device *dev)
250{
251 struct smc_local *lp = netdev_priv(dev);
252 void __iomem *ioaddr = lp->base;
253 unsigned int ctl, cfg;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400254 struct sk_buff *pending_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Ben Boeckel6389aa42013-11-01 08:53:31 -0400256 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Nicolas Pitrebe836682005-06-19 23:56:21 -0400258 /* Disable all interrupts, block TX tasklet */
Russell King76cb4fe2006-08-14 23:00:20 -0700259 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900260 SMC_SELECT_BANK(lp, 2);
261 SMC_SET_INT_MASK(lp, 0);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400262 pending_skb = lp->pending_tx_skb;
263 lp->pending_tx_skb = NULL;
Russell King76cb4fe2006-08-14 23:00:20 -0700264 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Nicolas Pitrebe836682005-06-19 23:56:21 -0400266 /* free any pending tx skb */
267 if (pending_skb) {
268 dev_kfree_skb(pending_skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700269 dev->stats.tx_errors++;
270 dev->stats.tx_aborted_errors++;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400271 }
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /*
274 * This resets the registers mostly to defaults, but doesn't
275 * affect EEPROM. That seems unnecessary
276 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900277 SMC_SELECT_BANK(lp, 0);
278 SMC_SET_RCR(lp, RCR_SOFTRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /*
281 * Setup the Configuration Register
282 * This is necessary because the CONFIG_REG is not affected
283 * by a soft reset
284 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900285 SMC_SELECT_BANK(lp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 cfg = CONFIG_DEFAULT;
288
289 /*
290 * Setup for fast accesses if requested. If the card/system
291 * can't handle it then there will be no recovery except for
292 * a hard reset or power cycle
293 */
Eric Miaoc4f0e762008-06-19 17:39:03 +0800294 if (lp->cfg.flags & SMC91X_NOWAIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 cfg |= CONFIG_NO_WAIT;
296
297 /*
298 * Release from possible power-down state
299 * Configuration register is not affected by Soft Reset
300 */
301 cfg |= CONFIG_EPH_POWER_EN;
302
Magnus Dammcfdfa862008-02-22 19:55:05 +0900303 SMC_SET_CONFIG(lp, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* this should pause enough for the chip to be happy */
306 /*
307 * elaborate? What does the chip _need_? --jgarzik
308 *
309 * This seems to be undocumented, but something the original
310 * driver(s) have always done. Suspect undocumented timing
311 * info/determined empirically. --rmk
312 */
313 udelay(1);
314
315 /* Disable transmit and receive functionality */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900316 SMC_SELECT_BANK(lp, 0);
317 SMC_SET_RCR(lp, RCR_CLEAR);
318 SMC_SET_TCR(lp, TCR_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Magnus Dammcfdfa862008-02-22 19:55:05 +0900320 SMC_SELECT_BANK(lp, 1);
321 ctl = SMC_GET_CTL(lp) | CTL_LE_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /*
324 * Set the control register to automatically release successfully
325 * transmitted packets, to make the best use out of our limited
326 * memory
327 */
328 if(!THROTTLE_TX_PKTS)
329 ctl |= CTL_AUTO_RELEASE;
330 else
331 ctl &= ~CTL_AUTO_RELEASE;
Magnus Dammcfdfa862008-02-22 19:55:05 +0900332 SMC_SET_CTL(lp, ctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Reset the MMU */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900335 SMC_SELECT_BANK(lp, 2);
336 SMC_SET_MMU_CMD(lp, MC_RESET);
337 SMC_WAIT_MMU_BUSY(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * Enable Interrupts, Receive, and Transmit
342 */
343static void smc_enable(struct net_device *dev)
344{
345 struct smc_local *lp = netdev_priv(dev);
346 void __iomem *ioaddr = lp->base;
347 int mask;
348
Ben Boeckel6389aa42013-11-01 08:53:31 -0400349 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* see the header file for options in TCR/RCR DEFAULT */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900352 SMC_SELECT_BANK(lp, 0);
353 SMC_SET_TCR(lp, lp->tcr_cur_mode);
354 SMC_SET_RCR(lp, lp->rcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Magnus Dammcfdfa862008-02-22 19:55:05 +0900356 SMC_SELECT_BANK(lp, 1);
357 SMC_SET_MAC_ADDR(lp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* now, enable interrupts */
360 mask = IM_EPH_INT|IM_RX_OVRN_INT|IM_RCV_INT;
361 if (lp->version >= (CHIP_91100 << 4))
362 mask |= IM_MDINT;
Magnus Dammcfdfa862008-02-22 19:55:05 +0900363 SMC_SELECT_BANK(lp, 2);
364 SMC_SET_INT_MASK(lp, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 /*
367 * From this point the register bank must _NOT_ be switched away
368 * to something else than bank 2 without proper locking against
369 * races with any tasklet or interrupt handlers until smc_shutdown()
370 * or smc_reset() is called.
371 */
372}
373
374/*
375 * this puts the device in an inactive state
376 */
377static void smc_shutdown(struct net_device *dev)
378{
379 struct smc_local *lp = netdev_priv(dev);
380 void __iomem *ioaddr = lp->base;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400381 struct sk_buff *pending_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ben Boeckel6389aa42013-11-01 08:53:31 -0400383 DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* no more interrupts for me */
Russell King76cb4fe2006-08-14 23:00:20 -0700386 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900387 SMC_SELECT_BANK(lp, 2);
388 SMC_SET_INT_MASK(lp, 0);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400389 pending_skb = lp->pending_tx_skb;
390 lp->pending_tx_skb = NULL;
Russell King76cb4fe2006-08-14 23:00:20 -0700391 spin_unlock_irq(&lp->lock);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400392 if (pending_skb)
393 dev_kfree_skb(pending_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* and tell the card to stay away from that nasty outside world */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900396 SMC_SELECT_BANK(lp, 0);
397 SMC_SET_RCR(lp, RCR_CLEAR);
398 SMC_SET_TCR(lp, TCR_CLEAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400#ifdef POWER_DOWN
401 /* finally, shut the chip down */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900402 SMC_SELECT_BANK(lp, 1);
403 SMC_SET_CONFIG(lp, SMC_GET_CONFIG(lp) & ~CONFIG_EPH_POWER_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#endif
405}
406
407/*
408 * This is the procedure to handle the receipt of a packet.
409 */
410static inline void smc_rcv(struct net_device *dev)
411{
412 struct smc_local *lp = netdev_priv(dev);
413 void __iomem *ioaddr = lp->base;
414 unsigned int packet_number, status, packet_len;
415
Ben Boeckel6389aa42013-11-01 08:53:31 -0400416 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Magnus Dammcfdfa862008-02-22 19:55:05 +0900418 packet_number = SMC_GET_RXFIFO(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (unlikely(packet_number & RXFIFO_REMPTY)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400420 PRINTK(dev, "smc_rcv with nothing on FIFO.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return;
422 }
423
424 /* read from start of packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900425 SMC_SET_PTR(lp, PTR_READ | PTR_RCV | PTR_AUTOINC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* First two words are status and packet length */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900428 SMC_GET_PKT_HDR(lp, status, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 packet_len &= 0x07ff; /* mask off top bits */
Ben Boeckel6389aa42013-11-01 08:53:31 -0400430 DBG(2, dev, "RX PNR 0x%x STATUS 0x%04x LENGTH 0x%04x (%d)\n",
431 packet_number, status, packet_len, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 back:
434 if (unlikely(packet_len < 6 || status & RS_ERRORS)) {
435 if (status & RS_TOOLONG && packet_len <= (1514 + 4 + 6)) {
436 /* accept VLAN packets */
437 status &= ~RS_TOOLONG;
438 goto back;
439 }
440 if (packet_len < 6) {
441 /* bloody hardware */
Ben Boeckel6389aa42013-11-01 08:53:31 -0400442 netdev_err(dev, "fubar (rxlen %u status %x\n",
443 packet_len, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 status |= RS_TOOSHORT;
445 }
Magnus Dammcfdfa862008-02-22 19:55:05 +0900446 SMC_WAIT_MMU_BUSY(lp);
447 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700448 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (status & RS_ALGNERR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700450 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (status & (RS_TOOSHORT | RS_TOOLONG))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700452 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (status & RS_BADCRC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700454 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 } else {
456 struct sk_buff *skb;
457 unsigned char *data;
458 unsigned int data_len;
459
460 /* set multicast stats */
461 if (status & RS_MULTICAST)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700462 dev->stats.multicast++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * Actual payload is packet_len - 6 (or 5 if odd byte).
466 * We want skb_reserve(2) and the final ctrl word
467 * (2 bytes, possibly containing the payload odd byte).
468 * Furthermore, we add 2 bytes to allow rounding up to
469 * multiple of 4 bytes on 32 bit buses.
470 * Hence packet_len - 6 + 2 + 2 + 2.
471 */
Pradeep A. Dalvidae2e9f2012-02-06 11:16:13 +0000472 skb = netdev_alloc_skb(dev, packet_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (unlikely(skb == NULL)) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900474 SMC_WAIT_MMU_BUSY(lp);
475 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700476 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return;
478 }
479
480 /* Align IP header to 32 bits */
481 skb_reserve(skb, 2);
482
483 /* BUG: the LAN91C111 rev A never sets this bit. Force it. */
484 if (lp->version == 0x90)
485 status |= RS_ODDFRAME;
486
487 /*
488 * If odd length: packet_len - 5,
489 * otherwise packet_len - 6.
490 * With the trailing ctrl byte it's packet_len - 4.
491 */
492 data_len = packet_len - ((status & RS_ODDFRAME) ? 5 : 6);
493 data = skb_put(skb, data_len);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900494 SMC_PULL_DATA(lp, data, packet_len - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Magnus Dammcfdfa862008-02-22 19:55:05 +0900496 SMC_WAIT_MMU_BUSY(lp);
497 SMC_SET_MMU_CMD(lp, MC_RELEASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 PRINT_PKT(data, packet_len - 4);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 skb->protocol = eth_type_trans(skb, dev);
502 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700503 dev->stats.rx_packets++;
504 dev->stats.rx_bytes += data_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506}
507
508#ifdef CONFIG_SMP
509/*
510 * On SMP we have the following problem:
511 *
512 * A = smc_hardware_send_pkt()
513 * B = smc_hard_start_xmit()
514 * C = smc_interrupt()
515 *
516 * A and B can never be executed simultaneously. However, at least on UP,
517 * it is possible (and even desirable) for C to interrupt execution of
518 * A or B in order to have better RX reliability and avoid overruns.
519 * C, just like A and B, must have exclusive access to the chip and
520 * each of them must lock against any other concurrent access.
521 * Unfortunately this is not possible to have C suspend execution of A or
522 * B taking place on another CPU. On UP this is no an issue since A and B
523 * are run from softirq context and C from hard IRQ context, and there is
524 * no other CPU where concurrent access can happen.
525 * If ever there is a way to force at least B and C to always be executed
526 * on the same CPU then we could use read/write locks to protect against
527 * any other concurrent access and C would always interrupt B. But life
528 * isn't that easy in a SMP world...
529 */
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700530#define smc_special_trylock(lock, flags) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531({ \
532 int __ret; \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700533 local_irq_save(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 __ret = spin_trylock(lock); \
535 if (!__ret) \
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700536 local_irq_restore(flags); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 __ret; \
538})
Alexey Dobriyan0b4f2922009-08-26 12:03:35 -0700539#define smc_special_lock(lock, flags) spin_lock_irqsave(lock, flags)
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700540#define smc_special_unlock(lock, flags) spin_unlock_irqrestore(lock, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#else
Mike Frysingerfd0775b2009-12-09 03:40:04 +0000542#define smc_special_trylock(lock, flags) (flags == flags)
543#define smc_special_lock(lock, flags) do { flags = 0; } while (0)
544#define smc_special_unlock(lock, flags) do { flags = 0; } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#endif
546
547/*
548 * This is called to actually send a packet to the chip.
549 */
550static void smc_hardware_send_pkt(unsigned long data)
551{
552 struct net_device *dev = (struct net_device *)data;
553 struct smc_local *lp = netdev_priv(dev);
554 void __iomem *ioaddr = lp->base;
555 struct sk_buff *skb;
556 unsigned int packet_no, len;
557 unsigned char *buf;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700558 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Ben Boeckel6389aa42013-11-01 08:53:31 -0400560 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700562 if (!smc_special_trylock(&lp->lock, flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 netif_stop_queue(dev);
564 tasklet_schedule(&lp->tx_task);
565 return;
566 }
567
568 skb = lp->pending_tx_skb;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400569 if (unlikely(!skb)) {
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700570 smc_special_unlock(&lp->lock, flags);
Nicolas Pitrebe836682005-06-19 23:56:21 -0400571 return;
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 lp->pending_tx_skb = NULL;
Nicolas Pitrebe836682005-06-19 23:56:21 -0400574
Magnus Dammcfdfa862008-02-22 19:55:05 +0900575 packet_no = SMC_GET_AR(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (unlikely(packet_no & AR_FAILED)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400577 netdev_err(dev, "Memory allocation failed.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700578 dev->stats.tx_errors++;
579 dev->stats.tx_fifo_errors++;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700580 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 goto done;
582 }
583
584 /* point to the beginning of the packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900585 SMC_SET_PN(lp, packet_no);
586 SMC_SET_PTR(lp, PTR_AUTOINC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 buf = skb->data;
589 len = skb->len;
Ben Boeckel6389aa42013-11-01 08:53:31 -0400590 DBG(2, dev, "TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",
591 packet_no, len, len, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 PRINT_PKT(buf, len);
593
594 /*
595 * Send the packet length (+6 for status words, length, and ctl.
596 * The card will pad to 64 bytes with zeroes if packet is too small.
597 */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900598 SMC_PUT_PKT_HDR(lp, 0, len + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* send the actual data */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900601 SMC_PUSH_DATA(lp, buf, len & ~1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Send final ctl word with the last byte if there is one */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900604 SMC_outw(((len & 1) ? (0x2000 | buf[len-1]) : 0), ioaddr, DATA_REG(lp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 /*
Nicolas Pitreea937562005-04-12 16:26:40 -0400607 * If THROTTLE_TX_PKTS is set, we stop the queue here. This will
608 * have the effect of having at most one packet queued for TX
609 * in the chip's memory at all time.
610 *
611 * If THROTTLE_TX_PKTS is not set then the queue is stopped only
612 * when memory allocation (MC_ALLOC) does not succeed right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Nicolas Pitreea937562005-04-12 16:26:40 -0400614 if (THROTTLE_TX_PKTS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 netif_stop_queue(dev);
616
617 /* queue the packet for TX */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900618 SMC_SET_MMU_CMD(lp, MC_ENQUEUE);
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700619 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700622 dev->stats.tx_packets++;
623 dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Magnus Dammcfdfa862008-02-22 19:55:05 +0900625 SMC_ENABLE_INT(lp, IM_TX_INT | IM_TX_EMPTY_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627done: if (!THROTTLE_TX_PKTS)
628 netif_wake_queue(dev);
629
Eric W. Biederman4b61fe22014-03-15 18:08:04 -0700630 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
634 * Since I am not sure if I will have enough room in the chip's ram
635 * to store the packet, I call this routine which either sends it
636 * now, or set the card to generates an interrupt when ready
637 * for the packet.
638 */
639static int smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
640{
641 struct smc_local *lp = netdev_priv(dev);
642 void __iomem *ioaddr = lp->base;
643 unsigned int numPages, poll_count, status;
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700644 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ben Boeckel6389aa42013-11-01 08:53:31 -0400646 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 BUG_ON(lp->pending_tx_skb != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /*
651 * The MMU wants the number of pages to be the number of 256 bytes
652 * 'pages', minus 1 (since a packet can't ever have 0 pages :))
653 *
654 * The 91C111 ignores the size bits, but earlier models don't.
655 *
656 * Pkt size for allocating is data length +6 (for additional status
657 * words, length and ctl)
658 *
659 * If odd size then last byte is included in ctl word.
660 */
661 numPages = ((skb->len & ~1) + (6 - 1)) >> 8;
662 if (unlikely(numPages > 7)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400663 netdev_warn(dev, "Far too big packet error.\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700664 dev->stats.tx_errors++;
665 dev->stats.tx_dropped++;
Eric W. Biederman4b61fe22014-03-15 18:08:04 -0700666 dev_kfree_skb_any(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000667 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700670 smc_special_lock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 /* now, try to allocate the memory */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900673 SMC_SET_MMU_CMD(lp, MC_ALLOC | numPages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /*
676 * Poll the chip for a short amount of time in case the
677 * allocation succeeds quickly.
678 */
679 poll_count = MEMORY_WAIT_TIME;
680 do {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900681 status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (status & IM_ALLOC_INT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900683 SMC_ACK_INT(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685 }
686 } while (--poll_count);
687
Dongdong Deng8ff499e2009-08-23 22:59:04 -0700688 smc_special_unlock(&lp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Nicolas Pitrebe836682005-06-19 23:56:21 -0400690 lp->pending_tx_skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!poll_count) {
692 /* oh well, wait until the chip finds memory later */
693 netif_stop_queue(dev);
Ben Boeckel6389aa42013-11-01 08:53:31 -0400694 DBG(2, dev, "TX memory allocation deferred.\n");
Magnus Dammcfdfa862008-02-22 19:55:05 +0900695 SMC_ENABLE_INT(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 } else {
697 /*
698 * Allocation succeeded: push packet to the chip's own memory
699 * immediately.
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 smc_hardware_send_pkt((unsigned long)dev);
702 }
703
Patrick McHardy6ed10652009-06-23 06:03:08 +0000704 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/*
708 * This handles a TX interrupt, which is only called when:
709 * - a TX error occurred, or
710 * - CTL_AUTO_RELEASE is not set and TX of a packet completed.
711 */
712static void smc_tx(struct net_device *dev)
713{
714 struct smc_local *lp = netdev_priv(dev);
715 void __iomem *ioaddr = lp->base;
716 unsigned int saved_packet, packet_no, tx_status, pkt_len;
717
Ben Boeckel6389aa42013-11-01 08:53:31 -0400718 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* If the TX FIFO is empty then nothing to do */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900721 packet_no = SMC_GET_TXFIFO(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (unlikely(packet_no & TXFIFO_TEMPTY)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400723 PRINTK(dev, "smc_tx with nothing on FIFO.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return;
725 }
726
727 /* select packet to read from */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900728 saved_packet = SMC_GET_PN(lp);
729 SMC_SET_PN(lp, packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* read the first word (status word) from this packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900732 SMC_SET_PTR(lp, PTR_AUTOINC | PTR_READ);
733 SMC_GET_PKT_HDR(lp, tx_status, pkt_len);
Ben Boeckel6389aa42013-11-01 08:53:31 -0400734 DBG(2, dev, "TX STATUS 0x%04x PNR 0x%02x\n",
735 tx_status, packet_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Nicolas Pitre8de90112005-04-12 16:21:11 -0400737 if (!(tx_status & ES_TX_SUC))
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700738 dev->stats.tx_errors++;
Nicolas Pitre8de90112005-04-12 16:21:11 -0400739
740 if (tx_status & ES_LOSTCARR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700741 dev->stats.tx_carrier_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Nicolas Pitre8de90112005-04-12 16:21:11 -0400743 if (tx_status & (ES_LATCOL | ES_16COL)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400744 PRINTK(dev, "%s occurred on last xmit\n",
Nicolas Pitre8de90112005-04-12 16:21:11 -0400745 (tx_status & ES_LATCOL) ?
746 "late collision" : "too many collisions");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700747 dev->stats.tx_window_errors++;
748 if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {
Ben Boeckel6389aa42013-11-01 08:53:31 -0400749 netdev_info(dev, "unexpectedly large number of bad collisions. Please check duplex setting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751 }
752
753 /* kill the packet */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900754 SMC_WAIT_MMU_BUSY(lp);
755 SMC_SET_MMU_CMD(lp, MC_FREEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* Don't restore Packet Number Reg until busy bit is cleared */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900758 SMC_WAIT_MMU_BUSY(lp);
759 SMC_SET_PN(lp, saved_packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* re-enable transmit */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900762 SMC_SELECT_BANK(lp, 0);
763 SMC_SET_TCR(lp, lp->tcr_cur_mode);
764 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767
768/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
769
770static void smc_mii_out(struct net_device *dev, unsigned int val, int bits)
771{
772 struct smc_local *lp = netdev_priv(dev);
773 void __iomem *ioaddr = lp->base;
774 unsigned int mii_reg, mask;
775
Magnus Dammcfdfa862008-02-22 19:55:05 +0900776 mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mii_reg |= MII_MDOE;
778
779 for (mask = 1 << (bits - 1); mask; mask >>= 1) {
780 if (val & mask)
781 mii_reg |= MII_MDO;
782 else
783 mii_reg &= ~MII_MDO;
784
Magnus Dammcfdfa862008-02-22 19:55:05 +0900785 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 udelay(MII_DELAY);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900787 SMC_SET_MII(lp, mii_reg | MII_MCLK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 udelay(MII_DELAY);
789 }
790}
791
792static unsigned int smc_mii_in(struct net_device *dev, int bits)
793{
794 struct smc_local *lp = netdev_priv(dev);
795 void __iomem *ioaddr = lp->base;
796 unsigned int mii_reg, mask, val;
797
Magnus Dammcfdfa862008-02-22 19:55:05 +0900798 mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
799 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {
Magnus Dammcfdfa862008-02-22 19:55:05 +0900802 if (SMC_GET_MII(lp) & MII_MDI)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 val |= mask;
804
Magnus Dammcfdfa862008-02-22 19:55:05 +0900805 SMC_SET_MII(lp, mii_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 udelay(MII_DELAY);
Magnus Dammcfdfa862008-02-22 19:55:05 +0900807 SMC_SET_MII(lp, mii_reg | MII_MCLK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 udelay(MII_DELAY);
809 }
810
811 return val;
812}
813
814/*
815 * Reads a register from the MII Management serial interface
816 */
817static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg)
818{
819 struct smc_local *lp = netdev_priv(dev);
820 void __iomem *ioaddr = lp->base;
821 unsigned int phydata;
822
Magnus Dammcfdfa862008-02-22 19:55:05 +0900823 SMC_SELECT_BANK(lp, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 /* Idle - 32 ones */
826 smc_mii_out(dev, 0xffffffff, 32);
827
828 /* Start code (01) + read (10) + phyaddr + phyreg */
829 smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);
830
831 /* Turnaround (2bits) + phydata */
832 phydata = smc_mii_in(dev, 18);
833
834 /* Return to idle state */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900835 SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Ben Boeckel6389aa42013-11-01 08:53:31 -0400837 DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
838 __func__, phyaddr, phyreg, phydata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Magnus Dammcfdfa862008-02-22 19:55:05 +0900840 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return phydata;
842}
843
844/*
845 * Writes a register to the MII Management serial interface
846 */
847static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,
848 int phydata)
849{
850 struct smc_local *lp = netdev_priv(dev);
851 void __iomem *ioaddr = lp->base;
852
Magnus Dammcfdfa862008-02-22 19:55:05 +0900853 SMC_SELECT_BANK(lp, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Idle - 32 ones */
856 smc_mii_out(dev, 0xffffffff, 32);
857
858 /* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */
859 smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);
860
861 /* Return to idle state */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900862 SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Ben Boeckel6389aa42013-11-01 08:53:31 -0400864 DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
865 __func__, phyaddr, phyreg, phydata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Magnus Dammcfdfa862008-02-22 19:55:05 +0900867 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870/*
871 * Finds and reports the PHY address
872 */
873static void smc_phy_detect(struct net_device *dev)
874{
875 struct smc_local *lp = netdev_priv(dev);
876 int phyaddr;
877
Ben Boeckel6389aa42013-11-01 08:53:31 -0400878 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 lp->phy_type = 0;
881
882 /*
883 * Scan all 32 PHY addresses if necessary, starting at
884 * PHY#1 to PHY#31, and then PHY#0 last.
885 */
886 for (phyaddr = 1; phyaddr < 33; ++phyaddr) {
887 unsigned int id1, id2;
888
889 /* Read the PHY identifiers */
890 id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);
891 id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);
892
Ben Boeckel6389aa42013-11-01 08:53:31 -0400893 DBG(3, dev, "phy_id1=0x%x, phy_id2=0x%x\n",
894 id1, id2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /* Make sure it is a valid identifier */
897 if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&
898 id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {
899 /* Save the PHY's address */
900 lp->mii.phy_id = phyaddr & 31;
901 lp->phy_type = id1 << 16 | id2;
902 break;
903 }
904 }
905}
906
907/*
908 * Sets the PHY to a configuration as determined by the user
909 */
910static int smc_phy_fixed(struct net_device *dev)
911{
912 struct smc_local *lp = netdev_priv(dev);
913 void __iomem *ioaddr = lp->base;
914 int phyaddr = lp->mii.phy_id;
915 int bmcr, cfg1;
916
Ben Boeckel6389aa42013-11-01 08:53:31 -0400917 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* Enter Link Disable state */
920 cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);
921 cfg1 |= PHY_CFG1_LNKDIS;
922 smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);
923
924 /*
925 * Set our fixed capabilities
926 * Disable auto-negotiation
927 */
928 bmcr = 0;
929
930 if (lp->ctl_rfduplx)
931 bmcr |= BMCR_FULLDPLX;
932
933 if (lp->ctl_rspeed == 100)
934 bmcr |= BMCR_SPEED100;
935
936 /* Write our capabilities to the phy control register */
937 smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);
938
939 /* Re-Configure the Receive/Phy Control register */
Magnus Dammcfdfa862008-02-22 19:55:05 +0900940 SMC_SELECT_BANK(lp, 0);
941 SMC_SET_RPC(lp, lp->rpc_cur_mode);
942 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 return 1;
945}
946
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000947/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * smc_phy_reset - reset the phy
949 * @dev: net device
950 * @phy: phy address
951 *
952 * Issue a software reset for the specified PHY and
953 * wait up to 100ms for the reset to complete. We should
954 * not access the PHY for 50ms after issuing the reset.
955 *
956 * The time to wait appears to be dependent on the PHY.
957 *
958 * Must be called with lp->lock locked.
959 */
960static int smc_phy_reset(struct net_device *dev, int phy)
961{
962 struct smc_local *lp = netdev_priv(dev);
963 unsigned int bmcr;
964 int timeout;
965
966 smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);
967
968 for (timeout = 2; timeout; timeout--) {
969 spin_unlock_irq(&lp->lock);
970 msleep(50);
971 spin_lock_irq(&lp->lock);
972
973 bmcr = smc_phy_read(dev, phy, MII_BMCR);
974 if (!(bmcr & BMCR_RESET))
975 break;
976 }
977
978 return bmcr & BMCR_RESET;
979}
980
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000981/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * smc_phy_powerdown - powerdown phy
983 * @dev: net device
984 *
985 * Power down the specified PHY
986 */
987static void smc_phy_powerdown(struct net_device *dev)
988{
989 struct smc_local *lp = netdev_priv(dev);
990 unsigned int bmcr;
991 int phy = lp->mii.phy_id;
992
993 if (lp->phy_type == 0)
994 return;
995
996 /* We need to ensure that no calls to smc_phy_configure are
997 pending.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
David S. Miller4bb073c2008-06-12 02:22:02 -0700999 cancel_work_sync(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 bmcr = smc_phy_read(dev, phy, MII_BMCR);
1002 smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);
1003}
1004
Ben Hutchings49ce9c22012-07-10 10:56:00 +00001005/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * smc_phy_check_media - check the media status and adjust TCR
1007 * @dev: net device
1008 * @init: set true for initialisation
1009 *
1010 * Select duplex mode depending on negotiation state. This
1011 * also updates our carrier state.
1012 */
1013static void smc_phy_check_media(struct net_device *dev, int init)
1014{
1015 struct smc_local *lp = netdev_priv(dev);
1016 void __iomem *ioaddr = lp->base;
1017
1018 if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
1019 /* duplex state has changed */
1020 if (lp->mii.full_duplex) {
1021 lp->tcr_cur_mode |= TCR_SWFDUP;
1022 } else {
1023 lp->tcr_cur_mode &= ~TCR_SWFDUP;
1024 }
1025
Magnus Dammcfdfa862008-02-22 19:55:05 +09001026 SMC_SELECT_BANK(lp, 0);
1027 SMC_SET_TCR(lp, lp->tcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029}
1030
1031/*
1032 * Configures the specified PHY through the MII management interface
1033 * using Autonegotiation.
1034 * Calls smc_phy_fixed() if the user has requested a certain config.
1035 * If RPC ANEG bit is set, the media selection is dependent purely on
1036 * the selection by the MII (either in the MII BMCR reg or the result
1037 * of autonegotiation.) If the RPC ANEG bit is cleared, the selection
1038 * is controlled by the RPC SPEED and RPC DPLX bits.
1039 */
David Howells6d5aefb2006-12-05 19:36:26 +00001040static void smc_phy_configure(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
David Howells6d5aefb2006-12-05 19:36:26 +00001042 struct smc_local *lp =
1043 container_of(work, struct smc_local, phy_configure);
1044 struct net_device *dev = lp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 void __iomem *ioaddr = lp->base;
1046 int phyaddr = lp->mii.phy_id;
1047 int my_phy_caps; /* My PHY capabilities */
1048 int my_ad_caps; /* My Advertised capabilities */
1049 int status;
1050
Ben Boeckel6389aa42013-11-01 08:53:31 -04001051 DBG(3, dev, "smc_program_phy()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 spin_lock_irq(&lp->lock);
1054
1055 /*
1056 * We should not be called if phy_type is zero.
1057 */
1058 if (lp->phy_type == 0)
1059 goto smc_phy_configure_exit;
1060
1061 if (smc_phy_reset(dev, phyaddr)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001062 netdev_info(dev, "PHY reset timed out\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 goto smc_phy_configure_exit;
1064 }
1065
1066 /*
1067 * Enable PHY Interrupts (for register 18)
1068 * Interrupts listed here are disabled
1069 */
1070 smc_phy_write(dev, phyaddr, PHY_MASK_REG,
1071 PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
1072 PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
1073 PHY_INT_SPDDET | PHY_INT_DPLXDET);
1074
1075 /* Configure the Receive/Phy Control register */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001076 SMC_SELECT_BANK(lp, 0);
1077 SMC_SET_RPC(lp, lp->rpc_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* If the user requested no auto neg, then go set his request */
1080 if (lp->mii.force_media) {
1081 smc_phy_fixed(dev);
1082 goto smc_phy_configure_exit;
1083 }
1084
1085 /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
1086 my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);
1087
1088 if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001089 netdev_info(dev, "Auto negotiation NOT supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 smc_phy_fixed(dev);
1091 goto smc_phy_configure_exit;
1092 }
1093
1094 my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */
1095
1096 if (my_phy_caps & BMSR_100BASE4)
1097 my_ad_caps |= ADVERTISE_100BASE4;
1098 if (my_phy_caps & BMSR_100FULL)
1099 my_ad_caps |= ADVERTISE_100FULL;
1100 if (my_phy_caps & BMSR_100HALF)
1101 my_ad_caps |= ADVERTISE_100HALF;
1102 if (my_phy_caps & BMSR_10FULL)
1103 my_ad_caps |= ADVERTISE_10FULL;
1104 if (my_phy_caps & BMSR_10HALF)
1105 my_ad_caps |= ADVERTISE_10HALF;
1106
1107 /* Disable capabilities not selected by our user */
1108 if (lp->ctl_rspeed != 100)
1109 my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
1110
1111 if (!lp->ctl_rfduplx)
1112 my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
1113
1114 /* Update our Auto-Neg Advertisement Register */
1115 smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);
1116 lp->mii.advertising = my_ad_caps;
1117
1118 /*
1119 * Read the register back. Without this, it appears that when
1120 * auto-negotiation is restarted, sometimes it isn't ready and
1121 * the link does not come up.
1122 */
1123 status = smc_phy_read(dev, phyaddr, MII_ADVERTISE);
1124
Ben Boeckel6389aa42013-11-01 08:53:31 -04001125 DBG(2, dev, "phy caps=%x\n", my_phy_caps);
1126 DBG(2, dev, "phy advertised caps=%x\n", my_ad_caps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* Restart auto-negotiation process in order to advertise my caps */
1129 smc_phy_write(dev, phyaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
1130
1131 smc_phy_check_media(dev, 1);
1132
1133smc_phy_configure_exit:
Magnus Dammcfdfa862008-02-22 19:55:05 +09001134 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 spin_unlock_irq(&lp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
1138/*
1139 * smc_phy_interrupt
1140 *
1141 * Purpose: Handle interrupts relating to PHY register 18. This is
1142 * called from the "hard" interrupt handler under our private spinlock.
1143 */
1144static void smc_phy_interrupt(struct net_device *dev)
1145{
1146 struct smc_local *lp = netdev_priv(dev);
1147 int phyaddr = lp->mii.phy_id;
1148 int phy18;
1149
Ben Boeckel6389aa42013-11-01 08:53:31 -04001150 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (lp->phy_type == 0)
1153 return;
1154
1155 for(;;) {
1156 smc_phy_check_media(dev, 0);
1157
1158 /* Read PHY Register 18, Status Output */
1159 phy18 = smc_phy_read(dev, phyaddr, PHY_INT_REG);
1160 if ((phy18 & PHY_INT_INT) == 0)
1161 break;
1162 }
1163}
1164
1165/*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
1166
1167static void smc_10bt_check_media(struct net_device *dev, int init)
1168{
1169 struct smc_local *lp = netdev_priv(dev);
1170 void __iomem *ioaddr = lp->base;
1171 unsigned int old_carrier, new_carrier;
1172
1173 old_carrier = netif_carrier_ok(dev) ? 1 : 0;
1174
Magnus Dammcfdfa862008-02-22 19:55:05 +09001175 SMC_SELECT_BANK(lp, 0);
1176 new_carrier = (SMC_GET_EPH_STATUS(lp) & ES_LINK_OK) ? 1 : 0;
1177 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 if (init || (old_carrier != new_carrier)) {
1180 if (!new_carrier) {
1181 netif_carrier_off(dev);
1182 } else {
1183 netif_carrier_on(dev);
1184 }
1185 if (netif_msg_link(lp))
Ben Boeckel6389aa42013-11-01 08:53:31 -04001186 netdev_info(dev, "link %s\n",
1187 new_carrier ? "up" : "down");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189}
1190
1191static void smc_eph_interrupt(struct net_device *dev)
1192{
1193 struct smc_local *lp = netdev_priv(dev);
1194 void __iomem *ioaddr = lp->base;
1195 unsigned int ctl;
1196
1197 smc_10bt_check_media(dev, 0);
1198
Magnus Dammcfdfa862008-02-22 19:55:05 +09001199 SMC_SELECT_BANK(lp, 1);
1200 ctl = SMC_GET_CTL(lp);
1201 SMC_SET_CTL(lp, ctl & ~CTL_LE_ENABLE);
1202 SMC_SET_CTL(lp, ctl);
1203 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206/*
1207 * This is the main routine of the driver, to handle the device when
1208 * it needs some attention.
1209 */
David Howells7d12e782006-10-05 14:55:46 +01001210static irqreturn_t smc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct net_device *dev = dev_id;
1213 struct smc_local *lp = netdev_priv(dev);
1214 void __iomem *ioaddr = lp->base;
1215 int status, mask, timeout, card_stats;
1216 int saved_pointer;
1217
Ben Boeckel6389aa42013-11-01 08:53:31 -04001218 DBG(3, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 spin_lock(&lp->lock);
1221
1222 /* A preamble may be used when there is a potential race
1223 * between the interruptible transmit functions and this
1224 * ISR. */
1225 SMC_INTERRUPT_PREAMBLE;
1226
Magnus Dammcfdfa862008-02-22 19:55:05 +09001227 saved_pointer = SMC_GET_PTR(lp);
1228 mask = SMC_GET_INT_MASK(lp);
1229 SMC_SET_INT_MASK(lp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* set a timeout value, so I don't stay here forever */
Nicolas Pitre5d0571d2005-11-17 14:02:48 -05001232 timeout = MAX_IRQ_LOOPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 do {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001235 status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Ben Boeckel6389aa42013-11-01 08:53:31 -04001237 DBG(2, dev, "INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x\n",
1238 status, mask,
1239 ({ int meminfo; SMC_SELECT_BANK(lp, 0);
1240 meminfo = SMC_GET_MIR(lp);
1241 SMC_SELECT_BANK(lp, 2); meminfo; }),
1242 SMC_GET_FIFO(lp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 status &= mask;
1245 if (!status)
1246 break;
1247
Nicolas Pitreea937562005-04-12 16:26:40 -04001248 if (status & IM_TX_INT) {
1249 /* do this before RX as it will free memory quickly */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001250 DBG(3, dev, "TX int\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 smc_tx(dev);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001252 SMC_ACK_INT(lp, IM_TX_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (THROTTLE_TX_PKTS)
1254 netif_wake_queue(dev);
Nicolas Pitreea937562005-04-12 16:26:40 -04001255 } else if (status & IM_RCV_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001256 DBG(3, dev, "RX irq\n");
Nicolas Pitreea937562005-04-12 16:26:40 -04001257 smc_rcv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 } else if (status & IM_ALLOC_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001259 DBG(3, dev, "Allocation irq\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 tasklet_hi_schedule(&lp->tx_task);
1261 mask &= ~IM_ALLOC_INT;
1262 } else if (status & IM_TX_EMPTY_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001263 DBG(3, dev, "TX empty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 mask &= ~IM_TX_EMPTY_INT;
1265
1266 /* update stats */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001267 SMC_SELECT_BANK(lp, 0);
1268 card_stats = SMC_GET_COUNTER(lp);
1269 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /* single collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001272 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 card_stats >>= 4;
1274
1275 /* multiple collisions */
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001276 dev->stats.collisions += card_stats & 0xF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 } else if (status & IM_RX_OVRN_INT) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001278 DBG(1, dev, "RX overrun (EPH_ST 0x%04x)\n",
1279 ({ int eph_st; SMC_SELECT_BANK(lp, 0);
1280 eph_st = SMC_GET_EPH_STATUS(lp);
1281 SMC_SELECT_BANK(lp, 2); eph_st; }));
Magnus Dammcfdfa862008-02-22 19:55:05 +09001282 SMC_ACK_INT(lp, IM_RX_OVRN_INT);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001283 dev->stats.rx_errors++;
1284 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } else if (status & IM_EPH_INT) {
1286 smc_eph_interrupt(dev);
1287 } else if (status & IM_MDINT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001288 SMC_ACK_INT(lp, IM_MDINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 smc_phy_interrupt(dev);
1290 } else if (status & IM_ERCV_INT) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001291 SMC_ACK_INT(lp, IM_ERCV_INT);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001292 PRINTK(dev, "UNSUPPORTED: ERCV INTERRUPT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 } while (--timeout);
1295
1296 /* restore register states */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001297 SMC_SET_PTR(lp, saved_pointer);
1298 SMC_SET_INT_MASK(lp, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 spin_unlock(&lp->lock);
1300
Sonic Zhangc500cb22008-04-10 21:30:37 -07001301#ifndef CONFIG_NET_POLL_CONTROLLER
Nicolas Pitre5d0571d2005-11-17 14:02:48 -05001302 if (timeout == MAX_IRQ_LOOPS)
Ben Boeckel6389aa42013-11-01 08:53:31 -04001303 PRINTK(dev, "spurious interrupt (mask = 0x%02x)\n",
1304 mask);
Sonic Zhangc500cb22008-04-10 21:30:37 -07001305#endif
Ben Boeckel6389aa42013-11-01 08:53:31 -04001306 DBG(3, dev, "Interrupt done (%d loops)\n",
1307 MAX_IRQ_LOOPS - timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
1310 * We return IRQ_HANDLED unconditionally here even if there was
1311 * nothing to do. There is a possibility that a packet might
1312 * get enqueued into the chip right after TX_EMPTY_INT is raised
1313 * but just before the CPU acknowledges the IRQ.
1314 * Better take an unneeded IRQ in some occasions than complexifying
1315 * the code for all cases.
1316 */
1317 return IRQ_HANDLED;
1318}
1319
1320#ifdef CONFIG_NET_POLL_CONTROLLER
1321/*
1322 * Polling receive - used by netconsole and other diagnostic tools
1323 * to allow network i/o with interrupts disabled.
1324 */
1325static void smc_poll_controller(struct net_device *dev)
1326{
1327 disable_irq(dev->irq);
Al Viro9c8e7f52006-10-07 16:29:18 +01001328 smc_interrupt(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 enable_irq(dev->irq);
1330}
1331#endif
1332
1333/* Our watchdog timed out. Called by the networking layer */
1334static void smc_timeout(struct net_device *dev)
1335{
1336 struct smc_local *lp = netdev_priv(dev);
1337 void __iomem *ioaddr = lp->base;
Nicolas Pitre8de90112005-04-12 16:21:11 -04001338 int status, mask, eph_st, meminfo, fifo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Ben Boeckel6389aa42013-11-01 08:53:31 -04001340 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001343 status = SMC_GET_INT(lp);
1344 mask = SMC_GET_INT_MASK(lp);
1345 fifo = SMC_GET_FIFO(lp);
1346 SMC_SELECT_BANK(lp, 0);
1347 eph_st = SMC_GET_EPH_STATUS(lp);
1348 meminfo = SMC_GET_MIR(lp);
1349 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 spin_unlock_irq(&lp->lock);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001351 PRINTK(dev, "TX timeout (INT 0x%02x INTMASK 0x%02x MEM 0x%04x FIFO 0x%04x EPH_ST 0x%04x)\n",
1352 status, mask, meminfo, fifo, eph_st);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 smc_reset(dev);
1355 smc_enable(dev);
1356
1357 /*
1358 * Reconfiguring the PHY doesn't seem like a bad idea here, but
1359 * smc_phy_configure() calls msleep() which calls schedule_timeout()
1360 * which calls schedule(). Hence we use a work queue.
1361 */
David S. Miller4bb073c2008-06-12 02:22:02 -07001362 if (lp->phy_type != 0)
1363 schedule_work(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* We can accept TX packets again */
Eric Dumazet1ae5dc32010-05-10 05:01:31 -07001366 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 netif_wake_queue(dev);
1368}
1369
1370/*
1371 * This routine will, depending on the values passed to it,
1372 * either make it accept multicast packets, go into
1373 * promiscuous mode (for TCPDUMP and cousins) or accept
1374 * a select set of multicast packets
1375 */
1376static void smc_set_multicast_list(struct net_device *dev)
1377{
1378 struct smc_local *lp = netdev_priv(dev);
1379 void __iomem *ioaddr = lp->base;
1380 unsigned char multicast_table[8];
1381 int update_multicast = 0;
1382
Ben Boeckel6389aa42013-11-01 08:53:31 -04001383 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 if (dev->flags & IFF_PROMISC) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001386 DBG(2, dev, "RCR_PRMS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 lp->rcr_cur_mode |= RCR_PRMS;
1388 }
1389
1390/* BUG? I never disable promiscuous mode if multicasting was turned on.
1391 Now, I turn off promiscuous mode, but I don't do anything to multicasting
1392 when promiscuous mode is turned on.
1393*/
1394
1395 /*
1396 * Here, I am setting this to accept all multicast packets.
1397 * I don't need to zero the multicast table, because the flag is
1398 * checked before the table is
1399 */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001400 else if (dev->flags & IFF_ALLMULTI || netdev_mc_count(dev) > 16) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001401 DBG(2, dev, "RCR_ALMUL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 lp->rcr_cur_mode |= RCR_ALMUL;
1403 }
1404
1405 /*
1406 * This sets the internal hardware table to filter out unwanted
1407 * multicast packets before they take up memory.
1408 *
1409 * The SMC chip uses a hash table where the high 6 bits of the CRC of
1410 * address are the offset into the table. If that bit is 1, then the
1411 * multicast packet is accepted. Otherwise, it's dropped silently.
1412 *
1413 * To use the 6 bits as an offset into the table, the high 3 bits are
1414 * the number of the 8 bit register, while the low 3 bits are the bit
1415 * within that register.
1416 */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001417 else if (!netdev_mc_empty(dev)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001418 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /* table for flipping the order of 3 bits */
1421 static const unsigned char invert3[] = {0, 4, 2, 6, 1, 5, 3, 7};
1422
1423 /* start with a table of all zeros: reject all */
1424 memset(multicast_table, 0, sizeof(multicast_table));
1425
Jiri Pirko22bedad32010-04-01 21:22:57 +00001426 netdev_for_each_mc_addr(ha, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int position;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* only use the low order bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00001430 position = crc32_le(~0, ha->addr, 6) & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /* do some messy swapping to put the bit in the right spot */
1433 multicast_table[invert3[position&7]] |=
1434 (1<<invert3[(position>>3)&7]);
1435 }
1436
1437 /* be sure I get rid of flags I might have set */
1438 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1439
1440 /* now, the table can be loaded into the chipset */
1441 update_multicast = 1;
1442 } else {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001443 DBG(2, dev, "~(RCR_PRMS|RCR_ALMUL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1445
1446 /*
1447 * since I'm disabling all multicast entirely, I need to
1448 * clear the multicast list
1449 */
1450 memset(multicast_table, 0, sizeof(multicast_table));
1451 update_multicast = 1;
1452 }
1453
1454 spin_lock_irq(&lp->lock);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001455 SMC_SELECT_BANK(lp, 0);
1456 SMC_SET_RCR(lp, lp->rcr_cur_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (update_multicast) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001458 SMC_SELECT_BANK(lp, 3);
1459 SMC_SET_MCAST(lp, multicast_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Magnus Dammcfdfa862008-02-22 19:55:05 +09001461 SMC_SELECT_BANK(lp, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 spin_unlock_irq(&lp->lock);
1463}
1464
1465
1466/*
1467 * Open and Initialize the board
1468 *
1469 * Set up everything, reset the card, etc..
1470 */
1471static int
1472smc_open(struct net_device *dev)
1473{
1474 struct smc_local *lp = netdev_priv(dev);
1475
Ben Boeckel6389aa42013-11-01 08:53:31 -04001476 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Setup the default Register Modes */
1479 lp->tcr_cur_mode = TCR_DEFAULT;
1480 lp->rcr_cur_mode = RCR_DEFAULT;
Russell Kingb0dbcf52008-09-04 21:13:37 +01001481 lp->rpc_cur_mode = RPC_DEFAULT |
1482 lp->cfg.leda << RPC_LSXA_SHFT |
1483 lp->cfg.ledb << RPC_LSXB_SHFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /*
1486 * If we are not using a MII interface, we need to
1487 * monitor our own carrier signal to detect faults.
1488 */
1489 if (lp->phy_type == 0)
1490 lp->tcr_cur_mode |= TCR_MON_CSN;
1491
1492 /* reset the hardware */
1493 smc_reset(dev);
1494 smc_enable(dev);
1495
1496 /* Configure the PHY, initialize the link state */
1497 if (lp->phy_type != 0)
David Howells6d5aefb2006-12-05 19:36:26 +00001498 smc_phy_configure(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 else {
1500 spin_lock_irq(&lp->lock);
1501 smc_10bt_check_media(dev, 1);
1502 spin_unlock_irq(&lp->lock);
1503 }
1504
1505 netif_start_queue(dev);
1506 return 0;
1507}
1508
1509/*
1510 * smc_close
1511 *
1512 * this makes the board clean up everything that it can
1513 * and not talk to the outside world. Caused by
1514 * an 'ifconfig ethX down'
1515 */
1516static int smc_close(struct net_device *dev)
1517{
1518 struct smc_local *lp = netdev_priv(dev);
1519
Ben Boeckel6389aa42013-11-01 08:53:31 -04001520 DBG(2, dev, "%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 netif_stop_queue(dev);
1523 netif_carrier_off(dev);
1524
1525 /* clear everything */
1526 smc_shutdown(dev);
Nicolas Pitrebe836682005-06-19 23:56:21 -04001527 tasklet_kill(&lp->tx_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 smc_phy_powerdown(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530}
1531
1532/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * Ethtool support
1534 */
1535static int
1536smc_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1537{
1538 struct smc_local *lp = netdev_priv(dev);
1539 int ret;
1540
1541 cmd->maxtxpkt = 1;
1542 cmd->maxrxpkt = 1;
1543
1544 if (lp->phy_type != 0) {
1545 spin_lock_irq(&lp->lock);
1546 ret = mii_ethtool_gset(&lp->mii, cmd);
1547 spin_unlock_irq(&lp->lock);
1548 } else {
1549 cmd->supported = SUPPORTED_10baseT_Half |
1550 SUPPORTED_10baseT_Full |
1551 SUPPORTED_TP | SUPPORTED_AUI;
1552
1553 if (lp->ctl_rspeed == 10)
David Decotigny70739492011-04-27 18:32:40 +00001554 ethtool_cmd_speed_set(cmd, SPEED_10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 else if (lp->ctl_rspeed == 100)
David Decotigny70739492011-04-27 18:32:40 +00001556 ethtool_cmd_speed_set(cmd, SPEED_100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 cmd->autoneg = AUTONEG_DISABLE;
1559 cmd->transceiver = XCVR_INTERNAL;
1560 cmd->port = 0;
1561 cmd->duplex = lp->tcr_cur_mode & TCR_SWFDUP ? DUPLEX_FULL : DUPLEX_HALF;
1562
1563 ret = 0;
1564 }
1565
1566 return ret;
1567}
1568
1569static int
1570smc_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1571{
1572 struct smc_local *lp = netdev_priv(dev);
1573 int ret;
1574
1575 if (lp->phy_type != 0) {
1576 spin_lock_irq(&lp->lock);
1577 ret = mii_ethtool_sset(&lp->mii, cmd);
1578 spin_unlock_irq(&lp->lock);
1579 } else {
1580 if (cmd->autoneg != AUTONEG_DISABLE ||
1581 cmd->speed != SPEED_10 ||
1582 (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL) ||
1583 (cmd->port != PORT_TP && cmd->port != PORT_AUI))
1584 return -EINVAL;
1585
1586// lp->port = cmd->port;
1587 lp->ctl_rfduplx = cmd->duplex == DUPLEX_FULL;
1588
1589// if (netif_running(dev))
1590// smc_set_port(dev);
1591
1592 ret = 0;
1593 }
1594
1595 return ret;
1596}
1597
1598static void
1599smc_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1600{
Jiri Pirko7826d432013-01-06 00:44:26 +00001601 strlcpy(info->driver, CARDNAME, sizeof(info->driver));
1602 strlcpy(info->version, version, sizeof(info->version));
1603 strlcpy(info->bus_info, dev_name(dev->dev.parent),
1604 sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607static int smc_ethtool_nwayreset(struct net_device *dev)
1608{
1609 struct smc_local *lp = netdev_priv(dev);
1610 int ret = -EINVAL;
1611
1612 if (lp->phy_type != 0) {
1613 spin_lock_irq(&lp->lock);
1614 ret = mii_nway_restart(&lp->mii);
1615 spin_unlock_irq(&lp->lock);
1616 }
1617
1618 return ret;
1619}
1620
1621static u32 smc_ethtool_getmsglevel(struct net_device *dev)
1622{
1623 struct smc_local *lp = netdev_priv(dev);
1624 return lp->msg_enable;
1625}
1626
1627static void smc_ethtool_setmsglevel(struct net_device *dev, u32 level)
1628{
1629 struct smc_local *lp = netdev_priv(dev);
1630 lp->msg_enable = level;
1631}
1632
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001633static int smc_write_eeprom_word(struct net_device *dev, u16 addr, u16 word)
1634{
1635 u16 ctl;
1636 struct smc_local *lp = netdev_priv(dev);
1637 void __iomem *ioaddr = lp->base;
1638
1639 spin_lock_irq(&lp->lock);
1640 /* load word into GP register */
1641 SMC_SELECT_BANK(lp, 1);
1642 SMC_SET_GP(lp, word);
1643 /* set the address to put the data in EEPROM */
1644 SMC_SELECT_BANK(lp, 2);
1645 SMC_SET_PTR(lp, addr);
1646 /* tell it to write */
1647 SMC_SELECT_BANK(lp, 1);
1648 ctl = SMC_GET_CTL(lp);
1649 SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_STORE));
1650 /* wait for it to finish */
1651 do {
1652 udelay(1);
1653 } while (SMC_GET_CTL(lp) & CTL_STORE);
1654 /* clean up */
1655 SMC_SET_CTL(lp, ctl);
1656 SMC_SELECT_BANK(lp, 2);
1657 spin_unlock_irq(&lp->lock);
1658 return 0;
1659}
1660
1661static int smc_read_eeprom_word(struct net_device *dev, u16 addr, u16 *word)
1662{
1663 u16 ctl;
1664 struct smc_local *lp = netdev_priv(dev);
1665 void __iomem *ioaddr = lp->base;
1666
1667 spin_lock_irq(&lp->lock);
1668 /* set the EEPROM address to get the data from */
1669 SMC_SELECT_BANK(lp, 2);
1670 SMC_SET_PTR(lp, addr | PTR_READ);
1671 /* tell it to load */
1672 SMC_SELECT_BANK(lp, 1);
1673 SMC_SET_GP(lp, 0xffff); /* init to known */
1674 ctl = SMC_GET_CTL(lp);
1675 SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_RELOAD));
1676 /* wait for it to finish */
1677 do {
1678 udelay(1);
1679 } while (SMC_GET_CTL(lp) & CTL_RELOAD);
1680 /* read word from GP register */
1681 *word = SMC_GET_GP(lp);
1682 /* clean up */
1683 SMC_SET_CTL(lp, ctl);
1684 SMC_SELECT_BANK(lp, 2);
1685 spin_unlock_irq(&lp->lock);
1686 return 0;
1687}
1688
1689static int smc_ethtool_geteeprom_len(struct net_device *dev)
1690{
1691 return 0x23 * 2;
1692}
1693
1694static int smc_ethtool_geteeprom(struct net_device *dev,
1695 struct ethtool_eeprom *eeprom, u8 *data)
1696{
1697 int i;
1698 int imax;
1699
Ben Boeckel6389aa42013-11-01 08:53:31 -04001700 DBG(1, dev, "Reading %d bytes at %d(0x%x)\n",
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001701 eeprom->len, eeprom->offset, eeprom->offset);
1702 imax = smc_ethtool_geteeprom_len(dev);
1703 for (i = 0; i < eeprom->len; i += 2) {
1704 int ret;
1705 u16 wbuf;
1706 int offset = i + eeprom->offset;
1707 if (offset > imax)
1708 break;
1709 ret = smc_read_eeprom_word(dev, offset >> 1, &wbuf);
1710 if (ret != 0)
1711 return ret;
Ben Boeckel6389aa42013-11-01 08:53:31 -04001712 DBG(2, dev, "Read 0x%x from 0x%x\n", wbuf, offset >> 1);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001713 data[i] = (wbuf >> 8) & 0xff;
1714 data[i+1] = wbuf & 0xff;
1715 }
1716 return 0;
1717}
1718
1719static int smc_ethtool_seteeprom(struct net_device *dev,
1720 struct ethtool_eeprom *eeprom, u8 *data)
1721{
1722 int i;
1723 int imax;
1724
Ben Boeckel6389aa42013-11-01 08:53:31 -04001725 DBG(1, dev, "Writing %d bytes to %d(0x%x)\n",
1726 eeprom->len, eeprom->offset, eeprom->offset);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001727 imax = smc_ethtool_geteeprom_len(dev);
1728 for (i = 0; i < eeprom->len; i += 2) {
1729 int ret;
1730 u16 wbuf;
1731 int offset = i + eeprom->offset;
1732 if (offset > imax)
1733 break;
1734 wbuf = (data[i] << 8) | data[i + 1];
Ben Boeckel6389aa42013-11-01 08:53:31 -04001735 DBG(2, dev, "Writing 0x%x to 0x%x\n", wbuf, offset >> 1);
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001736 ret = smc_write_eeprom_word(dev, offset >> 1, wbuf);
1737 if (ret != 0)
1738 return ret;
1739 }
1740 return 0;
1741}
1742
1743
Jeff Garzik7282d492006-09-13 14:30:00 -04001744static const struct ethtool_ops smc_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 .get_settings = smc_ethtool_getsettings,
1746 .set_settings = smc_ethtool_setsettings,
1747 .get_drvinfo = smc_ethtool_getdrvinfo,
1748
1749 .get_msglevel = smc_ethtool_getmsglevel,
1750 .set_msglevel = smc_ethtool_setmsglevel,
1751 .nway_reset = smc_ethtool_nwayreset,
1752 .get_link = ethtool_op_get_link,
Vernon Sauder357fe2c2009-01-16 13:23:19 +00001753 .get_eeprom_len = smc_ethtool_geteeprom_len,
1754 .get_eeprom = smc_ethtool_geteeprom,
1755 .set_eeprom = smc_ethtool_seteeprom,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756};
1757
Magnus Damma5280792009-01-26 21:32:25 -08001758static const struct net_device_ops smc_netdev_ops = {
1759 .ndo_open = smc_open,
1760 .ndo_stop = smc_close,
1761 .ndo_start_xmit = smc_hard_start_xmit,
1762 .ndo_tx_timeout = smc_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001763 .ndo_set_rx_mode = smc_set_multicast_list,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001764 .ndo_change_mtu = eth_change_mtu,
Magnus Damma5280792009-01-26 21:32:25 -08001765 .ndo_validate_addr = eth_validate_addr,
1766 .ndo_set_mac_address = eth_mac_addr,
1767#ifdef CONFIG_NET_POLL_CONTROLLER
1768 .ndo_poll_controller = smc_poll_controller,
1769#endif
1770};
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/*
1773 * smc_findirq
1774 *
1775 * This routine has a simple purpose -- make the SMC chip generate an
1776 * interrupt, so an auto-detect routine can detect it, and find the IRQ,
1777 */
1778/*
1779 * does this still work?
1780 *
1781 * I just deleted auto_irq.c, since it was never built...
1782 * --jgarzik
1783 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05001784static int smc_findirq(struct smc_local *lp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Magnus Dammcfdfa862008-02-22 19:55:05 +09001786 void __iomem *ioaddr = lp->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int timeout = 20;
1788 unsigned long cookie;
1789
Zi Shen Lim702b3462014-04-18 20:47:30 -07001790 DBG(2, lp->dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 cookie = probe_irq_on();
1793
1794 /*
1795 * What I try to do here is trigger an ALLOC_INT. This is done
1796 * by allocating a small chunk of memory, which will give an interrupt
1797 * when done.
1798 */
1799 /* enable ALLOCation interrupts ONLY */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001800 SMC_SELECT_BANK(lp, 2);
1801 SMC_SET_INT_MASK(lp, IM_ALLOC_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 /*
1804 * Allocate 512 bytes of memory. Note that the chip was just
1805 * reset so all the memory is available
1806 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001807 SMC_SET_MMU_CMD(lp, MC_ALLOC | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /*
1810 * Wait until positive that the interrupt has been generated
1811 */
1812 do {
1813 int int_status;
1814 udelay(10);
Magnus Dammcfdfa862008-02-22 19:55:05 +09001815 int_status = SMC_GET_INT(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (int_status & IM_ALLOC_INT)
1817 break; /* got the interrupt */
1818 } while (--timeout);
1819
1820 /*
1821 * there is really nothing that I can do here if timeout fails,
1822 * as autoirq_report will return a 0 anyway, which is what I
1823 * want in this case. Plus, the clean up is needed in both
1824 * cases.
1825 */
1826
1827 /* and disable all interrupts again */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001828 SMC_SET_INT_MASK(lp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 /* and return what I found */
1831 return probe_irq_off(cookie);
1832}
1833
1834/*
1835 * Function: smc_probe(unsigned long ioaddr)
1836 *
1837 * Purpose:
1838 * Tests to see if a given ioaddr points to an SMC91x chip.
1839 * Returns a 0 on success
1840 *
1841 * Algorithm:
1842 * (1) see if the high byte of BANK_SELECT is 0x33
1843 * (2) compare the ioaddr with the base register's address
1844 * (3) see if I recognize the chip ID in the appropriate register
1845 *
1846 * Here I do typical initialization tasks.
1847 *
1848 * o Initialize the structure if needed
1849 * o print out my vanity message if not done so already
1850 * o print out what type of hardware is detected
1851 * o print out the ethernet address
1852 * o find the IRQ
1853 * o set up my private data
1854 * o configure the dev structure with my subroutines
1855 * o actually GRAB the irq.
1856 * o GRAB the region
1857 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05001858static int smc_probe(struct net_device *dev, void __iomem *ioaddr,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00001859 unsigned long irq_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
1861 struct smc_local *lp = netdev_priv(dev);
Joe Perches0795af52007-10-03 17:59:30 -07001862 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned int val, revision_register;
1864 const char *version_string;
1865
Ben Boeckel6389aa42013-11-01 08:53:31 -04001866 DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 /* First, see if the high byte is 0x33 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001869 val = SMC_CURRENT_BANK(lp);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001870 DBG(2, dev, "%s: bank signature probe returned 0x%04x\n",
1871 CARDNAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if ((val & 0xFF00) != 0x3300) {
1873 if ((val & 0xFF) == 0x33) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001874 netdev_warn(dev,
1875 "%s: Detected possible byte-swapped interface at IOADDR %p\n",
1876 CARDNAME, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 retval = -ENODEV;
1879 goto err_out;
1880 }
1881
1882 /*
1883 * The above MIGHT indicate a device, but I need to write to
1884 * further test this.
1885 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001886 SMC_SELECT_BANK(lp, 0);
1887 val = SMC_CURRENT_BANK(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if ((val & 0xFF00) != 0x3300) {
1889 retval = -ENODEV;
1890 goto err_out;
1891 }
1892
1893 /*
1894 * well, we've already written once, so hopefully another
1895 * time won't hurt. This time, I need to switch the bank
1896 * register to bank 1, so I can access the base address
1897 * register
1898 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001899 SMC_SELECT_BANK(lp, 1);
1900 val = SMC_GET_BASE(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 val = ((val & 0x1F00) >> 3) << SMC_IO_SHIFT;
Pankaj Dubey6bc21ee2014-01-10 12:04:06 +09001902 if (((unsigned long)ioaddr & (0x3e0 << SMC_IO_SHIFT)) != val) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001903 netdev_warn(dev, "%s: IOADDR %p doesn't match configuration (%x).\n",
1904 CARDNAME, ioaddr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
1907 /*
1908 * check if the revision register is something that I
1909 * recognize. These might need to be added to later,
1910 * as future revisions could be added.
1911 */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001912 SMC_SELECT_BANK(lp, 3);
1913 revision_register = SMC_GET_REV(lp);
Ben Boeckel6389aa42013-11-01 08:53:31 -04001914 DBG(2, dev, "%s: revision = 0x%04x\n", CARDNAME, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 version_string = chip_ids[ (revision_register >> 4) & 0xF];
1916 if (!version_string || (revision_register & 0xff00) != 0x3300) {
1917 /* I don't recognize this chip, so... */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001918 netdev_warn(dev, "%s: IO %p: Unrecognized revision register 0x%04x, Contact author.\n",
1919 CARDNAME, ioaddr, revision_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 retval = -ENODEV;
1922 goto err_out;
1923 }
1924
1925 /* At this point I'll assume that the chip is an SMC91x. */
Ben Boeckel6389aa42013-11-01 08:53:31 -04001926 pr_info_once("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 /* fill in some of the fields */
1929 dev->base_addr = (unsigned long)ioaddr;
1930 lp->base = ioaddr;
1931 lp->version = revision_register & 0xff;
1932 spin_lock_init(&lp->lock);
1933
1934 /* Get the MAC address */
Magnus Dammcfdfa862008-02-22 19:55:05 +09001935 SMC_SELECT_BANK(lp, 1);
1936 SMC_GET_MAC_ADDR(lp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* now, reset the chip, and put it into a known state */
1939 smc_reset(dev);
1940
1941 /*
1942 * If dev->irq is 0, then the device has to be banged on to see
1943 * what the IRQ is.
Ben Boeckel6389aa42013-11-01 08:53:31 -04001944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 * This banging doesn't always detect the IRQ, for unknown reasons.
1946 * a workaround is to reset the chip and try again.
1947 *
1948 * Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1949 * be what is requested on the command line. I don't do that, mostly
1950 * because the card that I have uses a non-standard method of accessing
1951 * the IRQs, and because this _should_ work in most configurations.
1952 *
1953 * Specifying an IRQ is done with the assumption that the user knows
1954 * what (s)he is doing. No checking is done!!!!
1955 */
1956 if (dev->irq < 1) {
1957 int trials;
1958
1959 trials = 3;
1960 while (trials--) {
Magnus Dammcfdfa862008-02-22 19:55:05 +09001961 dev->irq = smc_findirq(lp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (dev->irq)
1963 break;
1964 /* kick the card and try again */
1965 smc_reset(dev);
1966 }
1967 }
1968 if (dev->irq == 0) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04001969 netdev_warn(dev, "Couldn't autodetect your IRQ. Use irq=xx.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 retval = -ENODEV;
1971 goto err_out;
1972 }
1973 dev->irq = irq_canonicalize(dev->irq);
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
Magnus Damma5280792009-01-26 21:32:25 -08001976 dev->netdev_ops = &smc_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 dev->ethtool_ops = &smc_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 tasklet_init(&lp->tx_task, smc_hardware_send_pkt, (unsigned long)dev);
David Howells6d5aefb2006-12-05 19:36:26 +00001980 INIT_WORK(&lp->phy_configure, smc_phy_configure);
1981 lp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 lp->mii.phy_id_mask = 0x1f;
1983 lp->mii.reg_num_mask = 0x1f;
1984 lp->mii.force_media = 0;
1985 lp->mii.full_duplex = 0;
1986 lp->mii.dev = dev;
1987 lp->mii.mdio_read = smc_phy_read;
1988 lp->mii.mdio_write = smc_phy_write;
1989
1990 /*
1991 * Locate the phy, if any.
1992 */
1993 if (lp->version >= (CHIP_91100 << 4))
1994 smc_phy_detect(dev);
1995
Nicolas Pitre99e1baf2005-10-05 11:10:24 -04001996 /* then shut everything down to save power */
1997 smc_shutdown(dev);
1998 smc_phy_powerdown(dev);
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* Set default parameters */
2001 lp->msg_enable = NETIF_MSG_LINK;
2002 lp->ctl_rfduplx = 0;
2003 lp->ctl_rspeed = 10;
2004
2005 if (lp->version >= (CHIP_91100 << 4)) {
2006 lp->ctl_rfduplx = 1;
2007 lp->ctl_rspeed = 100;
2008 }
2009
2010 /* Grab the IRQ */
Joe Perchesa0607fd2009-11-18 23:29:17 -08002011 retval = request_irq(dev->irq, smc_interrupt, irq_flags, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (retval)
2013 goto err_out;
2014
Eric Miao52256c02008-06-24 15:36:05 +08002015#ifdef CONFIG_ARCH_PXA
2016# ifdef SMC_USE_PXA_DMA
2017 lp->cfg.flags |= SMC91X_USE_DMA;
2018# endif
2019 if (lp->cfg.flags & SMC91X_USE_DMA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int dma = pxa_request_dma(dev->name, DMA_PRIO_LOW,
2021 smc_pxa_dma_irq, NULL);
2022 if (dma >= 0)
2023 dev->dma = dma;
2024 }
2025#endif
2026
2027 retval = register_netdev(dev);
2028 if (retval == 0) {
2029 /* now, print out the card info, in a short format.. */
Ben Boeckel6389aa42013-11-01 08:53:31 -04002030 netdev_info(dev, "%s (rev %d) at %p IRQ %d",
2031 version_string, revision_register & 0x0f,
2032 lp->base, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if (dev->dma != (unsigned char)-1)
Ben Boeckel6389aa42013-11-01 08:53:31 -04002035 pr_cont(" DMA %d", dev->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Ben Boeckel6389aa42013-11-01 08:53:31 -04002037 pr_cont("%s%s\n",
Magnus Dammd6bc3722008-09-08 14:02:56 +09002038 lp->cfg.flags & SMC91X_NOWAIT ? " [nowait]" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 THROTTLE_TX_PKTS ? " [throttle_tx]" : "");
2040
2041 if (!is_valid_ether_addr(dev->dev_addr)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002042 netdev_warn(dev, "Invalid ethernet MAC address. Please set using ifconfig\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 } else {
2044 /* Print the Ethernet address */
Ben Boeckel6389aa42013-11-01 08:53:31 -04002045 netdev_info(dev, "Ethernet addr: %pM\n",
2046 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
2049 if (lp->phy_type == 0) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002050 PRINTK(dev, "No PHY found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } else if ((lp->phy_type & 0xfffffff0) == 0x0016f840) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002052 PRINTK(dev, "PHY LAN83C183 (LAN91C111 Internal)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else if ((lp->phy_type & 0xfffffff0) == 0x02821c50) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002054 PRINTK(dev, "PHY LAN83C180\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 }
2057
2058err_out:
Eric Miao52256c02008-06-24 15:36:05 +08002059#ifdef CONFIG_ARCH_PXA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (retval && dev->dma != (unsigned char)-1)
2061 pxa_free_dma(dev->dma);
2062#endif
2063 return retval;
2064}
2065
2066static int smc_enable_device(struct platform_device *pdev)
2067{
Magnus Damm3e947942008-02-22 19:55:15 +09002068 struct net_device *ndev = platform_get_drvdata(pdev);
2069 struct smc_local *lp = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 unsigned long flags;
2071 unsigned char ecor, ecsr;
2072 void __iomem *addr;
2073 struct resource * res;
2074
2075 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2076 if (!res)
2077 return 0;
2078
2079 /*
2080 * Map the attribute space. This is overkill, but clean.
2081 */
2082 addr = ioremap(res->start, ATTRIB_SIZE);
2083 if (!addr)
2084 return -ENOMEM;
2085
2086 /*
2087 * Reset the device. We must disable IRQs around this
2088 * since a reset causes the IRQ line become active.
2089 */
2090 local_irq_save(flags);
2091 ecor = readb(addr + (ECOR << SMC_IO_SHIFT)) & ~ECOR_RESET;
2092 writeb(ecor | ECOR_RESET, addr + (ECOR << SMC_IO_SHIFT));
2093 readb(addr + (ECOR << SMC_IO_SHIFT));
2094
2095 /*
2096 * Wait 100us for the chip to reset.
2097 */
2098 udelay(100);
2099
2100 /*
2101 * The device will ignore all writes to the enable bit while
2102 * reset is asserted, even if the reset bit is cleared in the
2103 * same write. Must clear reset first, then enable the device.
2104 */
2105 writeb(ecor, addr + (ECOR << SMC_IO_SHIFT));
2106 writeb(ecor | ECOR_ENABLE, addr + (ECOR << SMC_IO_SHIFT));
2107
2108 /*
2109 * Set the appropriate byte/word mode.
2110 */
2111 ecsr = readb(addr + (ECSR << SMC_IO_SHIFT)) & ~ECSR_IOIS8;
Magnus Damm3e947942008-02-22 19:55:15 +09002112 if (!SMC_16BIT(lp))
Nicolas Pitre09779c62006-03-20 11:54:27 -05002113 ecsr |= ECSR_IOIS8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 writeb(ecsr, addr + (ECSR << SMC_IO_SHIFT));
2115 local_irq_restore(flags);
2116
2117 iounmap(addr);
2118
2119 /*
2120 * Wait for the chip to wake up. We could poll the control
2121 * register in the main register space, but that isn't mapped
2122 * yet. We know this is going to take 750us.
2123 */
2124 msleep(1);
2125
2126 return 0;
2127}
2128
Eric Miao15919882008-06-24 13:38:50 +08002129static int smc_request_attrib(struct platform_device *pdev,
2130 struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
David S. Miller55c8eb62008-11-03 00:04:24 -08002133 struct smc_local *lp __maybe_unused = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 if (!res)
2136 return 0;
2137
2138 if (!request_mem_region(res->start, ATTRIB_SIZE, CARDNAME))
2139 return -EBUSY;
2140
2141 return 0;
2142}
2143
Eric Miao15919882008-06-24 13:38:50 +08002144static void smc_release_attrib(struct platform_device *pdev,
2145 struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
David S. Miller55c8eb62008-11-03 00:04:24 -08002148 struct smc_local *lp __maybe_unused = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 if (res)
2151 release_mem_region(res->start, ATTRIB_SIZE);
2152}
2153
Nicolas Pitre09779c62006-03-20 11:54:27 -05002154static inline void smc_request_datacs(struct platform_device *pdev, struct net_device *ndev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Nicolas Pitre09779c62006-03-20 11:54:27 -05002156 if (SMC_CAN_USE_DATACS) {
2157 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2158 struct smc_local *lp = netdev_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Nicolas Pitre09779c62006-03-20 11:54:27 -05002160 if (!res)
2161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Nicolas Pitre09779c62006-03-20 11:54:27 -05002163 if(!request_mem_region(res->start, SMC_DATA_EXTENT, CARDNAME)) {
Ben Boeckel6389aa42013-11-01 08:53:31 -04002164 netdev_info(ndev, "%s: failed to request datacs memory region.\n",
2165 CARDNAME);
Nicolas Pitre09779c62006-03-20 11:54:27 -05002166 return;
2167 }
2168
2169 lp->datacs = ioremap(res->start, SMC_DATA_EXTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173static void smc_release_datacs(struct platform_device *pdev, struct net_device *ndev)
2174{
Nicolas Pitre09779c62006-03-20 11:54:27 -05002175 if (SMC_CAN_USE_DATACS) {
2176 struct smc_local *lp = netdev_priv(ndev);
2177 struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Nicolas Pitre09779c62006-03-20 11:54:27 -05002179 if (lp->datacs)
2180 iounmap(lp->datacs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Nicolas Pitre09779c62006-03-20 11:54:27 -05002182 lp->datacs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Nicolas Pitre09779c62006-03-20 11:54:27 -05002184 if (res)
2185 release_mem_region(res->start, SMC_DATA_EXTENT);
2186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Tony Lindgren3f823c12013-12-11 13:04:27 -08002189#if IS_BUILTIN(CONFIG_OF)
2190static const struct of_device_id smc91x_match[] = {
2191 { .compatible = "smsc,lan91c94", },
2192 { .compatible = "smsc,lan91c111", },
2193 {},
2194};
2195MODULE_DEVICE_TABLE(of, smc91x_match);
Tony Lindgren7d2911c2014-10-30 09:59:27 -07002196
2197/**
2198 * of_try_set_control_gpio - configure a gpio if it exists
2199 */
2200static int try_toggle_control_gpio(struct device *dev,
2201 struct gpio_desc **desc,
2202 const char *name, int index,
2203 int value, unsigned int nsdelay)
2204{
2205 struct gpio_desc *gpio = *desc;
2206 int res;
2207
2208 gpio = devm_gpiod_get_index(dev, name, index);
2209 if (IS_ERR(gpio)) {
2210 if (PTR_ERR(gpio) == -ENOENT) {
2211 *desc = NULL;
2212 return 0;
2213 }
2214
2215 return PTR_ERR(gpio);
2216 }
2217 res = gpiod_direction_output(gpio, !value);
2218 if (res) {
2219 dev_err(dev, "unable to toggle gpio %s: %i\n", name, res);
2220 devm_gpiod_put(dev, gpio);
2221 gpio = NULL;
2222 return res;
2223 }
2224 if (nsdelay)
2225 usleep_range(nsdelay, 2 * nsdelay);
2226 gpiod_set_value_cansleep(gpio, value);
2227 *desc = gpio;
2228
2229 return 0;
2230}
Tony Lindgren3f823c12013-12-11 13:04:27 -08002231#endif
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
2234 * smc_init(void)
2235 * Input parameters:
2236 * dev->base_addr == 0, try to find all possible locations
2237 * dev->base_addr > 0x1ff, this is the address to check
2238 * dev->base_addr == <anything else>, return failure code
2239 *
2240 * Output:
2241 * 0 --> there is a device
2242 * anything else, error
2243 */
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002244static int smc_drv_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Jingoo Hanf64deac2013-08-30 14:01:35 +09002246 struct smc91x_platdata *pd = dev_get_platdata(&pdev->dev);
Tony Lindgren3f823c12013-12-11 13:04:27 -08002247 const struct of_device_id *match = NULL;
Magnus Damm3e947942008-02-22 19:55:15 +09002248 struct smc_local *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct net_device *ndev;
Linus Walleijd52fdbb2014-10-31 21:32:06 +01002250 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 unsigned int __iomem *addr;
Eric Miaod280ead2008-06-06 17:13:02 +08002252 unsigned long irq_flags = SMC_IRQ_FLAGS;
Linus Walleijd52fdbb2014-10-31 21:32:06 +01002253 unsigned long irq_resflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int ret;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 ndev = alloc_etherdev(sizeof(struct smc_local));
2257 if (!ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 ret = -ENOMEM;
Eric Miao15919882008-06-24 13:38:50 +08002259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Russell King3ae5eae2005-11-09 22:32:44 +00002261 SET_NETDEV_DEV(ndev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Magnus Damm3e947942008-02-22 19:55:15 +09002263 /* get configuration from platform data, only allow use of
2264 * bus width if both SMC_CAN_USE_xxx and SMC91X_USE_xxx are set.
2265 */
2266
2267 lp = netdev_priv(ndev);
Tony Lindgren3f823c12013-12-11 13:04:27 -08002268 lp->cfg.flags = 0;
Magnus Damm3e947942008-02-22 19:55:15 +09002269
Eric Miao15919882008-06-24 13:38:50 +08002270 if (pd) {
Magnus Damm3e947942008-02-22 19:55:15 +09002271 memcpy(&lp->cfg, pd, sizeof(lp->cfg));
Eric Miao15919882008-06-24 13:38:50 +08002272 lp->io_shift = SMC91X_IO_SHIFT(lp->cfg.flags);
Tony Lindgren3f823c12013-12-11 13:04:27 -08002273 }
2274
2275#if IS_BUILTIN(CONFIG_OF)
2276 match = of_match_device(of_match_ptr(smc91x_match), &pdev->dev);
2277 if (match) {
2278 struct device_node *np = pdev->dev.of_node;
2279 u32 val;
2280
Tony Lindgren7d2911c2014-10-30 09:59:27 -07002281 /* Optional pwrdwn GPIO configured? */
2282 ret = try_toggle_control_gpio(&pdev->dev, &lp->power_gpio,
2283 "power", 0, 0, 100);
2284 if (ret)
2285 return ret;
2286
2287 /*
2288 * Optional reset GPIO configured? Minimum 100 ns reset needed
2289 * according to LAN91C96 datasheet page 14.
2290 */
2291 ret = try_toggle_control_gpio(&pdev->dev, &lp->reset_gpio,
2292 "reset", 0, 0, 100);
2293 if (ret)
2294 return ret;
2295
2296 /*
2297 * Need to wait for optional EEPROM to load, max 750 us according
2298 * to LAN91C96 datasheet page 55.
2299 */
2300 if (lp->reset_gpio)
2301 usleep_range(750, 1000);
2302
Tony Lindgren3f823c12013-12-11 13:04:27 -08002303 /* Combination of IO widths supported, default to 16-bit */
2304 if (!of_property_read_u32(np, "reg-io-width", &val)) {
2305 if (val & 1)
2306 lp->cfg.flags |= SMC91X_USE_8BIT;
2307 if ((val == 0) || (val & 2))
2308 lp->cfg.flags |= SMC91X_USE_16BIT;
2309 if (val & 4)
2310 lp->cfg.flags |= SMC91X_USE_32BIT;
2311 } else {
2312 lp->cfg.flags |= SMC91X_USE_16BIT;
2313 }
2314 }
2315#endif
2316
2317 if (!pd && !match) {
Eric Miaofa6d3be2008-06-19 17:19:57 +08002318 lp->cfg.flags |= (SMC_CAN_USE_8BIT) ? SMC91X_USE_8BIT : 0;
2319 lp->cfg.flags |= (SMC_CAN_USE_16BIT) ? SMC91X_USE_16BIT : 0;
2320 lp->cfg.flags |= (SMC_CAN_USE_32BIT) ? SMC91X_USE_32BIT : 0;
Eric Miaoc4f0e762008-06-19 17:39:03 +08002321 lp->cfg.flags |= (nowait) ? SMC91X_NOWAIT : 0;
Magnus Damm3e947942008-02-22 19:55:15 +09002322 }
2323
Russell Kingb0dbcf52008-09-04 21:13:37 +01002324 if (!lp->cfg.leda && !lp->cfg.ledb) {
2325 lp->cfg.leda = RPC_LSA_DEFAULT;
2326 lp->cfg.ledb = RPC_LSB_DEFAULT;
2327 }
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 ndev->dma = (unsigned char)-1;
Russell Kinge7b3dc72008-01-14 22:30:10 +00002330
Eric Miao15919882008-06-24 13:38:50 +08002331 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2332 if (!res)
2333 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2334 if (!res) {
2335 ret = -ENODEV;
2336 goto out_free_netdev;
2337 }
2338
2339
2340 if (!request_mem_region(res->start, SMC_IO_EXTENT, CARDNAME)) {
2341 ret = -EBUSY;
2342 goto out_free_netdev;
2343 }
2344
Linus Walleijd52fdbb2014-10-31 21:32:06 +01002345 ndev->irq = platform_get_irq(pdev, 0);
2346 if (ndev->irq <= 0) {
David Vrabel48944732006-01-19 17:56:29 +00002347 ret = -ENODEV;
Eric Miao15919882008-06-24 13:38:50 +08002348 goto out_release_io;
David Vrabel48944732006-01-19 17:56:29 +00002349 }
Linus Walleijd52fdbb2014-10-31 21:32:06 +01002350 /*
2351 * If this platform does not specify any special irqflags, or if
2352 * the resource supplies a trigger, override the irqflags with
2353 * the trigger flags from the resource.
2354 */
2355 irq_resflags = irqd_get_trigger_type(irq_get_irq_data(ndev->irq));
2356 if (irq_flags == -1 || irq_resflags & IRQF_TRIGGER_MASK)
2357 irq_flags = irq_resflags & IRQF_TRIGGER_MASK;
Russell Kinge7b3dc72008-01-14 22:30:10 +00002358
Eric Miao15919882008-06-24 13:38:50 +08002359 ret = smc_request_attrib(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (ret)
Eric Miao15919882008-06-24 13:38:50 +08002361 goto out_release_io;
Arnd Bergmannb70661c2015-02-25 16:31:57 +01002362#if defined(CONFIG_ASSABET_NEPONSET)
2363 if (machine_is_assabet() && machine_has_neponset())
2364 neponset_ncr_set(NCR_ENET_OSC_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#endif
Magnus Damm3e947942008-02-22 19:55:15 +09002366 platform_set_drvdata(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 ret = smc_enable_device(pdev);
2368 if (ret)
2369 goto out_release_attrib;
2370
2371 addr = ioremap(res->start, SMC_IO_EXTENT);
2372 if (!addr) {
2373 ret = -ENOMEM;
2374 goto out_release_attrib;
2375 }
2376
Eric Miao52256c02008-06-24 15:36:05 +08002377#ifdef CONFIG_ARCH_PXA
Russell King073ac8f2007-09-01 21:27:18 +01002378 {
2379 struct smc_local *lp = netdev_priv(ndev);
2380 lp->device = &pdev->dev;
2381 lp->physaddr = res->start;
2382 }
2383#endif
2384
Eric Miaod280ead2008-06-06 17:13:02 +08002385 ret = smc_probe(ndev, addr, irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (ret != 0)
2387 goto out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 smc_request_datacs(pdev, ndev);
2390
2391 return 0;
2392
2393 out_iounmap:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 iounmap(addr);
2395 out_release_attrib:
Eric Miao15919882008-06-24 13:38:50 +08002396 smc_release_attrib(pdev, ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 out_release_io:
2398 release_mem_region(res->start, SMC_IO_EXTENT);
Eric Miao15919882008-06-24 13:38:50 +08002399 out_free_netdev:
2400 free_netdev(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 out:
Ben Boeckel6389aa42013-11-01 08:53:31 -04002402 pr_info("%s: not found (%d).\n", CARDNAME, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 return ret;
2405}
2406
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002407static int smc_drv_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Russell King3ae5eae2005-11-09 22:32:44 +00002409 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct smc_local *lp = netdev_priv(ndev);
2411 struct resource *res;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 unregister_netdev(ndev);
2414
2415 free_irq(ndev->irq, ndev);
2416
Eric Miao52256c02008-06-24 15:36:05 +08002417#ifdef CONFIG_ARCH_PXA
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (ndev->dma != (unsigned char)-1)
2419 pxa_free_dma(ndev->dma);
2420#endif
2421 iounmap(lp->base);
2422
2423 smc_release_datacs(pdev,ndev);
Eric Miao15919882008-06-24 13:38:50 +08002424 smc_release_attrib(pdev,ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2427 if (!res)
Jeff Garzik6fc30db52008-08-27 05:54:30 -04002428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 release_mem_region(res->start, SMC_IO_EXTENT);
2430
2431 free_netdev(ndev);
2432
2433 return 0;
2434}
2435
Kevin Hilman9f950f72009-11-24 12:57:47 +00002436static int smc_drv_suspend(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Kevin Hilman9f950f72009-11-24 12:57:47 +00002438 struct platform_device *pdev = to_platform_device(dev);
2439 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Russell King9480e302005-10-28 09:52:56 -07002441 if (ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (netif_running(ndev)) {
2443 netif_device_detach(ndev);
2444 smc_shutdown(ndev);
2445 smc_phy_powerdown(ndev);
2446 }
2447 }
2448 return 0;
2449}
2450
Kevin Hilman9f950f72009-11-24 12:57:47 +00002451static int smc_drv_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Kevin Hilman9f950f72009-11-24 12:57:47 +00002453 struct platform_device *pdev = to_platform_device(dev);
2454 struct net_device *ndev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Russell King9480e302005-10-28 09:52:56 -07002456 if (ndev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct smc_local *lp = netdev_priv(ndev);
Paul Mundt5fc34412009-12-10 20:42:27 +00002458 smc_enable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (netif_running(ndev)) {
2460 smc_reset(ndev);
2461 smc_enable(ndev);
2462 if (lp->phy_type != 0)
David Howells6d5aefb2006-12-05 19:36:26 +00002463 smc_phy_configure(&lp->phy_configure);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 netif_device_attach(ndev);
2465 }
2466 }
2467 return 0;
2468}
2469
Kevin Hilman9f950f72009-11-24 12:57:47 +00002470static struct dev_pm_ops smc_drv_pm_ops = {
2471 .suspend = smc_drv_suspend,
2472 .resume = smc_drv_resume,
2473};
2474
Russell King3ae5eae2005-11-09 22:32:44 +00002475static struct platform_driver smc_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .probe = smc_drv_probe,
Bill Pemberton1e48fea2012-12-03 09:23:37 -05002477 .remove = smc_drv_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002478 .driver = {
2479 .name = CARDNAME,
Kevin Hilman9f950f72009-11-24 12:57:47 +00002480 .pm = &smc_drv_pm_ops,
Sachin Kamat89ce3762012-12-19 01:17:09 +00002481 .of_match_table = of_match_ptr(smc91x_match),
Russell King3ae5eae2005-11-09 22:32:44 +00002482 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483};
2484
Axel Lindb62f682011-11-27 16:44:17 +00002485module_platform_driver(smc_driver);