11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ae150435SJeff Kirsher /*
3ae150435SJeff Kirsher * smc91x.c
4ae150435SJeff Kirsher * This is a driver for SMSC's 91C9x/91C1xx single-chip Ethernet devices.
5ae150435SJeff Kirsher *
6ae150435SJeff Kirsher * Copyright (C) 1996 by Erik Stahlman
7ae150435SJeff Kirsher * Copyright (C) 2001 Standard Microsystems Corporation
8ae150435SJeff Kirsher * Developed by Simple Network Magic Corporation
9ae150435SJeff Kirsher * Copyright (C) 2003 Monta Vista Software, Inc.
10ae150435SJeff Kirsher * Unified SMC91x driver by Nicolas Pitre
11ae150435SJeff Kirsher *
12ae150435SJeff Kirsher * Arguments:
13ae150435SJeff Kirsher * io = for the base address
14ae150435SJeff Kirsher * irq = for the IRQ
15ae150435SJeff Kirsher * nowait = 0 for normal wait states, 1 eliminates additional wait states
16ae150435SJeff Kirsher *
17ae150435SJeff Kirsher * original author:
18ae150435SJeff Kirsher * Erik Stahlman <erik@vt.edu>
19ae150435SJeff Kirsher *
20ae150435SJeff Kirsher * hardware multicast code:
21ae150435SJeff Kirsher * Peter Cammaert <pc@denkart.be>
22ae150435SJeff Kirsher *
23ae150435SJeff Kirsher * contributors:
24ae150435SJeff Kirsher * Daris A Nevil <dnevil@snmc.com>
25ae150435SJeff Kirsher * Nicolas Pitre <nico@fluxnic.net>
26ae150435SJeff Kirsher * Russell King <rmk@arm.linux.org.uk>
27ae150435SJeff Kirsher *
28ae150435SJeff Kirsher * History:
29ae150435SJeff Kirsher * 08/20/00 Arnaldo Melo fix kfree(skb) in smc_hardware_send_packet
30ae150435SJeff Kirsher * 12/15/00 Christian Jullien fix "Warning: kfree_skb on hard IRQ"
31ae150435SJeff Kirsher * 03/16/01 Daris A Nevil modified smc9194.c for use with LAN91C111
32ae150435SJeff Kirsher * 08/22/01 Scott Anderson merge changes from smc9194 to smc91111
33ae150435SJeff Kirsher * 08/21/01 Pramod B Bhardwaj added support for RevB of LAN91C111
34ae150435SJeff Kirsher * 12/20/01 Jeff Sutherland initial port to Xscale PXA with DMA support
35ae150435SJeff Kirsher * 04/07/03 Nicolas Pitre unified SMC91x driver, killed irq races,
36ae150435SJeff Kirsher * more bus abstraction, big cleanup, etc.
37ae150435SJeff Kirsher * 29/09/03 Russell King - add driver model support
38ae150435SJeff Kirsher * - ethtool support
39ae150435SJeff Kirsher * - convert to use generic MII interface
40ae150435SJeff Kirsher * - add link up/down notification
41ae150435SJeff Kirsher * - don't try to handle full negotiation in
42ae150435SJeff Kirsher * smc_phy_configure
43ae150435SJeff Kirsher * - clean up (and fix stack overrun) in PHY
44ae150435SJeff Kirsher * MII read/write functions
45ae150435SJeff Kirsher * 22/09/04 Nicolas Pitre big update (see commit log for details)
46ae150435SJeff Kirsher */
47ae150435SJeff Kirsher static const char version[] =
486389aa45SBen Boeckel "smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre <nico@fluxnic.net>";
49ae150435SJeff Kirsher
50ae150435SJeff Kirsher /* Debugging level */
51ae150435SJeff Kirsher #ifndef SMC_DEBUG
52ae150435SJeff Kirsher #define SMC_DEBUG 0
53ae150435SJeff Kirsher #endif
54ae150435SJeff Kirsher
55ae150435SJeff Kirsher
56ae150435SJeff Kirsher #include <linux/module.h>
57ae150435SJeff Kirsher #include <linux/kernel.h>
58ae150435SJeff Kirsher #include <linux/sched.h>
59ae150435SJeff Kirsher #include <linux/delay.h>
60*c0e906a9SAndy Shevchenko #include <linux/gpio/consumer.h>
61ae150435SJeff Kirsher #include <linux/interrupt.h>
62ae150435SJeff Kirsher #include <linux/irq.h>
63ae150435SJeff Kirsher #include <linux/errno.h>
64ae150435SJeff Kirsher #include <linux/ioport.h>
65ae150435SJeff Kirsher #include <linux/crc32.h>
66ae150435SJeff Kirsher #include <linux/platform_device.h>
67ae150435SJeff Kirsher #include <linux/spinlock.h>
68ae150435SJeff Kirsher #include <linux/ethtool.h>
69ae150435SJeff Kirsher #include <linux/mii.h>
70ae150435SJeff Kirsher #include <linux/workqueue.h>
71ae150435SJeff Kirsher #include <linux/of.h>
723f823c15STony Lindgren #include <linux/of_device.h>
73ae150435SJeff Kirsher
74ae150435SJeff Kirsher #include <linux/netdevice.h>
75ae150435SJeff Kirsher #include <linux/etherdevice.h>
76ae150435SJeff Kirsher #include <linux/skbuff.h>
77ae150435SJeff Kirsher
78ae150435SJeff Kirsher #include <asm/io.h>
79ae150435SJeff Kirsher
80ae150435SJeff Kirsher #include "smc91x.h"
81ae150435SJeff Kirsher
82b70661c7SArnd Bergmann #if defined(CONFIG_ASSABET_NEPONSET)
8304b91701SArnd Bergmann #include <mach/assabet.h>
84b70661c7SArnd Bergmann #include <mach/neponset.h>
85b70661c7SArnd Bergmann #endif
86b70661c7SArnd Bergmann
87ae150435SJeff Kirsher #ifndef SMC_NOWAIT
88ae150435SJeff Kirsher # define SMC_NOWAIT 0
89ae150435SJeff Kirsher #endif
90ae150435SJeff Kirsher static int nowait = SMC_NOWAIT;
91ae150435SJeff Kirsher module_param(nowait, int, 0400);
92ae150435SJeff Kirsher MODULE_PARM_DESC(nowait, "set to 1 for no wait state");
93ae150435SJeff Kirsher
94ae150435SJeff Kirsher /*
95ae150435SJeff Kirsher * Transmit timeout, default 5 seconds.
96ae150435SJeff Kirsher */
97ae150435SJeff Kirsher static int watchdog = 1000;
98ae150435SJeff Kirsher module_param(watchdog, int, 0400);
99ae150435SJeff Kirsher MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");
100ae150435SJeff Kirsher
101ae150435SJeff Kirsher MODULE_LICENSE("GPL");
102ae150435SJeff Kirsher MODULE_ALIAS("platform:smc91x");
103ae150435SJeff Kirsher
104ae150435SJeff Kirsher /*
105ae150435SJeff Kirsher * The internal workings of the driver. If you are changing anything
106ae150435SJeff Kirsher * here with the SMC stuff, you should have the datasheet and know
107ae150435SJeff Kirsher * what you are doing.
108ae150435SJeff Kirsher */
109ae150435SJeff Kirsher #define CARDNAME "smc91x"
110ae150435SJeff Kirsher
111ae150435SJeff Kirsher /*
112ae150435SJeff Kirsher * Use power-down feature of the chip
113ae150435SJeff Kirsher */
114ae150435SJeff Kirsher #define POWER_DOWN 1
115ae150435SJeff Kirsher
116ae150435SJeff Kirsher /*
117ae150435SJeff Kirsher * Wait time for memory to be free. This probably shouldn't be
118ae150435SJeff Kirsher * tuned that much, as waiting for this means nothing else happens
119ae150435SJeff Kirsher * in the system
120ae150435SJeff Kirsher */
121ae150435SJeff Kirsher #define MEMORY_WAIT_TIME 16
122ae150435SJeff Kirsher
123ae150435SJeff Kirsher /*
124ae150435SJeff Kirsher * The maximum number of processing loops allowed for each call to the
125ae150435SJeff Kirsher * IRQ handler.
126ae150435SJeff Kirsher */
127ae150435SJeff Kirsher #define MAX_IRQ_LOOPS 8
128ae150435SJeff Kirsher
129ae150435SJeff Kirsher /*
130ae150435SJeff Kirsher * This selects whether TX packets are sent one by one to the SMC91x internal
131ae150435SJeff Kirsher * memory and throttled until transmission completes. This may prevent
132ae150435SJeff Kirsher * RX overruns a litle by keeping much of the memory free for RX packets
133ae150435SJeff Kirsher * but to the expense of reduced TX throughput and increased IRQ overhead.
134ae150435SJeff Kirsher * Note this is not a cure for a too slow data bus or too high IRQ latency.
135ae150435SJeff Kirsher */
136ae150435SJeff Kirsher #define THROTTLE_TX_PKTS 0
137ae150435SJeff Kirsher
138ae150435SJeff Kirsher /*
139ae150435SJeff Kirsher * The MII clock high/low times. 2x this number gives the MII clock period
140ae150435SJeff Kirsher * in microseconds. (was 50, but this gives 6.4ms for each MII transaction!)
141ae150435SJeff Kirsher */
142ae150435SJeff Kirsher #define MII_DELAY 1
143ae150435SJeff Kirsher
144a450a685SZi Shen Lim #define DBG(n, dev, fmt, ...) \
145ae150435SJeff Kirsher do { \
146ae150435SJeff Kirsher if (SMC_DEBUG >= (n)) \
147a450a685SZi Shen Lim netdev_dbg(dev, fmt, ##__VA_ARGS__); \
148ae150435SJeff Kirsher } while (0)
149ae150435SJeff Kirsher
150a450a685SZi Shen Lim #define PRINTK(dev, fmt, ...) \
151a450a685SZi Shen Lim do { \
152a450a685SZi Shen Lim if (SMC_DEBUG > 0) \
153a450a685SZi Shen Lim netdev_info(dev, fmt, ##__VA_ARGS__); \
154a450a685SZi Shen Lim else \
155a450a685SZi Shen Lim netdev_dbg(dev, fmt, ##__VA_ARGS__); \
156a450a685SZi Shen Lim } while (0)
157ae150435SJeff Kirsher
158ae150435SJeff Kirsher #if SMC_DEBUG > 3
PRINT_PKT(u_char * buf,int length)159ae150435SJeff Kirsher static void PRINT_PKT(u_char *buf, int length)
160ae150435SJeff Kirsher {
161ae150435SJeff Kirsher int i;
162ae150435SJeff Kirsher int remainder;
163ae150435SJeff Kirsher int lines;
164ae150435SJeff Kirsher
165ae150435SJeff Kirsher lines = length / 16;
166ae150435SJeff Kirsher remainder = length % 16;
167ae150435SJeff Kirsher
168ae150435SJeff Kirsher for (i = 0; i < lines ; i ++) {
169ae150435SJeff Kirsher int cur;
1706389aa45SBen Boeckel printk(KERN_DEBUG);
171ae150435SJeff Kirsher for (cur = 0; cur < 8; cur++) {
172ae150435SJeff Kirsher u_char a, b;
173ae150435SJeff Kirsher a = *buf++;
174ae150435SJeff Kirsher b = *buf++;
1756389aa45SBen Boeckel pr_cont("%02x%02x ", a, b);
176ae150435SJeff Kirsher }
1776389aa45SBen Boeckel pr_cont("\n");
178ae150435SJeff Kirsher }
1796389aa45SBen Boeckel printk(KERN_DEBUG);
180ae150435SJeff Kirsher for (i = 0; i < remainder/2 ; i++) {
181ae150435SJeff Kirsher u_char a, b;
182ae150435SJeff Kirsher a = *buf++;
183ae150435SJeff Kirsher b = *buf++;
1846389aa45SBen Boeckel pr_cont("%02x%02x ", a, b);
185ae150435SJeff Kirsher }
1866389aa45SBen Boeckel pr_cont("\n");
187ae150435SJeff Kirsher }
188ae150435SJeff Kirsher #else
PRINT_PKT(u_char * buf,int length)189a450a685SZi Shen Lim static inline void PRINT_PKT(u_char *buf, int length) { }
190ae150435SJeff Kirsher #endif
191ae150435SJeff Kirsher
192ae150435SJeff Kirsher
193ae150435SJeff Kirsher /* this enables an interrupt in the interrupt mask register */
194ae150435SJeff Kirsher #define SMC_ENABLE_INT(lp, x) do { \
195ae150435SJeff Kirsher unsigned char mask; \
196ae150435SJeff Kirsher unsigned long smc_enable_flags; \
197ae150435SJeff Kirsher spin_lock_irqsave(&lp->lock, smc_enable_flags); \
198ae150435SJeff Kirsher mask = SMC_GET_INT_MASK(lp); \
199ae150435SJeff Kirsher mask |= (x); \
200ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, mask); \
201ae150435SJeff Kirsher spin_unlock_irqrestore(&lp->lock, smc_enable_flags); \
202ae150435SJeff Kirsher } while (0)
203ae150435SJeff Kirsher
204ae150435SJeff Kirsher /* this disables an interrupt from the interrupt mask register */
205ae150435SJeff Kirsher #define SMC_DISABLE_INT(lp, x) do { \
206ae150435SJeff Kirsher unsigned char mask; \
207ae150435SJeff Kirsher unsigned long smc_disable_flags; \
208ae150435SJeff Kirsher spin_lock_irqsave(&lp->lock, smc_disable_flags); \
209ae150435SJeff Kirsher mask = SMC_GET_INT_MASK(lp); \
210ae150435SJeff Kirsher mask &= ~(x); \
211ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, mask); \
212ae150435SJeff Kirsher spin_unlock_irqrestore(&lp->lock, smc_disable_flags); \
213ae150435SJeff Kirsher } while (0)
214ae150435SJeff Kirsher
215ae150435SJeff Kirsher /*
216ae150435SJeff Kirsher * Wait while MMU is busy. This is usually in the order of a few nanosecs
217ae150435SJeff Kirsher * if at all, but let's avoid deadlocking the system if the hardware
218ae150435SJeff Kirsher * decides to go south.
219ae150435SJeff Kirsher */
220ae150435SJeff Kirsher #define SMC_WAIT_MMU_BUSY(lp) do { \
221ae150435SJeff Kirsher if (unlikely(SMC_GET_MMU_CMD(lp) & MC_BUSY)) { \
222ae150435SJeff Kirsher unsigned long timeout = jiffies + 2; \
223ae150435SJeff Kirsher while (SMC_GET_MMU_CMD(lp) & MC_BUSY) { \
224ae150435SJeff Kirsher if (time_after(jiffies, timeout)) { \
2256389aa45SBen Boeckel netdev_dbg(dev, "timeout %s line %d\n", \
2266389aa45SBen Boeckel __FILE__, __LINE__); \
227ae150435SJeff Kirsher break; \
228ae150435SJeff Kirsher } \
229ae150435SJeff Kirsher cpu_relax(); \
230ae150435SJeff Kirsher } \
231ae150435SJeff Kirsher } \
232ae150435SJeff Kirsher } while (0)
233ae150435SJeff Kirsher
234ae150435SJeff Kirsher
235ae150435SJeff Kirsher /*
236ae150435SJeff Kirsher * this does a soft reset on the device
237ae150435SJeff Kirsher */
smc_reset(struct net_device * dev)238ae150435SJeff Kirsher static void smc_reset(struct net_device *dev)
239ae150435SJeff Kirsher {
240ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
241ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
242ae150435SJeff Kirsher unsigned int ctl, cfg;
243ae150435SJeff Kirsher struct sk_buff *pending_skb;
244ae150435SJeff Kirsher
2456389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
246ae150435SJeff Kirsher
247ae150435SJeff Kirsher /* Disable all interrupts, block TX tasklet */
248ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
249ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
250ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, 0);
251ae150435SJeff Kirsher pending_skb = lp->pending_tx_skb;
252ae150435SJeff Kirsher lp->pending_tx_skb = NULL;
253ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
254ae150435SJeff Kirsher
255ae150435SJeff Kirsher /* free any pending tx skb */
256ae150435SJeff Kirsher if (pending_skb) {
257ae150435SJeff Kirsher dev_kfree_skb(pending_skb);
258ae150435SJeff Kirsher dev->stats.tx_errors++;
259ae150435SJeff Kirsher dev->stats.tx_aborted_errors++;
260ae150435SJeff Kirsher }
261ae150435SJeff Kirsher
262ae150435SJeff Kirsher /*
263ae150435SJeff Kirsher * This resets the registers mostly to defaults, but doesn't
264ae150435SJeff Kirsher * affect EEPROM. That seems unnecessary
265ae150435SJeff Kirsher */
266ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
267ae150435SJeff Kirsher SMC_SET_RCR(lp, RCR_SOFTRST);
268ae150435SJeff Kirsher
269ae150435SJeff Kirsher /*
270ae150435SJeff Kirsher * Setup the Configuration Register
271ae150435SJeff Kirsher * This is necessary because the CONFIG_REG is not affected
272ae150435SJeff Kirsher * by a soft reset
273ae150435SJeff Kirsher */
274ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
275ae150435SJeff Kirsher
276ae150435SJeff Kirsher cfg = CONFIG_DEFAULT;
277ae150435SJeff Kirsher
278ae150435SJeff Kirsher /*
279ae150435SJeff Kirsher * Setup for fast accesses if requested. If the card/system
280ae150435SJeff Kirsher * can't handle it then there will be no recovery except for
281ae150435SJeff Kirsher * a hard reset or power cycle
282ae150435SJeff Kirsher */
283ae150435SJeff Kirsher if (lp->cfg.flags & SMC91X_NOWAIT)
284ae150435SJeff Kirsher cfg |= CONFIG_NO_WAIT;
285ae150435SJeff Kirsher
286ae150435SJeff Kirsher /*
287ae150435SJeff Kirsher * Release from possible power-down state
288ae150435SJeff Kirsher * Configuration register is not affected by Soft Reset
289ae150435SJeff Kirsher */
290ae150435SJeff Kirsher cfg |= CONFIG_EPH_POWER_EN;
291ae150435SJeff Kirsher
292ae150435SJeff Kirsher SMC_SET_CONFIG(lp, cfg);
293ae150435SJeff Kirsher
294ae150435SJeff Kirsher /* this should pause enough for the chip to be happy */
295ae150435SJeff Kirsher /*
296ae150435SJeff Kirsher * elaborate? What does the chip _need_? --jgarzik
297ae150435SJeff Kirsher *
298ae150435SJeff Kirsher * This seems to be undocumented, but something the original
299ae150435SJeff Kirsher * driver(s) have always done. Suspect undocumented timing
300ae150435SJeff Kirsher * info/determined empirically. --rmk
301ae150435SJeff Kirsher */
302ae150435SJeff Kirsher udelay(1);
303ae150435SJeff Kirsher
304ae150435SJeff Kirsher /* Disable transmit and receive functionality */
305ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
306ae150435SJeff Kirsher SMC_SET_RCR(lp, RCR_CLEAR);
307ae150435SJeff Kirsher SMC_SET_TCR(lp, TCR_CLEAR);
308ae150435SJeff Kirsher
309ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
310ae150435SJeff Kirsher ctl = SMC_GET_CTL(lp) | CTL_LE_ENABLE;
311ae150435SJeff Kirsher
312ae150435SJeff Kirsher /*
313ae150435SJeff Kirsher * Set the control register to automatically release successfully
314ae150435SJeff Kirsher * transmitted packets, to make the best use out of our limited
315ae150435SJeff Kirsher * memory
316ae150435SJeff Kirsher */
317ae150435SJeff Kirsher if(!THROTTLE_TX_PKTS)
318ae150435SJeff Kirsher ctl |= CTL_AUTO_RELEASE;
319ae150435SJeff Kirsher else
320ae150435SJeff Kirsher ctl &= ~CTL_AUTO_RELEASE;
321ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl);
322ae150435SJeff Kirsher
323ae150435SJeff Kirsher /* Reset the MMU */
324ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
325ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_RESET);
326ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
327ae150435SJeff Kirsher }
328ae150435SJeff Kirsher
329ae150435SJeff Kirsher /*
330ae150435SJeff Kirsher * Enable Interrupts, Receive, and Transmit
331ae150435SJeff Kirsher */
smc_enable(struct net_device * dev)332ae150435SJeff Kirsher static void smc_enable(struct net_device *dev)
333ae150435SJeff Kirsher {
334ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
335ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
336ae150435SJeff Kirsher int mask;
337ae150435SJeff Kirsher
3386389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
339ae150435SJeff Kirsher
340ae150435SJeff Kirsher /* see the header file for options in TCR/RCR DEFAULT */
341ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
342ae150435SJeff Kirsher SMC_SET_TCR(lp, lp->tcr_cur_mode);
343ae150435SJeff Kirsher SMC_SET_RCR(lp, lp->rcr_cur_mode);
344ae150435SJeff Kirsher
345ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
346ae150435SJeff Kirsher SMC_SET_MAC_ADDR(lp, dev->dev_addr);
347ae150435SJeff Kirsher
348ae150435SJeff Kirsher /* now, enable interrupts */
349ae150435SJeff Kirsher mask = IM_EPH_INT|IM_RX_OVRN_INT|IM_RCV_INT;
350ae150435SJeff Kirsher if (lp->version >= (CHIP_91100 << 4))
351ae150435SJeff Kirsher mask |= IM_MDINT;
352ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
353ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, mask);
354ae150435SJeff Kirsher
355ae150435SJeff Kirsher /*
356ae150435SJeff Kirsher * From this point the register bank must _NOT_ be switched away
357ae150435SJeff Kirsher * to something else than bank 2 without proper locking against
358ae150435SJeff Kirsher * races with any tasklet or interrupt handlers until smc_shutdown()
359ae150435SJeff Kirsher * or smc_reset() is called.
360ae150435SJeff Kirsher */
361ae150435SJeff Kirsher }
362ae150435SJeff Kirsher
363ae150435SJeff Kirsher /*
364ae150435SJeff Kirsher * this puts the device in an inactive state
365ae150435SJeff Kirsher */
smc_shutdown(struct net_device * dev)366ae150435SJeff Kirsher static void smc_shutdown(struct net_device *dev)
367ae150435SJeff Kirsher {
368ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
369ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
370ae150435SJeff Kirsher struct sk_buff *pending_skb;
371ae150435SJeff Kirsher
3726389aa45SBen Boeckel DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
373ae150435SJeff Kirsher
374ae150435SJeff Kirsher /* no more interrupts for me */
375ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
376ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
377ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, 0);
378ae150435SJeff Kirsher pending_skb = lp->pending_tx_skb;
379ae150435SJeff Kirsher lp->pending_tx_skb = NULL;
380ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
381ae150435SJeff Kirsher dev_kfree_skb(pending_skb);
382ae150435SJeff Kirsher
383ae150435SJeff Kirsher /* and tell the card to stay away from that nasty outside world */
384ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
385ae150435SJeff Kirsher SMC_SET_RCR(lp, RCR_CLEAR);
386ae150435SJeff Kirsher SMC_SET_TCR(lp, TCR_CLEAR);
387ae150435SJeff Kirsher
388ae150435SJeff Kirsher #ifdef POWER_DOWN
389ae150435SJeff Kirsher /* finally, shut the chip down */
390ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
391ae150435SJeff Kirsher SMC_SET_CONFIG(lp, SMC_GET_CONFIG(lp) & ~CONFIG_EPH_POWER_EN);
392ae150435SJeff Kirsher #endif
393ae150435SJeff Kirsher }
394ae150435SJeff Kirsher
395ae150435SJeff Kirsher /*
396ae150435SJeff Kirsher * This is the procedure to handle the receipt of a packet.
397ae150435SJeff Kirsher */
smc_rcv(struct net_device * dev)398ae150435SJeff Kirsher static inline void smc_rcv(struct net_device *dev)
399ae150435SJeff Kirsher {
400ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
401ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
402ae150435SJeff Kirsher unsigned int packet_number, status, packet_len;
403ae150435SJeff Kirsher
4046389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
405ae150435SJeff Kirsher
406ae150435SJeff Kirsher packet_number = SMC_GET_RXFIFO(lp);
407ae150435SJeff Kirsher if (unlikely(packet_number & RXFIFO_REMPTY)) {
4086389aa45SBen Boeckel PRINTK(dev, "smc_rcv with nothing on FIFO.\n");
409ae150435SJeff Kirsher return;
410ae150435SJeff Kirsher }
411ae150435SJeff Kirsher
412ae150435SJeff Kirsher /* read from start of packet */
413ae150435SJeff Kirsher SMC_SET_PTR(lp, PTR_READ | PTR_RCV | PTR_AUTOINC);
414ae150435SJeff Kirsher
415ae150435SJeff Kirsher /* First two words are status and packet length */
416ae150435SJeff Kirsher SMC_GET_PKT_HDR(lp, status, packet_len);
417ae150435SJeff Kirsher packet_len &= 0x07ff; /* mask off top bits */
4186389aa45SBen Boeckel DBG(2, dev, "RX PNR 0x%x STATUS 0x%04x LENGTH 0x%04x (%d)\n",
4196389aa45SBen Boeckel packet_number, status, packet_len, packet_len);
420ae150435SJeff Kirsher
421ae150435SJeff Kirsher back:
422ae150435SJeff Kirsher if (unlikely(packet_len < 6 || status & RS_ERRORS)) {
423ae150435SJeff Kirsher if (status & RS_TOOLONG && packet_len <= (1514 + 4 + 6)) {
424ae150435SJeff Kirsher /* accept VLAN packets */
425ae150435SJeff Kirsher status &= ~RS_TOOLONG;
426ae150435SJeff Kirsher goto back;
427ae150435SJeff Kirsher }
428ae150435SJeff Kirsher if (packet_len < 6) {
429ae150435SJeff Kirsher /* bloody hardware */
4306389aa45SBen Boeckel netdev_err(dev, "fubar (rxlen %u status %x\n",
4316389aa45SBen Boeckel packet_len, status);
432ae150435SJeff Kirsher status |= RS_TOOSHORT;
433ae150435SJeff Kirsher }
434ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
435ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_RELEASE);
436ae150435SJeff Kirsher dev->stats.rx_errors++;
437ae150435SJeff Kirsher if (status & RS_ALGNERR)
438ae150435SJeff Kirsher dev->stats.rx_frame_errors++;
439ae150435SJeff Kirsher if (status & (RS_TOOSHORT | RS_TOOLONG))
440ae150435SJeff Kirsher dev->stats.rx_length_errors++;
441ae150435SJeff Kirsher if (status & RS_BADCRC)
442ae150435SJeff Kirsher dev->stats.rx_crc_errors++;
443ae150435SJeff Kirsher } else {
444ae150435SJeff Kirsher struct sk_buff *skb;
445ae150435SJeff Kirsher unsigned char *data;
446ae150435SJeff Kirsher unsigned int data_len;
447ae150435SJeff Kirsher
448ae150435SJeff Kirsher /* set multicast stats */
449ae150435SJeff Kirsher if (status & RS_MULTICAST)
450ae150435SJeff Kirsher dev->stats.multicast++;
451ae150435SJeff Kirsher
452ae150435SJeff Kirsher /*
453ae150435SJeff Kirsher * Actual payload is packet_len - 6 (or 5 if odd byte).
454ae150435SJeff Kirsher * We want skb_reserve(2) and the final ctrl word
455ae150435SJeff Kirsher * (2 bytes, possibly containing the payload odd byte).
456ae150435SJeff Kirsher * Furthermore, we add 2 bytes to allow rounding up to
457ae150435SJeff Kirsher * multiple of 4 bytes on 32 bit buses.
458ae150435SJeff Kirsher * Hence packet_len - 6 + 2 + 2 + 2.
459ae150435SJeff Kirsher */
460dae2e9f4SPradeep A. Dalvi skb = netdev_alloc_skb(dev, packet_len);
461ae150435SJeff Kirsher if (unlikely(skb == NULL)) {
462ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
463ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_RELEASE);
464ae150435SJeff Kirsher dev->stats.rx_dropped++;
465ae150435SJeff Kirsher return;
466ae150435SJeff Kirsher }
467ae150435SJeff Kirsher
468ae150435SJeff Kirsher /* Align IP header to 32 bits */
469ae150435SJeff Kirsher skb_reserve(skb, 2);
470ae150435SJeff Kirsher
471ae150435SJeff Kirsher /* BUG: the LAN91C111 rev A never sets this bit. Force it. */
472ae150435SJeff Kirsher if (lp->version == 0x90)
473ae150435SJeff Kirsher status |= RS_ODDFRAME;
474ae150435SJeff Kirsher
475ae150435SJeff Kirsher /*
476ae150435SJeff Kirsher * If odd length: packet_len - 5,
477ae150435SJeff Kirsher * otherwise packet_len - 6.
478ae150435SJeff Kirsher * With the trailing ctrl byte it's packet_len - 4.
479ae150435SJeff Kirsher */
480ae150435SJeff Kirsher data_len = packet_len - ((status & RS_ODDFRAME) ? 5 : 6);
481ae150435SJeff Kirsher data = skb_put(skb, data_len);
482ae150435SJeff Kirsher SMC_PULL_DATA(lp, data, packet_len - 4);
483ae150435SJeff Kirsher
484ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
485ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_RELEASE);
486ae150435SJeff Kirsher
487ae150435SJeff Kirsher PRINT_PKT(data, packet_len - 4);
488ae150435SJeff Kirsher
489ae150435SJeff Kirsher skb->protocol = eth_type_trans(skb, dev);
490ae150435SJeff Kirsher netif_rx(skb);
491ae150435SJeff Kirsher dev->stats.rx_packets++;
492ae150435SJeff Kirsher dev->stats.rx_bytes += data_len;
493ae150435SJeff Kirsher }
494ae150435SJeff Kirsher }
495ae150435SJeff Kirsher
496ae150435SJeff Kirsher #ifdef CONFIG_SMP
497ae150435SJeff Kirsher /*
498ae150435SJeff Kirsher * On SMP we have the following problem:
499ae150435SJeff Kirsher *
500ae150435SJeff Kirsher * A = smc_hardware_send_pkt()
501ae150435SJeff Kirsher * B = smc_hard_start_xmit()
502ae150435SJeff Kirsher * C = smc_interrupt()
503ae150435SJeff Kirsher *
504ae150435SJeff Kirsher * A and B can never be executed simultaneously. However, at least on UP,
505ae150435SJeff Kirsher * it is possible (and even desirable) for C to interrupt execution of
506ae150435SJeff Kirsher * A or B in order to have better RX reliability and avoid overruns.
507ae150435SJeff Kirsher * C, just like A and B, must have exclusive access to the chip and
508ae150435SJeff Kirsher * each of them must lock against any other concurrent access.
509ae150435SJeff Kirsher * Unfortunately this is not possible to have C suspend execution of A or
510ae150435SJeff Kirsher * B taking place on another CPU. On UP this is no an issue since A and B
511ae150435SJeff Kirsher * are run from softirq context and C from hard IRQ context, and there is
512ae150435SJeff Kirsher * no other CPU where concurrent access can happen.
513ae150435SJeff Kirsher * If ever there is a way to force at least B and C to always be executed
514ae150435SJeff Kirsher * on the same CPU then we could use read/write locks to protect against
515ae150435SJeff Kirsher * any other concurrent access and C would always interrupt B. But life
516ae150435SJeff Kirsher * isn't that easy in a SMP world...
517ae150435SJeff Kirsher */
518ae150435SJeff Kirsher #define smc_special_trylock(lock, flags) \
519ae150435SJeff Kirsher ({ \
520ae150435SJeff Kirsher int __ret; \
521ae150435SJeff Kirsher local_irq_save(flags); \
522ae150435SJeff Kirsher __ret = spin_trylock(lock); \
523ae150435SJeff Kirsher if (!__ret) \
524ae150435SJeff Kirsher local_irq_restore(flags); \
525ae150435SJeff Kirsher __ret; \
526ae150435SJeff Kirsher })
527ae150435SJeff Kirsher #define smc_special_lock(lock, flags) spin_lock_irqsave(lock, flags)
528ae150435SJeff Kirsher #define smc_special_unlock(lock, flags) spin_unlock_irqrestore(lock, flags)
529ae150435SJeff Kirsher #else
530e3ebd894SArnd Bergmann #define smc_special_trylock(lock, flags) ((void)flags, true)
531ae150435SJeff Kirsher #define smc_special_lock(lock, flags) do { flags = 0; } while (0)
532ae150435SJeff Kirsher #define smc_special_unlock(lock, flags) do { flags = 0; } while (0)
533ae150435SJeff Kirsher #endif
534ae150435SJeff Kirsher
535ae150435SJeff Kirsher /*
536ae150435SJeff Kirsher * This is called to actually send a packet to the chip.
537ae150435SJeff Kirsher */
smc_hardware_send_pkt(struct tasklet_struct * t)5387961fb10SAllen Pais static void smc_hardware_send_pkt(struct tasklet_struct *t)
539ae150435SJeff Kirsher {
5407961fb10SAllen Pais struct smc_local *lp = from_tasklet(lp, t, tx_task);
5417961fb10SAllen Pais struct net_device *dev = lp->dev;
542ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
543ae150435SJeff Kirsher struct sk_buff *skb;
544ae150435SJeff Kirsher unsigned int packet_no, len;
545ae150435SJeff Kirsher unsigned char *buf;
546ae150435SJeff Kirsher unsigned long flags;
547ae150435SJeff Kirsher
5486389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
549ae150435SJeff Kirsher
550ae150435SJeff Kirsher if (!smc_special_trylock(&lp->lock, flags)) {
551ae150435SJeff Kirsher netif_stop_queue(dev);
552ae150435SJeff Kirsher tasklet_schedule(&lp->tx_task);
553ae150435SJeff Kirsher return;
554ae150435SJeff Kirsher }
555ae150435SJeff Kirsher
556ae150435SJeff Kirsher skb = lp->pending_tx_skb;
557ae150435SJeff Kirsher if (unlikely(!skb)) {
558ae150435SJeff Kirsher smc_special_unlock(&lp->lock, flags);
559ae150435SJeff Kirsher return;
560ae150435SJeff Kirsher }
561ae150435SJeff Kirsher lp->pending_tx_skb = NULL;
562ae150435SJeff Kirsher
563ae150435SJeff Kirsher packet_no = SMC_GET_AR(lp);
564ae150435SJeff Kirsher if (unlikely(packet_no & AR_FAILED)) {
5656389aa45SBen Boeckel netdev_err(dev, "Memory allocation failed.\n");
566ae150435SJeff Kirsher dev->stats.tx_errors++;
567ae150435SJeff Kirsher dev->stats.tx_fifo_errors++;
568ae150435SJeff Kirsher smc_special_unlock(&lp->lock, flags);
569ae150435SJeff Kirsher goto done;
570ae150435SJeff Kirsher }
571ae150435SJeff Kirsher
572ae150435SJeff Kirsher /* point to the beginning of the packet */
573ae150435SJeff Kirsher SMC_SET_PN(lp, packet_no);
574ae150435SJeff Kirsher SMC_SET_PTR(lp, PTR_AUTOINC);
575ae150435SJeff Kirsher
576ae150435SJeff Kirsher buf = skb->data;
577ae150435SJeff Kirsher len = skb->len;
5786389aa45SBen Boeckel DBG(2, dev, "TX PNR 0x%x LENGTH 0x%04x (%d) BUF 0x%p\n",
5796389aa45SBen Boeckel packet_no, len, len, buf);
580ae150435SJeff Kirsher PRINT_PKT(buf, len);
581ae150435SJeff Kirsher
582ae150435SJeff Kirsher /*
583ae150435SJeff Kirsher * Send the packet length (+6 for status words, length, and ctl.
584ae150435SJeff Kirsher * The card will pad to 64 bytes with zeroes if packet is too small.
585ae150435SJeff Kirsher */
586ae150435SJeff Kirsher SMC_PUT_PKT_HDR(lp, 0, len + 6);
587ae150435SJeff Kirsher
588ae150435SJeff Kirsher /* send the actual data */
589ae150435SJeff Kirsher SMC_PUSH_DATA(lp, buf, len & ~1);
590ae150435SJeff Kirsher
591ae150435SJeff Kirsher /* Send final ctl word with the last byte if there is one */
592d09d747aSRobert Jarzmik SMC_outw(lp, ((len & 1) ? (0x2000 | buf[len - 1]) : 0), ioaddr,
593d09d747aSRobert Jarzmik DATA_REG(lp));
594ae150435SJeff Kirsher
595ae150435SJeff Kirsher /*
596ae150435SJeff Kirsher * If THROTTLE_TX_PKTS is set, we stop the queue here. This will
597ae150435SJeff Kirsher * have the effect of having at most one packet queued for TX
598ae150435SJeff Kirsher * in the chip's memory at all time.
599ae150435SJeff Kirsher *
600ae150435SJeff Kirsher * If THROTTLE_TX_PKTS is not set then the queue is stopped only
601ae150435SJeff Kirsher * when memory allocation (MC_ALLOC) does not succeed right away.
602ae150435SJeff Kirsher */
603ae150435SJeff Kirsher if (THROTTLE_TX_PKTS)
604ae150435SJeff Kirsher netif_stop_queue(dev);
605ae150435SJeff Kirsher
606ae150435SJeff Kirsher /* queue the packet for TX */
607ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_ENQUEUE);
608ae150435SJeff Kirsher smc_special_unlock(&lp->lock, flags);
609ae150435SJeff Kirsher
610860e9538SFlorian Westphal netif_trans_update(dev);
611ae150435SJeff Kirsher dev->stats.tx_packets++;
612ae150435SJeff Kirsher dev->stats.tx_bytes += len;
613ae150435SJeff Kirsher
614ae150435SJeff Kirsher SMC_ENABLE_INT(lp, IM_TX_INT | IM_TX_EMPTY_INT);
615ae150435SJeff Kirsher
616ae150435SJeff Kirsher done: if (!THROTTLE_TX_PKTS)
617ae150435SJeff Kirsher netif_wake_queue(dev);
618ae150435SJeff Kirsher
6194b61fe26SEric W. Biederman dev_consume_skb_any(skb);
620ae150435SJeff Kirsher }
621ae150435SJeff Kirsher
622ae150435SJeff Kirsher /*
623ae150435SJeff Kirsher * Since I am not sure if I will have enough room in the chip's ram
624ae150435SJeff Kirsher * to store the packet, I call this routine which either sends it
625ae150435SJeff Kirsher * now, or set the card to generates an interrupt when ready
626ae150435SJeff Kirsher * for the packet.
627ae150435SJeff Kirsher */
6286323d57fSYueHaibing static netdev_tx_t
smc_hard_start_xmit(struct sk_buff * skb,struct net_device * dev)6296323d57fSYueHaibing smc_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
630ae150435SJeff Kirsher {
631ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
632ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
633ae150435SJeff Kirsher unsigned int numPages, poll_count, status;
634ae150435SJeff Kirsher unsigned long flags;
635ae150435SJeff Kirsher
6366389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
637ae150435SJeff Kirsher
638ae150435SJeff Kirsher BUG_ON(lp->pending_tx_skb != NULL);
639ae150435SJeff Kirsher
640ae150435SJeff Kirsher /*
641ae150435SJeff Kirsher * The MMU wants the number of pages to be the number of 256 bytes
642ae150435SJeff Kirsher * 'pages', minus 1 (since a packet can't ever have 0 pages :))
643ae150435SJeff Kirsher *
644ae150435SJeff Kirsher * The 91C111 ignores the size bits, but earlier models don't.
645ae150435SJeff Kirsher *
646ae150435SJeff Kirsher * Pkt size for allocating is data length +6 (for additional status
647ae150435SJeff Kirsher * words, length and ctl)
648ae150435SJeff Kirsher *
649ae150435SJeff Kirsher * If odd size then last byte is included in ctl word.
650ae150435SJeff Kirsher */
651ae150435SJeff Kirsher numPages = ((skb->len & ~1) + (6 - 1)) >> 8;
652ae150435SJeff Kirsher if (unlikely(numPages > 7)) {
6536389aa45SBen Boeckel netdev_warn(dev, "Far too big packet error.\n");
654ae150435SJeff Kirsher dev->stats.tx_errors++;
655ae150435SJeff Kirsher dev->stats.tx_dropped++;
6564b61fe26SEric W. Biederman dev_kfree_skb_any(skb);
657ae150435SJeff Kirsher return NETDEV_TX_OK;
658ae150435SJeff Kirsher }
659ae150435SJeff Kirsher
660ae150435SJeff Kirsher smc_special_lock(&lp->lock, flags);
661ae150435SJeff Kirsher
662ae150435SJeff Kirsher /* now, try to allocate the memory */
663ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_ALLOC | numPages);
664ae150435SJeff Kirsher
665ae150435SJeff Kirsher /*
666ae150435SJeff Kirsher * Poll the chip for a short amount of time in case the
667ae150435SJeff Kirsher * allocation succeeds quickly.
668ae150435SJeff Kirsher */
669ae150435SJeff Kirsher poll_count = MEMORY_WAIT_TIME;
670ae150435SJeff Kirsher do {
671ae150435SJeff Kirsher status = SMC_GET_INT(lp);
672ae150435SJeff Kirsher if (status & IM_ALLOC_INT) {
673ae150435SJeff Kirsher SMC_ACK_INT(lp, IM_ALLOC_INT);
674ae150435SJeff Kirsher break;
675ae150435SJeff Kirsher }
676ae150435SJeff Kirsher } while (--poll_count);
677ae150435SJeff Kirsher
678ae150435SJeff Kirsher smc_special_unlock(&lp->lock, flags);
679ae150435SJeff Kirsher
680ae150435SJeff Kirsher lp->pending_tx_skb = skb;
681ae150435SJeff Kirsher if (!poll_count) {
682ae150435SJeff Kirsher /* oh well, wait until the chip finds memory later */
683ae150435SJeff Kirsher netif_stop_queue(dev);
6846389aa45SBen Boeckel DBG(2, dev, "TX memory allocation deferred.\n");
685ae150435SJeff Kirsher SMC_ENABLE_INT(lp, IM_ALLOC_INT);
686ae150435SJeff Kirsher } else {
687ae150435SJeff Kirsher /*
688ae150435SJeff Kirsher * Allocation succeeded: push packet to the chip's own memory
689ae150435SJeff Kirsher * immediately.
690ae150435SJeff Kirsher */
6917961fb10SAllen Pais smc_hardware_send_pkt(&lp->tx_task);
692ae150435SJeff Kirsher }
693ae150435SJeff Kirsher
694ae150435SJeff Kirsher return NETDEV_TX_OK;
695ae150435SJeff Kirsher }
696ae150435SJeff Kirsher
697ae150435SJeff Kirsher /*
698ae150435SJeff Kirsher * This handles a TX interrupt, which is only called when:
699ae150435SJeff Kirsher * - a TX error occurred, or
700ae150435SJeff Kirsher * - CTL_AUTO_RELEASE is not set and TX of a packet completed.
701ae150435SJeff Kirsher */
smc_tx(struct net_device * dev)702ae150435SJeff Kirsher static void smc_tx(struct net_device *dev)
703ae150435SJeff Kirsher {
704ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
705ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
7065b320b53SAndrew Lunn unsigned int saved_packet, packet_no, tx_status;
7075b320b53SAndrew Lunn unsigned int pkt_len __always_unused;
708ae150435SJeff Kirsher
7096389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
710ae150435SJeff Kirsher
711ae150435SJeff Kirsher /* If the TX FIFO is empty then nothing to do */
712ae150435SJeff Kirsher packet_no = SMC_GET_TXFIFO(lp);
713ae150435SJeff Kirsher if (unlikely(packet_no & TXFIFO_TEMPTY)) {
7146389aa45SBen Boeckel PRINTK(dev, "smc_tx with nothing on FIFO.\n");
715ae150435SJeff Kirsher return;
716ae150435SJeff Kirsher }
717ae150435SJeff Kirsher
718ae150435SJeff Kirsher /* select packet to read from */
719ae150435SJeff Kirsher saved_packet = SMC_GET_PN(lp);
720ae150435SJeff Kirsher SMC_SET_PN(lp, packet_no);
721ae150435SJeff Kirsher
722ae150435SJeff Kirsher /* read the first word (status word) from this packet */
723ae150435SJeff Kirsher SMC_SET_PTR(lp, PTR_AUTOINC | PTR_READ);
724ae150435SJeff Kirsher SMC_GET_PKT_HDR(lp, tx_status, pkt_len);
7256389aa45SBen Boeckel DBG(2, dev, "TX STATUS 0x%04x PNR 0x%02x\n",
7266389aa45SBen Boeckel tx_status, packet_no);
727ae150435SJeff Kirsher
728ae150435SJeff Kirsher if (!(tx_status & ES_TX_SUC))
729ae150435SJeff Kirsher dev->stats.tx_errors++;
730ae150435SJeff Kirsher
731ae150435SJeff Kirsher if (tx_status & ES_LOSTCARR)
732ae150435SJeff Kirsher dev->stats.tx_carrier_errors++;
733ae150435SJeff Kirsher
734ae150435SJeff Kirsher if (tx_status & (ES_LATCOL | ES_16COL)) {
7356389aa45SBen Boeckel PRINTK(dev, "%s occurred on last xmit\n",
736ae150435SJeff Kirsher (tx_status & ES_LATCOL) ?
737ae150435SJeff Kirsher "late collision" : "too many collisions");
738ae150435SJeff Kirsher dev->stats.tx_window_errors++;
739ae150435SJeff Kirsher if (!(dev->stats.tx_window_errors & 63) && net_ratelimit()) {
7406389aa45SBen Boeckel netdev_info(dev, "unexpectedly large number of bad collisions. Please check duplex setting.\n");
741ae150435SJeff Kirsher }
742ae150435SJeff Kirsher }
743ae150435SJeff Kirsher
744ae150435SJeff Kirsher /* kill the packet */
745ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
746ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_FREEPKT);
747ae150435SJeff Kirsher
748ae150435SJeff Kirsher /* Don't restore Packet Number Reg until busy bit is cleared */
749ae150435SJeff Kirsher SMC_WAIT_MMU_BUSY(lp);
750ae150435SJeff Kirsher SMC_SET_PN(lp, saved_packet);
751ae150435SJeff Kirsher
752ae150435SJeff Kirsher /* re-enable transmit */
753ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
754ae150435SJeff Kirsher SMC_SET_TCR(lp, lp->tcr_cur_mode);
755ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
756ae150435SJeff Kirsher }
757ae150435SJeff Kirsher
758ae150435SJeff Kirsher
759ae150435SJeff Kirsher /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
760ae150435SJeff Kirsher
smc_mii_out(struct net_device * dev,unsigned int val,int bits)761ae150435SJeff Kirsher static void smc_mii_out(struct net_device *dev, unsigned int val, int bits)
762ae150435SJeff Kirsher {
763ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
764ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
765ae150435SJeff Kirsher unsigned int mii_reg, mask;
766ae150435SJeff Kirsher
767ae150435SJeff Kirsher mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
768ae150435SJeff Kirsher mii_reg |= MII_MDOE;
769ae150435SJeff Kirsher
770ae150435SJeff Kirsher for (mask = 1 << (bits - 1); mask; mask >>= 1) {
771ae150435SJeff Kirsher if (val & mask)
772ae150435SJeff Kirsher mii_reg |= MII_MDO;
773ae150435SJeff Kirsher else
774ae150435SJeff Kirsher mii_reg &= ~MII_MDO;
775ae150435SJeff Kirsher
776ae150435SJeff Kirsher SMC_SET_MII(lp, mii_reg);
777ae150435SJeff Kirsher udelay(MII_DELAY);
778ae150435SJeff Kirsher SMC_SET_MII(lp, mii_reg | MII_MCLK);
779ae150435SJeff Kirsher udelay(MII_DELAY);
780ae150435SJeff Kirsher }
781ae150435SJeff Kirsher }
782ae150435SJeff Kirsher
smc_mii_in(struct net_device * dev,int bits)783ae150435SJeff Kirsher static unsigned int smc_mii_in(struct net_device *dev, int bits)
784ae150435SJeff Kirsher {
785ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
786ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
787ae150435SJeff Kirsher unsigned int mii_reg, mask, val;
788ae150435SJeff Kirsher
789ae150435SJeff Kirsher mii_reg = SMC_GET_MII(lp) & ~(MII_MCLK | MII_MDOE | MII_MDO);
790ae150435SJeff Kirsher SMC_SET_MII(lp, mii_reg);
791ae150435SJeff Kirsher
792ae150435SJeff Kirsher for (mask = 1 << (bits - 1), val = 0; mask; mask >>= 1) {
793ae150435SJeff Kirsher if (SMC_GET_MII(lp) & MII_MDI)
794ae150435SJeff Kirsher val |= mask;
795ae150435SJeff Kirsher
796ae150435SJeff Kirsher SMC_SET_MII(lp, mii_reg);
797ae150435SJeff Kirsher udelay(MII_DELAY);
798ae150435SJeff Kirsher SMC_SET_MII(lp, mii_reg | MII_MCLK);
799ae150435SJeff Kirsher udelay(MII_DELAY);
800ae150435SJeff Kirsher }
801ae150435SJeff Kirsher
802ae150435SJeff Kirsher return val;
803ae150435SJeff Kirsher }
804ae150435SJeff Kirsher
805ae150435SJeff Kirsher /*
806ae150435SJeff Kirsher * Reads a register from the MII Management serial interface
807ae150435SJeff Kirsher */
smc_phy_read(struct net_device * dev,int phyaddr,int phyreg)808ae150435SJeff Kirsher static int smc_phy_read(struct net_device *dev, int phyaddr, int phyreg)
809ae150435SJeff Kirsher {
810ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
811ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
812ae150435SJeff Kirsher unsigned int phydata;
813ae150435SJeff Kirsher
814ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 3);
815ae150435SJeff Kirsher
816ae150435SJeff Kirsher /* Idle - 32 ones */
817ae150435SJeff Kirsher smc_mii_out(dev, 0xffffffff, 32);
818ae150435SJeff Kirsher
819ae150435SJeff Kirsher /* Start code (01) + read (10) + phyaddr + phyreg */
820ae150435SJeff Kirsher smc_mii_out(dev, 6 << 10 | phyaddr << 5 | phyreg, 14);
821ae150435SJeff Kirsher
822ae150435SJeff Kirsher /* Turnaround (2bits) + phydata */
823ae150435SJeff Kirsher phydata = smc_mii_in(dev, 18);
824ae150435SJeff Kirsher
825ae150435SJeff Kirsher /* Return to idle state */
826ae150435SJeff Kirsher SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
827ae150435SJeff Kirsher
8286389aa45SBen Boeckel DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
829ae150435SJeff Kirsher __func__, phyaddr, phyreg, phydata);
830ae150435SJeff Kirsher
831ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
832ae150435SJeff Kirsher return phydata;
833ae150435SJeff Kirsher }
834ae150435SJeff Kirsher
835ae150435SJeff Kirsher /*
836ae150435SJeff Kirsher * Writes a register to the MII Management serial interface
837ae150435SJeff Kirsher */
smc_phy_write(struct net_device * dev,int phyaddr,int phyreg,int phydata)838ae150435SJeff Kirsher static void smc_phy_write(struct net_device *dev, int phyaddr, int phyreg,
839ae150435SJeff Kirsher int phydata)
840ae150435SJeff Kirsher {
841ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
842ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
843ae150435SJeff Kirsher
844ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 3);
845ae150435SJeff Kirsher
846ae150435SJeff Kirsher /* Idle - 32 ones */
847ae150435SJeff Kirsher smc_mii_out(dev, 0xffffffff, 32);
848ae150435SJeff Kirsher
849ae150435SJeff Kirsher /* Start code (01) + write (01) + phyaddr + phyreg + turnaround + phydata */
850ae150435SJeff Kirsher smc_mii_out(dev, 5 << 28 | phyaddr << 23 | phyreg << 18 | 2 << 16 | phydata, 32);
851ae150435SJeff Kirsher
852ae150435SJeff Kirsher /* Return to idle state */
853ae150435SJeff Kirsher SMC_SET_MII(lp, SMC_GET_MII(lp) & ~(MII_MCLK|MII_MDOE|MII_MDO));
854ae150435SJeff Kirsher
8556389aa45SBen Boeckel DBG(3, dev, "%s: phyaddr=0x%x, phyreg=0x%x, phydata=0x%x\n",
856ae150435SJeff Kirsher __func__, phyaddr, phyreg, phydata);
857ae150435SJeff Kirsher
858ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
859ae150435SJeff Kirsher }
860ae150435SJeff Kirsher
861ae150435SJeff Kirsher /*
862ae150435SJeff Kirsher * Finds and reports the PHY address
863ae150435SJeff Kirsher */
smc_phy_detect(struct net_device * dev)864ae150435SJeff Kirsher static void smc_phy_detect(struct net_device *dev)
865ae150435SJeff Kirsher {
866ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
867ae150435SJeff Kirsher int phyaddr;
868ae150435SJeff Kirsher
8696389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
870ae150435SJeff Kirsher
871ae150435SJeff Kirsher lp->phy_type = 0;
872ae150435SJeff Kirsher
873ae150435SJeff Kirsher /*
874ae150435SJeff Kirsher * Scan all 32 PHY addresses if necessary, starting at
875ae150435SJeff Kirsher * PHY#1 to PHY#31, and then PHY#0 last.
876ae150435SJeff Kirsher */
877ae150435SJeff Kirsher for (phyaddr = 1; phyaddr < 33; ++phyaddr) {
878ae150435SJeff Kirsher unsigned int id1, id2;
879ae150435SJeff Kirsher
880ae150435SJeff Kirsher /* Read the PHY identifiers */
881ae150435SJeff Kirsher id1 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID1);
882ae150435SJeff Kirsher id2 = smc_phy_read(dev, phyaddr & 31, MII_PHYSID2);
883ae150435SJeff Kirsher
8846389aa45SBen Boeckel DBG(3, dev, "phy_id1=0x%x, phy_id2=0x%x\n",
8856389aa45SBen Boeckel id1, id2);
886ae150435SJeff Kirsher
887ae150435SJeff Kirsher /* Make sure it is a valid identifier */
888ae150435SJeff Kirsher if (id1 != 0x0000 && id1 != 0xffff && id1 != 0x8000 &&
889ae150435SJeff Kirsher id2 != 0x0000 && id2 != 0xffff && id2 != 0x8000) {
890ae150435SJeff Kirsher /* Save the PHY's address */
891ae150435SJeff Kirsher lp->mii.phy_id = phyaddr & 31;
892ae150435SJeff Kirsher lp->phy_type = id1 << 16 | id2;
893ae150435SJeff Kirsher break;
894ae150435SJeff Kirsher }
895ae150435SJeff Kirsher }
896ae150435SJeff Kirsher }
897ae150435SJeff Kirsher
898ae150435SJeff Kirsher /*
899ae150435SJeff Kirsher * Sets the PHY to a configuration as determined by the user
900ae150435SJeff Kirsher */
smc_phy_fixed(struct net_device * dev)901ae150435SJeff Kirsher static int smc_phy_fixed(struct net_device *dev)
902ae150435SJeff Kirsher {
903ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
904ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
905ae150435SJeff Kirsher int phyaddr = lp->mii.phy_id;
906ae150435SJeff Kirsher int bmcr, cfg1;
907ae150435SJeff Kirsher
9086389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
909ae150435SJeff Kirsher
910ae150435SJeff Kirsher /* Enter Link Disable state */
911ae150435SJeff Kirsher cfg1 = smc_phy_read(dev, phyaddr, PHY_CFG1_REG);
912ae150435SJeff Kirsher cfg1 |= PHY_CFG1_LNKDIS;
913ae150435SJeff Kirsher smc_phy_write(dev, phyaddr, PHY_CFG1_REG, cfg1);
914ae150435SJeff Kirsher
915ae150435SJeff Kirsher /*
916ae150435SJeff Kirsher * Set our fixed capabilities
917ae150435SJeff Kirsher * Disable auto-negotiation
918ae150435SJeff Kirsher */
919ae150435SJeff Kirsher bmcr = 0;
920ae150435SJeff Kirsher
921ae150435SJeff Kirsher if (lp->ctl_rfduplx)
922ae150435SJeff Kirsher bmcr |= BMCR_FULLDPLX;
923ae150435SJeff Kirsher
924ae150435SJeff Kirsher if (lp->ctl_rspeed == 100)
925ae150435SJeff Kirsher bmcr |= BMCR_SPEED100;
926ae150435SJeff Kirsher
927ae150435SJeff Kirsher /* Write our capabilities to the phy control register */
928ae150435SJeff Kirsher smc_phy_write(dev, phyaddr, MII_BMCR, bmcr);
929ae150435SJeff Kirsher
930ae150435SJeff Kirsher /* Re-Configure the Receive/Phy Control register */
931ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
932ae150435SJeff Kirsher SMC_SET_RPC(lp, lp->rpc_cur_mode);
933ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
934ae150435SJeff Kirsher
935ae150435SJeff Kirsher return 1;
936ae150435SJeff Kirsher }
937ae150435SJeff Kirsher
93849ce9c2cSBen Hutchings /**
939ae150435SJeff Kirsher * smc_phy_reset - reset the phy
940ae150435SJeff Kirsher * @dev: net device
941ae150435SJeff Kirsher * @phy: phy address
942ae150435SJeff Kirsher *
943ae150435SJeff Kirsher * Issue a software reset for the specified PHY and
944ae150435SJeff Kirsher * wait up to 100ms for the reset to complete. We should
945ae150435SJeff Kirsher * not access the PHY for 50ms after issuing the reset.
946ae150435SJeff Kirsher *
947ae150435SJeff Kirsher * The time to wait appears to be dependent on the PHY.
948ae150435SJeff Kirsher *
949ae150435SJeff Kirsher * Must be called with lp->lock locked.
950ae150435SJeff Kirsher */
smc_phy_reset(struct net_device * dev,int phy)951ae150435SJeff Kirsher static int smc_phy_reset(struct net_device *dev, int phy)
952ae150435SJeff Kirsher {
953ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
954ae150435SJeff Kirsher unsigned int bmcr;
955ae150435SJeff Kirsher int timeout;
956ae150435SJeff Kirsher
957ae150435SJeff Kirsher smc_phy_write(dev, phy, MII_BMCR, BMCR_RESET);
958ae150435SJeff Kirsher
959ae150435SJeff Kirsher for (timeout = 2; timeout; timeout--) {
960ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
961ae150435SJeff Kirsher msleep(50);
962ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
963ae150435SJeff Kirsher
964ae150435SJeff Kirsher bmcr = smc_phy_read(dev, phy, MII_BMCR);
965ae150435SJeff Kirsher if (!(bmcr & BMCR_RESET))
966ae150435SJeff Kirsher break;
967ae150435SJeff Kirsher }
968ae150435SJeff Kirsher
969ae150435SJeff Kirsher return bmcr & BMCR_RESET;
970ae150435SJeff Kirsher }
971ae150435SJeff Kirsher
97249ce9c2cSBen Hutchings /**
973ae150435SJeff Kirsher * smc_phy_powerdown - powerdown phy
974ae150435SJeff Kirsher * @dev: net device
975ae150435SJeff Kirsher *
976ae150435SJeff Kirsher * Power down the specified PHY
977ae150435SJeff Kirsher */
smc_phy_powerdown(struct net_device * dev)978ae150435SJeff Kirsher static void smc_phy_powerdown(struct net_device *dev)
979ae150435SJeff Kirsher {
980ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
981ae150435SJeff Kirsher unsigned int bmcr;
982ae150435SJeff Kirsher int phy = lp->mii.phy_id;
983ae150435SJeff Kirsher
984ae150435SJeff Kirsher if (lp->phy_type == 0)
985ae150435SJeff Kirsher return;
986ae150435SJeff Kirsher
987ae150435SJeff Kirsher /* We need to ensure that no calls to smc_phy_configure are
988ae150435SJeff Kirsher pending.
989ae150435SJeff Kirsher */
990ae150435SJeff Kirsher cancel_work_sync(&lp->phy_configure);
991ae150435SJeff Kirsher
992ae150435SJeff Kirsher bmcr = smc_phy_read(dev, phy, MII_BMCR);
993ae150435SJeff Kirsher smc_phy_write(dev, phy, MII_BMCR, bmcr | BMCR_PDOWN);
994ae150435SJeff Kirsher }
995ae150435SJeff Kirsher
99649ce9c2cSBen Hutchings /**
997ae150435SJeff Kirsher * smc_phy_check_media - check the media status and adjust TCR
998ae150435SJeff Kirsher * @dev: net device
999ae150435SJeff Kirsher * @init: set true for initialisation
1000ae150435SJeff Kirsher *
1001ae150435SJeff Kirsher * Select duplex mode depending on negotiation state. This
1002ae150435SJeff Kirsher * also updates our carrier state.
1003ae150435SJeff Kirsher */
smc_phy_check_media(struct net_device * dev,int init)1004ae150435SJeff Kirsher static void smc_phy_check_media(struct net_device *dev, int init)
1005ae150435SJeff Kirsher {
1006ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1007ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1008ae150435SJeff Kirsher
1009ae150435SJeff Kirsher if (mii_check_media(&lp->mii, netif_msg_link(lp), init)) {
1010ae150435SJeff Kirsher /* duplex state has changed */
1011ae150435SJeff Kirsher if (lp->mii.full_duplex) {
1012ae150435SJeff Kirsher lp->tcr_cur_mode |= TCR_SWFDUP;
1013ae150435SJeff Kirsher } else {
1014ae150435SJeff Kirsher lp->tcr_cur_mode &= ~TCR_SWFDUP;
1015ae150435SJeff Kirsher }
1016ae150435SJeff Kirsher
1017ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1018ae150435SJeff Kirsher SMC_SET_TCR(lp, lp->tcr_cur_mode);
1019ae150435SJeff Kirsher }
1020ae150435SJeff Kirsher }
1021ae150435SJeff Kirsher
1022ae150435SJeff Kirsher /*
1023ae150435SJeff Kirsher * Configures the specified PHY through the MII management interface
1024ae150435SJeff Kirsher * using Autonegotiation.
1025ae150435SJeff Kirsher * Calls smc_phy_fixed() if the user has requested a certain config.
1026ae150435SJeff Kirsher * If RPC ANEG bit is set, the media selection is dependent purely on
1027ae150435SJeff Kirsher * the selection by the MII (either in the MII BMCR reg or the result
1028ae150435SJeff Kirsher * of autonegotiation.) If the RPC ANEG bit is cleared, the selection
1029ae150435SJeff Kirsher * is controlled by the RPC SPEED and RPC DPLX bits.
1030ae150435SJeff Kirsher */
smc_phy_configure(struct work_struct * work)1031ae150435SJeff Kirsher static void smc_phy_configure(struct work_struct *work)
1032ae150435SJeff Kirsher {
1033ae150435SJeff Kirsher struct smc_local *lp =
1034ae150435SJeff Kirsher container_of(work, struct smc_local, phy_configure);
1035ae150435SJeff Kirsher struct net_device *dev = lp->dev;
1036ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1037ae150435SJeff Kirsher int phyaddr = lp->mii.phy_id;
1038ae150435SJeff Kirsher int my_phy_caps; /* My PHY capabilities */
1039ae150435SJeff Kirsher int my_ad_caps; /* My Advertised capabilities */
1040ae150435SJeff Kirsher
10416389aa45SBen Boeckel DBG(3, dev, "smc_program_phy()\n");
1042ae150435SJeff Kirsher
1043ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1044ae150435SJeff Kirsher
1045ae150435SJeff Kirsher /*
1046ae150435SJeff Kirsher * We should not be called if phy_type is zero.
1047ae150435SJeff Kirsher */
1048ae150435SJeff Kirsher if (lp->phy_type == 0)
1049ae150435SJeff Kirsher goto smc_phy_configure_exit;
1050ae150435SJeff Kirsher
1051ae150435SJeff Kirsher if (smc_phy_reset(dev, phyaddr)) {
10526389aa45SBen Boeckel netdev_info(dev, "PHY reset timed out\n");
1053ae150435SJeff Kirsher goto smc_phy_configure_exit;
1054ae150435SJeff Kirsher }
1055ae150435SJeff Kirsher
1056ae150435SJeff Kirsher /*
1057ae150435SJeff Kirsher * Enable PHY Interrupts (for register 18)
1058ae150435SJeff Kirsher * Interrupts listed here are disabled
1059ae150435SJeff Kirsher */
1060ae150435SJeff Kirsher smc_phy_write(dev, phyaddr, PHY_MASK_REG,
1061ae150435SJeff Kirsher PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
1062ae150435SJeff Kirsher PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
1063ae150435SJeff Kirsher PHY_INT_SPDDET | PHY_INT_DPLXDET);
1064ae150435SJeff Kirsher
1065ae150435SJeff Kirsher /* Configure the Receive/Phy Control register */
1066ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1067ae150435SJeff Kirsher SMC_SET_RPC(lp, lp->rpc_cur_mode);
1068ae150435SJeff Kirsher
1069ae150435SJeff Kirsher /* If the user requested no auto neg, then go set his request */
1070ae150435SJeff Kirsher if (lp->mii.force_media) {
1071ae150435SJeff Kirsher smc_phy_fixed(dev);
1072ae150435SJeff Kirsher goto smc_phy_configure_exit;
1073ae150435SJeff Kirsher }
1074ae150435SJeff Kirsher
1075ae150435SJeff Kirsher /* Copy our capabilities from MII_BMSR to MII_ADVERTISE */
1076ae150435SJeff Kirsher my_phy_caps = smc_phy_read(dev, phyaddr, MII_BMSR);
1077ae150435SJeff Kirsher
1078ae150435SJeff Kirsher if (!(my_phy_caps & BMSR_ANEGCAPABLE)) {
10796389aa45SBen Boeckel netdev_info(dev, "Auto negotiation NOT supported\n");
1080ae150435SJeff Kirsher smc_phy_fixed(dev);
1081ae150435SJeff Kirsher goto smc_phy_configure_exit;
1082ae150435SJeff Kirsher }
1083ae150435SJeff Kirsher
1084ae150435SJeff Kirsher my_ad_caps = ADVERTISE_CSMA; /* I am CSMA capable */
1085ae150435SJeff Kirsher
1086ae150435SJeff Kirsher if (my_phy_caps & BMSR_100BASE4)
1087ae150435SJeff Kirsher my_ad_caps |= ADVERTISE_100BASE4;
1088ae150435SJeff Kirsher if (my_phy_caps & BMSR_100FULL)
1089ae150435SJeff Kirsher my_ad_caps |= ADVERTISE_100FULL;
1090ae150435SJeff Kirsher if (my_phy_caps & BMSR_100HALF)
1091ae150435SJeff Kirsher my_ad_caps |= ADVERTISE_100HALF;
1092ae150435SJeff Kirsher if (my_phy_caps & BMSR_10FULL)
1093ae150435SJeff Kirsher my_ad_caps |= ADVERTISE_10FULL;
1094ae150435SJeff Kirsher if (my_phy_caps & BMSR_10HALF)
1095ae150435SJeff Kirsher my_ad_caps |= ADVERTISE_10HALF;
1096ae150435SJeff Kirsher
1097ae150435SJeff Kirsher /* Disable capabilities not selected by our user */
1098ae150435SJeff Kirsher if (lp->ctl_rspeed != 100)
1099ae150435SJeff Kirsher my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
1100ae150435SJeff Kirsher
1101ae150435SJeff Kirsher if (!lp->ctl_rfduplx)
1102ae150435SJeff Kirsher my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
1103ae150435SJeff Kirsher
1104ae150435SJeff Kirsher /* Update our Auto-Neg Advertisement Register */
1105ae150435SJeff Kirsher smc_phy_write(dev, phyaddr, MII_ADVERTISE, my_ad_caps);
1106ae150435SJeff Kirsher lp->mii.advertising = my_ad_caps;
1107ae150435SJeff Kirsher
1108ae150435SJeff Kirsher /*
1109ae150435SJeff Kirsher * Read the register back. Without this, it appears that when
1110ae150435SJeff Kirsher * auto-negotiation is restarted, sometimes it isn't ready and
1111ae150435SJeff Kirsher * the link does not come up.
1112ae150435SJeff Kirsher */
11139984c0bbSLuo Jiaxing smc_phy_read(dev, phyaddr, MII_ADVERTISE);
1114ae150435SJeff Kirsher
11156389aa45SBen Boeckel DBG(2, dev, "phy caps=%x\n", my_phy_caps);
11166389aa45SBen Boeckel DBG(2, dev, "phy advertised caps=%x\n", my_ad_caps);
1117ae150435SJeff Kirsher
1118ae150435SJeff Kirsher /* Restart auto-negotiation process in order to advertise my caps */
1119ae150435SJeff Kirsher smc_phy_write(dev, phyaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
1120ae150435SJeff Kirsher
1121ae150435SJeff Kirsher smc_phy_check_media(dev, 1);
1122ae150435SJeff Kirsher
1123ae150435SJeff Kirsher smc_phy_configure_exit:
1124ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1125ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1126ae150435SJeff Kirsher }
1127ae150435SJeff Kirsher
1128ae150435SJeff Kirsher /*
1129ae150435SJeff Kirsher * smc_phy_interrupt
1130ae150435SJeff Kirsher *
1131ae150435SJeff Kirsher * Purpose: Handle interrupts relating to PHY register 18. This is
1132ae150435SJeff Kirsher * called from the "hard" interrupt handler under our private spinlock.
1133ae150435SJeff Kirsher */
smc_phy_interrupt(struct net_device * dev)1134ae150435SJeff Kirsher static void smc_phy_interrupt(struct net_device *dev)
1135ae150435SJeff Kirsher {
1136ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1137ae150435SJeff Kirsher int phyaddr = lp->mii.phy_id;
1138ae150435SJeff Kirsher int phy18;
1139ae150435SJeff Kirsher
11406389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
1141ae150435SJeff Kirsher
1142ae150435SJeff Kirsher if (lp->phy_type == 0)
1143ae150435SJeff Kirsher return;
1144ae150435SJeff Kirsher
1145ae150435SJeff Kirsher for(;;) {
1146ae150435SJeff Kirsher smc_phy_check_media(dev, 0);
1147ae150435SJeff Kirsher
1148ae150435SJeff Kirsher /* Read PHY Register 18, Status Output */
1149ae150435SJeff Kirsher phy18 = smc_phy_read(dev, phyaddr, PHY_INT_REG);
1150ae150435SJeff Kirsher if ((phy18 & PHY_INT_INT) == 0)
1151ae150435SJeff Kirsher break;
1152ae150435SJeff Kirsher }
1153ae150435SJeff Kirsher }
1154ae150435SJeff Kirsher
1155ae150435SJeff Kirsher /*--- END PHY CONTROL AND CONFIGURATION-------------------------------------*/
1156ae150435SJeff Kirsher
smc_10bt_check_media(struct net_device * dev,int init)1157ae150435SJeff Kirsher static void smc_10bt_check_media(struct net_device *dev, int init)
1158ae150435SJeff Kirsher {
1159ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1160ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1161ae150435SJeff Kirsher unsigned int old_carrier, new_carrier;
1162ae150435SJeff Kirsher
1163ae150435SJeff Kirsher old_carrier = netif_carrier_ok(dev) ? 1 : 0;
1164ae150435SJeff Kirsher
1165ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1166ae150435SJeff Kirsher new_carrier = (SMC_GET_EPH_STATUS(lp) & ES_LINK_OK) ? 1 : 0;
1167ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1168ae150435SJeff Kirsher
1169ae150435SJeff Kirsher if (init || (old_carrier != new_carrier)) {
1170ae150435SJeff Kirsher if (!new_carrier) {
1171ae150435SJeff Kirsher netif_carrier_off(dev);
1172ae150435SJeff Kirsher } else {
1173ae150435SJeff Kirsher netif_carrier_on(dev);
1174ae150435SJeff Kirsher }
1175ae150435SJeff Kirsher if (netif_msg_link(lp))
11766389aa45SBen Boeckel netdev_info(dev, "link %s\n",
1177ae150435SJeff Kirsher new_carrier ? "up" : "down");
1178ae150435SJeff Kirsher }
1179ae150435SJeff Kirsher }
1180ae150435SJeff Kirsher
smc_eph_interrupt(struct net_device * dev)1181ae150435SJeff Kirsher static void smc_eph_interrupt(struct net_device *dev)
1182ae150435SJeff Kirsher {
1183ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1184ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1185ae150435SJeff Kirsher unsigned int ctl;
1186ae150435SJeff Kirsher
1187ae150435SJeff Kirsher smc_10bt_check_media(dev, 0);
1188ae150435SJeff Kirsher
1189ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
1190ae150435SJeff Kirsher ctl = SMC_GET_CTL(lp);
1191ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl & ~CTL_LE_ENABLE);
1192ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl);
1193ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1194ae150435SJeff Kirsher }
1195ae150435SJeff Kirsher
1196ae150435SJeff Kirsher /*
1197ae150435SJeff Kirsher * This is the main routine of the driver, to handle the device when
1198ae150435SJeff Kirsher * it needs some attention.
1199ae150435SJeff Kirsher */
smc_interrupt(int irq,void * dev_id)1200ae150435SJeff Kirsher static irqreturn_t smc_interrupt(int irq, void *dev_id)
1201ae150435SJeff Kirsher {
1202ae150435SJeff Kirsher struct net_device *dev = dev_id;
1203ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1204ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1205ae150435SJeff Kirsher int status, mask, timeout, card_stats;
1206ae150435SJeff Kirsher int saved_pointer;
1207ae150435SJeff Kirsher
12086389aa45SBen Boeckel DBG(3, dev, "%s\n", __func__);
1209ae150435SJeff Kirsher
1210ae150435SJeff Kirsher spin_lock(&lp->lock);
1211ae150435SJeff Kirsher
1212ae150435SJeff Kirsher /* A preamble may be used when there is a potential race
1213ae150435SJeff Kirsher * between the interruptible transmit functions and this
1214ae150435SJeff Kirsher * ISR. */
1215ae150435SJeff Kirsher SMC_INTERRUPT_PREAMBLE;
1216ae150435SJeff Kirsher
1217ae150435SJeff Kirsher saved_pointer = SMC_GET_PTR(lp);
1218ae150435SJeff Kirsher mask = SMC_GET_INT_MASK(lp);
1219ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, 0);
1220ae150435SJeff Kirsher
1221ae150435SJeff Kirsher /* set a timeout value, so I don't stay here forever */
1222ae150435SJeff Kirsher timeout = MAX_IRQ_LOOPS;
1223ae150435SJeff Kirsher
1224ae150435SJeff Kirsher do {
1225ae150435SJeff Kirsher status = SMC_GET_INT(lp);
1226ae150435SJeff Kirsher
12276389aa45SBen Boeckel DBG(2, dev, "INT 0x%02x MASK 0x%02x MEM 0x%04x FIFO 0x%04x\n",
12286389aa45SBen Boeckel status, mask,
1229ae150435SJeff Kirsher ({ int meminfo; SMC_SELECT_BANK(lp, 0);
1230ae150435SJeff Kirsher meminfo = SMC_GET_MIR(lp);
1231ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2); meminfo; }),
1232ae150435SJeff Kirsher SMC_GET_FIFO(lp));
1233ae150435SJeff Kirsher
1234ae150435SJeff Kirsher status &= mask;
1235ae150435SJeff Kirsher if (!status)
1236ae150435SJeff Kirsher break;
1237ae150435SJeff Kirsher
1238ae150435SJeff Kirsher if (status & IM_TX_INT) {
1239ae150435SJeff Kirsher /* do this before RX as it will free memory quickly */
12406389aa45SBen Boeckel DBG(3, dev, "TX int\n");
1241ae150435SJeff Kirsher smc_tx(dev);
1242ae150435SJeff Kirsher SMC_ACK_INT(lp, IM_TX_INT);
1243ae150435SJeff Kirsher if (THROTTLE_TX_PKTS)
1244ae150435SJeff Kirsher netif_wake_queue(dev);
1245ae150435SJeff Kirsher } else if (status & IM_RCV_INT) {
12466389aa45SBen Boeckel DBG(3, dev, "RX irq\n");
1247ae150435SJeff Kirsher smc_rcv(dev);
1248ae150435SJeff Kirsher } else if (status & IM_ALLOC_INT) {
12496389aa45SBen Boeckel DBG(3, dev, "Allocation irq\n");
1250ae150435SJeff Kirsher tasklet_hi_schedule(&lp->tx_task);
1251ae150435SJeff Kirsher mask &= ~IM_ALLOC_INT;
1252ae150435SJeff Kirsher } else if (status & IM_TX_EMPTY_INT) {
12536389aa45SBen Boeckel DBG(3, dev, "TX empty\n");
1254ae150435SJeff Kirsher mask &= ~IM_TX_EMPTY_INT;
1255ae150435SJeff Kirsher
1256ae150435SJeff Kirsher /* update stats */
1257ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1258ae150435SJeff Kirsher card_stats = SMC_GET_COUNTER(lp);
1259ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1260ae150435SJeff Kirsher
1261ae150435SJeff Kirsher /* single collisions */
1262ae150435SJeff Kirsher dev->stats.collisions += card_stats & 0xF;
1263ae150435SJeff Kirsher card_stats >>= 4;
1264ae150435SJeff Kirsher
1265ae150435SJeff Kirsher /* multiple collisions */
1266ae150435SJeff Kirsher dev->stats.collisions += card_stats & 0xF;
1267ae150435SJeff Kirsher } else if (status & IM_RX_OVRN_INT) {
12686389aa45SBen Boeckel DBG(1, dev, "RX overrun (EPH_ST 0x%04x)\n",
1269ae150435SJeff Kirsher ({ int eph_st; SMC_SELECT_BANK(lp, 0);
1270ae150435SJeff Kirsher eph_st = SMC_GET_EPH_STATUS(lp);
1271ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2); eph_st; }));
1272ae150435SJeff Kirsher SMC_ACK_INT(lp, IM_RX_OVRN_INT);
1273ae150435SJeff Kirsher dev->stats.rx_errors++;
1274ae150435SJeff Kirsher dev->stats.rx_fifo_errors++;
1275ae150435SJeff Kirsher } else if (status & IM_EPH_INT) {
1276ae150435SJeff Kirsher smc_eph_interrupt(dev);
1277ae150435SJeff Kirsher } else if (status & IM_MDINT) {
1278ae150435SJeff Kirsher SMC_ACK_INT(lp, IM_MDINT);
1279ae150435SJeff Kirsher smc_phy_interrupt(dev);
1280ae150435SJeff Kirsher } else if (status & IM_ERCV_INT) {
1281ae150435SJeff Kirsher SMC_ACK_INT(lp, IM_ERCV_INT);
12826389aa45SBen Boeckel PRINTK(dev, "UNSUPPORTED: ERCV INTERRUPT\n");
1283ae150435SJeff Kirsher }
1284ae150435SJeff Kirsher } while (--timeout);
1285ae150435SJeff Kirsher
1286ae150435SJeff Kirsher /* restore register states */
1287ae150435SJeff Kirsher SMC_SET_PTR(lp, saved_pointer);
1288ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, mask);
1289ae150435SJeff Kirsher spin_unlock(&lp->lock);
1290ae150435SJeff Kirsher
1291ae150435SJeff Kirsher #ifndef CONFIG_NET_POLL_CONTROLLER
1292ae150435SJeff Kirsher if (timeout == MAX_IRQ_LOOPS)
12936389aa45SBen Boeckel PRINTK(dev, "spurious interrupt (mask = 0x%02x)\n",
12946389aa45SBen Boeckel mask);
1295ae150435SJeff Kirsher #endif
12966389aa45SBen Boeckel DBG(3, dev, "Interrupt done (%d loops)\n",
12976389aa45SBen Boeckel MAX_IRQ_LOOPS - timeout);
1298ae150435SJeff Kirsher
1299ae150435SJeff Kirsher /*
1300ae150435SJeff Kirsher * We return IRQ_HANDLED unconditionally here even if there was
1301ae150435SJeff Kirsher * nothing to do. There is a possibility that a packet might
1302ae150435SJeff Kirsher * get enqueued into the chip right after TX_EMPTY_INT is raised
1303ae150435SJeff Kirsher * but just before the CPU acknowledges the IRQ.
1304ae150435SJeff Kirsher * Better take an unneeded IRQ in some occasions than complexifying
1305ae150435SJeff Kirsher * the code for all cases.
1306ae150435SJeff Kirsher */
1307ae150435SJeff Kirsher return IRQ_HANDLED;
1308ae150435SJeff Kirsher }
1309ae150435SJeff Kirsher
1310ae150435SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1311ae150435SJeff Kirsher /*
1312ae150435SJeff Kirsher * Polling receive - used by netconsole and other diagnostic tools
1313ae150435SJeff Kirsher * to allow network i/o with interrupts disabled.
1314ae150435SJeff Kirsher */
smc_poll_controller(struct net_device * dev)1315ae150435SJeff Kirsher static void smc_poll_controller(struct net_device *dev)
1316ae150435SJeff Kirsher {
1317ae150435SJeff Kirsher disable_irq(dev->irq);
1318ae150435SJeff Kirsher smc_interrupt(dev->irq, dev);
1319ae150435SJeff Kirsher enable_irq(dev->irq);
1320ae150435SJeff Kirsher }
1321ae150435SJeff Kirsher #endif
1322ae150435SJeff Kirsher
1323ae150435SJeff Kirsher /* Our watchdog timed out. Called by the networking layer */
smc_timeout(struct net_device * dev,unsigned int txqueue)13240290bd29SMichael S. Tsirkin static void smc_timeout(struct net_device *dev, unsigned int txqueue)
1325ae150435SJeff Kirsher {
1326ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1327ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1328ae150435SJeff Kirsher int status, mask, eph_st, meminfo, fifo;
1329ae150435SJeff Kirsher
13306389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
1331ae150435SJeff Kirsher
1332ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1333ae150435SJeff Kirsher status = SMC_GET_INT(lp);
1334ae150435SJeff Kirsher mask = SMC_GET_INT_MASK(lp);
1335ae150435SJeff Kirsher fifo = SMC_GET_FIFO(lp);
1336ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1337ae150435SJeff Kirsher eph_st = SMC_GET_EPH_STATUS(lp);
1338ae150435SJeff Kirsher meminfo = SMC_GET_MIR(lp);
1339ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1340ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
13416389aa45SBen Boeckel PRINTK(dev, "TX timeout (INT 0x%02x INTMASK 0x%02x MEM 0x%04x FIFO 0x%04x EPH_ST 0x%04x)\n",
13426389aa45SBen Boeckel status, mask, meminfo, fifo, eph_st);
1343ae150435SJeff Kirsher
1344ae150435SJeff Kirsher smc_reset(dev);
1345ae150435SJeff Kirsher smc_enable(dev);
1346ae150435SJeff Kirsher
1347ae150435SJeff Kirsher /*
1348ae150435SJeff Kirsher * Reconfiguring the PHY doesn't seem like a bad idea here, but
1349ae150435SJeff Kirsher * smc_phy_configure() calls msleep() which calls schedule_timeout()
1350ae150435SJeff Kirsher * which calls schedule(). Hence we use a work queue.
1351ae150435SJeff Kirsher */
1352ae150435SJeff Kirsher if (lp->phy_type != 0)
1353ae150435SJeff Kirsher schedule_work(&lp->phy_configure);
1354ae150435SJeff Kirsher
1355ae150435SJeff Kirsher /* We can accept TX packets again */
1356860e9538SFlorian Westphal netif_trans_update(dev); /* prevent tx timeout */
1357ae150435SJeff Kirsher netif_wake_queue(dev);
1358ae150435SJeff Kirsher }
1359ae150435SJeff Kirsher
1360ae150435SJeff Kirsher /*
1361ae150435SJeff Kirsher * This routine will, depending on the values passed to it,
1362ae150435SJeff Kirsher * either make it accept multicast packets, go into
1363ae150435SJeff Kirsher * promiscuous mode (for TCPDUMP and cousins) or accept
1364ae150435SJeff Kirsher * a select set of multicast packets
1365ae150435SJeff Kirsher */
smc_set_multicast_list(struct net_device * dev)1366ae150435SJeff Kirsher static void smc_set_multicast_list(struct net_device *dev)
1367ae150435SJeff Kirsher {
1368ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1369ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1370ae150435SJeff Kirsher unsigned char multicast_table[8];
1371ae150435SJeff Kirsher int update_multicast = 0;
1372ae150435SJeff Kirsher
13736389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
1374ae150435SJeff Kirsher
1375ae150435SJeff Kirsher if (dev->flags & IFF_PROMISC) {
13766389aa45SBen Boeckel DBG(2, dev, "RCR_PRMS\n");
1377ae150435SJeff Kirsher lp->rcr_cur_mode |= RCR_PRMS;
1378ae150435SJeff Kirsher }
1379ae150435SJeff Kirsher
1380ae150435SJeff Kirsher /* BUG? I never disable promiscuous mode if multicasting was turned on.
1381ae150435SJeff Kirsher Now, I turn off promiscuous mode, but I don't do anything to multicasting
1382ae150435SJeff Kirsher when promiscuous mode is turned on.
1383ae150435SJeff Kirsher */
1384ae150435SJeff Kirsher
1385ae150435SJeff Kirsher /*
1386ae150435SJeff Kirsher * Here, I am setting this to accept all multicast packets.
1387ae150435SJeff Kirsher * I don't need to zero the multicast table, because the flag is
1388ae150435SJeff Kirsher * checked before the table is
1389ae150435SJeff Kirsher */
1390ae150435SJeff Kirsher else if (dev->flags & IFF_ALLMULTI || netdev_mc_count(dev) > 16) {
13916389aa45SBen Boeckel DBG(2, dev, "RCR_ALMUL\n");
1392ae150435SJeff Kirsher lp->rcr_cur_mode |= RCR_ALMUL;
1393ae150435SJeff Kirsher }
1394ae150435SJeff Kirsher
1395ae150435SJeff Kirsher /*
1396ae150435SJeff Kirsher * This sets the internal hardware table to filter out unwanted
1397ae150435SJeff Kirsher * multicast packets before they take up memory.
1398ae150435SJeff Kirsher *
1399ae150435SJeff Kirsher * The SMC chip uses a hash table where the high 6 bits of the CRC of
1400ae150435SJeff Kirsher * address are the offset into the table. If that bit is 1, then the
1401ae150435SJeff Kirsher * multicast packet is accepted. Otherwise, it's dropped silently.
1402ae150435SJeff Kirsher *
1403ae150435SJeff Kirsher * To use the 6 bits as an offset into the table, the high 3 bits are
1404ae150435SJeff Kirsher * the number of the 8 bit register, while the low 3 bits are the bit
1405ae150435SJeff Kirsher * within that register.
1406ae150435SJeff Kirsher */
1407ae150435SJeff Kirsher else if (!netdev_mc_empty(dev)) {
1408ae150435SJeff Kirsher struct netdev_hw_addr *ha;
1409ae150435SJeff Kirsher
1410ae150435SJeff Kirsher /* table for flipping the order of 3 bits */
1411ae150435SJeff Kirsher static const unsigned char invert3[] = {0, 4, 2, 6, 1, 5, 3, 7};
1412ae150435SJeff Kirsher
1413ae150435SJeff Kirsher /* start with a table of all zeros: reject all */
1414ae150435SJeff Kirsher memset(multicast_table, 0, sizeof(multicast_table));
1415ae150435SJeff Kirsher
1416ae150435SJeff Kirsher netdev_for_each_mc_addr(ha, dev) {
1417ae150435SJeff Kirsher int position;
1418ae150435SJeff Kirsher
1419ae150435SJeff Kirsher /* only use the low order bits */
1420ae150435SJeff Kirsher position = crc32_le(~0, ha->addr, 6) & 0x3f;
1421ae150435SJeff Kirsher
1422ae150435SJeff Kirsher /* do some messy swapping to put the bit in the right spot */
1423ae150435SJeff Kirsher multicast_table[invert3[position&7]] |=
1424ae150435SJeff Kirsher (1<<invert3[(position>>3)&7]);
1425ae150435SJeff Kirsher }
1426ae150435SJeff Kirsher
1427ae150435SJeff Kirsher /* be sure I get rid of flags I might have set */
1428ae150435SJeff Kirsher lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1429ae150435SJeff Kirsher
1430ae150435SJeff Kirsher /* now, the table can be loaded into the chipset */
1431ae150435SJeff Kirsher update_multicast = 1;
1432ae150435SJeff Kirsher } else {
14336389aa45SBen Boeckel DBG(2, dev, "~(RCR_PRMS|RCR_ALMUL)\n");
1434ae150435SJeff Kirsher lp->rcr_cur_mode &= ~(RCR_PRMS | RCR_ALMUL);
1435ae150435SJeff Kirsher
1436ae150435SJeff Kirsher /*
1437ae150435SJeff Kirsher * since I'm disabling all multicast entirely, I need to
1438ae150435SJeff Kirsher * clear the multicast list
1439ae150435SJeff Kirsher */
1440ae150435SJeff Kirsher memset(multicast_table, 0, sizeof(multicast_table));
1441ae150435SJeff Kirsher update_multicast = 1;
1442ae150435SJeff Kirsher }
1443ae150435SJeff Kirsher
1444ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1445ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1446ae150435SJeff Kirsher SMC_SET_RCR(lp, lp->rcr_cur_mode);
1447ae150435SJeff Kirsher if (update_multicast) {
1448ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 3);
1449ae150435SJeff Kirsher SMC_SET_MCAST(lp, multicast_table);
1450ae150435SJeff Kirsher }
1451ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1452ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1453ae150435SJeff Kirsher }
1454ae150435SJeff Kirsher
1455ae150435SJeff Kirsher
1456ae150435SJeff Kirsher /*
1457ae150435SJeff Kirsher * Open and Initialize the board
1458ae150435SJeff Kirsher *
1459ae150435SJeff Kirsher * Set up everything, reset the card, etc..
1460ae150435SJeff Kirsher */
1461ae150435SJeff Kirsher static int
smc_open(struct net_device * dev)1462ae150435SJeff Kirsher smc_open(struct net_device *dev)
1463ae150435SJeff Kirsher {
1464ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1465ae150435SJeff Kirsher
14666389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
1467ae150435SJeff Kirsher
1468ae150435SJeff Kirsher /* Setup the default Register Modes */
1469ae150435SJeff Kirsher lp->tcr_cur_mode = TCR_DEFAULT;
1470ae150435SJeff Kirsher lp->rcr_cur_mode = RCR_DEFAULT;
1471ae150435SJeff Kirsher lp->rpc_cur_mode = RPC_DEFAULT |
1472ae150435SJeff Kirsher lp->cfg.leda << RPC_LSXA_SHFT |
1473ae150435SJeff Kirsher lp->cfg.ledb << RPC_LSXB_SHFT;
1474ae150435SJeff Kirsher
1475ae150435SJeff Kirsher /*
1476ae150435SJeff Kirsher * If we are not using a MII interface, we need to
1477ae150435SJeff Kirsher * monitor our own carrier signal to detect faults.
1478ae150435SJeff Kirsher */
1479ae150435SJeff Kirsher if (lp->phy_type == 0)
1480ae150435SJeff Kirsher lp->tcr_cur_mode |= TCR_MON_CSN;
1481ae150435SJeff Kirsher
1482ae150435SJeff Kirsher /* reset the hardware */
1483ae150435SJeff Kirsher smc_reset(dev);
1484ae150435SJeff Kirsher smc_enable(dev);
1485ae150435SJeff Kirsher
1486ae150435SJeff Kirsher /* Configure the PHY, initialize the link state */
1487ae150435SJeff Kirsher if (lp->phy_type != 0)
1488ae150435SJeff Kirsher smc_phy_configure(&lp->phy_configure);
1489ae150435SJeff Kirsher else {
1490ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1491ae150435SJeff Kirsher smc_10bt_check_media(dev, 1);
1492ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1493ae150435SJeff Kirsher }
1494ae150435SJeff Kirsher
1495ae150435SJeff Kirsher netif_start_queue(dev);
1496ae150435SJeff Kirsher return 0;
1497ae150435SJeff Kirsher }
1498ae150435SJeff Kirsher
1499ae150435SJeff Kirsher /*
1500ae150435SJeff Kirsher * smc_close
1501ae150435SJeff Kirsher *
1502ae150435SJeff Kirsher * this makes the board clean up everything that it can
1503ae150435SJeff Kirsher * and not talk to the outside world. Caused by
1504ae150435SJeff Kirsher * an 'ifconfig ethX down'
1505ae150435SJeff Kirsher */
smc_close(struct net_device * dev)1506ae150435SJeff Kirsher static int smc_close(struct net_device *dev)
1507ae150435SJeff Kirsher {
1508ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1509ae150435SJeff Kirsher
15106389aa45SBen Boeckel DBG(2, dev, "%s\n", __func__);
1511ae150435SJeff Kirsher
1512ae150435SJeff Kirsher netif_stop_queue(dev);
1513ae150435SJeff Kirsher netif_carrier_off(dev);
1514ae150435SJeff Kirsher
1515ae150435SJeff Kirsher /* clear everything */
1516ae150435SJeff Kirsher smc_shutdown(dev);
1517ae150435SJeff Kirsher tasklet_kill(&lp->tx_task);
1518ae150435SJeff Kirsher smc_phy_powerdown(dev);
1519ae150435SJeff Kirsher return 0;
1520ae150435SJeff Kirsher }
1521ae150435SJeff Kirsher
1522ae150435SJeff Kirsher /*
1523ae150435SJeff Kirsher * Ethtool support
1524ae150435SJeff Kirsher */
1525ae150435SJeff Kirsher static int
smc_ethtool_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)15267b022a1bSPhilippe Reynes smc_ethtool_get_link_ksettings(struct net_device *dev,
15277b022a1bSPhilippe Reynes struct ethtool_link_ksettings *cmd)
1528ae150435SJeff Kirsher {
1529ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1530ae150435SJeff Kirsher
1531ae150435SJeff Kirsher if (lp->phy_type != 0) {
1532ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
153382c01a84Syuval.shaia@oracle.com mii_ethtool_get_link_ksettings(&lp->mii, cmd);
1534ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1535ae150435SJeff Kirsher } else {
15367b022a1bSPhilippe Reynes u32 supported = SUPPORTED_10baseT_Half |
1537ae150435SJeff Kirsher SUPPORTED_10baseT_Full |
1538ae150435SJeff Kirsher SUPPORTED_TP | SUPPORTED_AUI;
1539ae150435SJeff Kirsher
1540ae150435SJeff Kirsher if (lp->ctl_rspeed == 10)
15417b022a1bSPhilippe Reynes cmd->base.speed = SPEED_10;
1542ae150435SJeff Kirsher else if (lp->ctl_rspeed == 100)
15437b022a1bSPhilippe Reynes cmd->base.speed = SPEED_100;
1544ae150435SJeff Kirsher
15457b022a1bSPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE;
15467b022a1bSPhilippe Reynes cmd->base.port = 0;
15477b022a1bSPhilippe Reynes cmd->base.duplex = lp->tcr_cur_mode & TCR_SWFDUP ?
15487b022a1bSPhilippe Reynes DUPLEX_FULL : DUPLEX_HALF;
15497b022a1bSPhilippe Reynes
15507b022a1bSPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(
15517b022a1bSPhilippe Reynes cmd->link_modes.supported, supported);
1552ae150435SJeff Kirsher }
1553ae150435SJeff Kirsher
155482c01a84Syuval.shaia@oracle.com return 0;
1555ae150435SJeff Kirsher }
1556ae150435SJeff Kirsher
1557ae150435SJeff Kirsher static int
smc_ethtool_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)15587b022a1bSPhilippe Reynes smc_ethtool_set_link_ksettings(struct net_device *dev,
15597b022a1bSPhilippe Reynes const struct ethtool_link_ksettings *cmd)
1560ae150435SJeff Kirsher {
1561ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1562ae150435SJeff Kirsher int ret;
1563ae150435SJeff Kirsher
1564ae150435SJeff Kirsher if (lp->phy_type != 0) {
1565ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
15667b022a1bSPhilippe Reynes ret = mii_ethtool_set_link_ksettings(&lp->mii, cmd);
1567ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1568ae150435SJeff Kirsher } else {
15697b022a1bSPhilippe Reynes if (cmd->base.autoneg != AUTONEG_DISABLE ||
15707b022a1bSPhilippe Reynes cmd->base.speed != SPEED_10 ||
15717b022a1bSPhilippe Reynes (cmd->base.duplex != DUPLEX_HALF &&
15727b022a1bSPhilippe Reynes cmd->base.duplex != DUPLEX_FULL) ||
15737b022a1bSPhilippe Reynes (cmd->base.port != PORT_TP && cmd->base.port != PORT_AUI))
1574ae150435SJeff Kirsher return -EINVAL;
1575ae150435SJeff Kirsher
15767b022a1bSPhilippe Reynes // lp->port = cmd->base.port;
15777b022a1bSPhilippe Reynes lp->ctl_rfduplx = cmd->base.duplex == DUPLEX_FULL;
1578ae150435SJeff Kirsher
1579ae150435SJeff Kirsher // if (netif_running(dev))
1580ae150435SJeff Kirsher // smc_set_port(dev);
1581ae150435SJeff Kirsher
1582ae150435SJeff Kirsher ret = 0;
1583ae150435SJeff Kirsher }
1584ae150435SJeff Kirsher
1585ae150435SJeff Kirsher return ret;
1586ae150435SJeff Kirsher }
1587ae150435SJeff Kirsher
1588ae150435SJeff Kirsher static void
smc_ethtool_getdrvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1589ae150435SJeff Kirsher smc_ethtool_getdrvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1590ae150435SJeff Kirsher {
1591f029c781SWolfram Sang strscpy(info->driver, CARDNAME, sizeof(info->driver));
1592f029c781SWolfram Sang strscpy(info->version, version, sizeof(info->version));
1593f029c781SWolfram Sang strscpy(info->bus_info, dev_name(dev->dev.parent),
15947826d43fSJiri Pirko sizeof(info->bus_info));
1595ae150435SJeff Kirsher }
1596ae150435SJeff Kirsher
smc_ethtool_nwayreset(struct net_device * dev)1597ae150435SJeff Kirsher static int smc_ethtool_nwayreset(struct net_device *dev)
1598ae150435SJeff Kirsher {
1599ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1600ae150435SJeff Kirsher int ret = -EINVAL;
1601ae150435SJeff Kirsher
1602ae150435SJeff Kirsher if (lp->phy_type != 0) {
1603ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1604ae150435SJeff Kirsher ret = mii_nway_restart(&lp->mii);
1605ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1606ae150435SJeff Kirsher }
1607ae150435SJeff Kirsher
1608ae150435SJeff Kirsher return ret;
1609ae150435SJeff Kirsher }
1610ae150435SJeff Kirsher
smc_ethtool_getmsglevel(struct net_device * dev)1611ae150435SJeff Kirsher static u32 smc_ethtool_getmsglevel(struct net_device *dev)
1612ae150435SJeff Kirsher {
1613ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1614ae150435SJeff Kirsher return lp->msg_enable;
1615ae150435SJeff Kirsher }
1616ae150435SJeff Kirsher
smc_ethtool_setmsglevel(struct net_device * dev,u32 level)1617ae150435SJeff Kirsher static void smc_ethtool_setmsglevel(struct net_device *dev, u32 level)
1618ae150435SJeff Kirsher {
1619ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1620ae150435SJeff Kirsher lp->msg_enable = level;
1621ae150435SJeff Kirsher }
1622ae150435SJeff Kirsher
smc_write_eeprom_word(struct net_device * dev,u16 addr,u16 word)1623ae150435SJeff Kirsher static int smc_write_eeprom_word(struct net_device *dev, u16 addr, u16 word)
1624ae150435SJeff Kirsher {
1625ae150435SJeff Kirsher u16 ctl;
1626ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1627ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1628ae150435SJeff Kirsher
1629ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1630ae150435SJeff Kirsher /* load word into GP register */
1631ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
1632ae150435SJeff Kirsher SMC_SET_GP(lp, word);
1633ae150435SJeff Kirsher /* set the address to put the data in EEPROM */
1634ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1635ae150435SJeff Kirsher SMC_SET_PTR(lp, addr);
1636ae150435SJeff Kirsher /* tell it to write */
1637ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
1638ae150435SJeff Kirsher ctl = SMC_GET_CTL(lp);
1639ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_STORE));
1640ae150435SJeff Kirsher /* wait for it to finish */
1641ae150435SJeff Kirsher do {
1642ae150435SJeff Kirsher udelay(1);
1643ae150435SJeff Kirsher } while (SMC_GET_CTL(lp) & CTL_STORE);
1644ae150435SJeff Kirsher /* clean up */
1645ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl);
1646ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1647ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1648ae150435SJeff Kirsher return 0;
1649ae150435SJeff Kirsher }
1650ae150435SJeff Kirsher
smc_read_eeprom_word(struct net_device * dev,u16 addr,u16 * word)1651ae150435SJeff Kirsher static int smc_read_eeprom_word(struct net_device *dev, u16 addr, u16 *word)
1652ae150435SJeff Kirsher {
1653ae150435SJeff Kirsher u16 ctl;
1654ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1655ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1656ae150435SJeff Kirsher
1657ae150435SJeff Kirsher spin_lock_irq(&lp->lock);
1658ae150435SJeff Kirsher /* set the EEPROM address to get the data from */
1659ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1660ae150435SJeff Kirsher SMC_SET_PTR(lp, addr | PTR_READ);
1661ae150435SJeff Kirsher /* tell it to load */
1662ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
1663ae150435SJeff Kirsher SMC_SET_GP(lp, 0xffff); /* init to known */
1664ae150435SJeff Kirsher ctl = SMC_GET_CTL(lp);
1665ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl | (CTL_EEPROM_SELECT | CTL_RELOAD));
1666ae150435SJeff Kirsher /* wait for it to finish */
1667ae150435SJeff Kirsher do {
1668ae150435SJeff Kirsher udelay(1);
1669ae150435SJeff Kirsher } while (SMC_GET_CTL(lp) & CTL_RELOAD);
1670ae150435SJeff Kirsher /* read word from GP register */
1671ae150435SJeff Kirsher *word = SMC_GET_GP(lp);
1672ae150435SJeff Kirsher /* clean up */
1673ae150435SJeff Kirsher SMC_SET_CTL(lp, ctl);
1674ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1675ae150435SJeff Kirsher spin_unlock_irq(&lp->lock);
1676ae150435SJeff Kirsher return 0;
1677ae150435SJeff Kirsher }
1678ae150435SJeff Kirsher
smc_ethtool_geteeprom_len(struct net_device * dev)1679ae150435SJeff Kirsher static int smc_ethtool_geteeprom_len(struct net_device *dev)
1680ae150435SJeff Kirsher {
1681ae150435SJeff Kirsher return 0x23 * 2;
1682ae150435SJeff Kirsher }
1683ae150435SJeff Kirsher
smc_ethtool_geteeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)1684ae150435SJeff Kirsher static int smc_ethtool_geteeprom(struct net_device *dev,
1685ae150435SJeff Kirsher struct ethtool_eeprom *eeprom, u8 *data)
1686ae150435SJeff Kirsher {
1687ae150435SJeff Kirsher int i;
1688ae150435SJeff Kirsher int imax;
1689ae150435SJeff Kirsher
16906389aa45SBen Boeckel DBG(1, dev, "Reading %d bytes at %d(0x%x)\n",
1691ae150435SJeff Kirsher eeprom->len, eeprom->offset, eeprom->offset);
1692ae150435SJeff Kirsher imax = smc_ethtool_geteeprom_len(dev);
1693ae150435SJeff Kirsher for (i = 0; i < eeprom->len; i += 2) {
1694ae150435SJeff Kirsher int ret;
1695ae150435SJeff Kirsher u16 wbuf;
1696ae150435SJeff Kirsher int offset = i + eeprom->offset;
1697ae150435SJeff Kirsher if (offset > imax)
1698ae150435SJeff Kirsher break;
1699ae150435SJeff Kirsher ret = smc_read_eeprom_word(dev, offset >> 1, &wbuf);
1700ae150435SJeff Kirsher if (ret != 0)
1701ae150435SJeff Kirsher return ret;
17026389aa45SBen Boeckel DBG(2, dev, "Read 0x%x from 0x%x\n", wbuf, offset >> 1);
1703ae150435SJeff Kirsher data[i] = (wbuf >> 8) & 0xff;
1704ae150435SJeff Kirsher data[i+1] = wbuf & 0xff;
1705ae150435SJeff Kirsher }
1706ae150435SJeff Kirsher return 0;
1707ae150435SJeff Kirsher }
1708ae150435SJeff Kirsher
smc_ethtool_seteeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)1709ae150435SJeff Kirsher static int smc_ethtool_seteeprom(struct net_device *dev,
1710ae150435SJeff Kirsher struct ethtool_eeprom *eeprom, u8 *data)
1711ae150435SJeff Kirsher {
1712ae150435SJeff Kirsher int i;
1713ae150435SJeff Kirsher int imax;
1714ae150435SJeff Kirsher
17156389aa45SBen Boeckel DBG(1, dev, "Writing %d bytes to %d(0x%x)\n",
1716ae150435SJeff Kirsher eeprom->len, eeprom->offset, eeprom->offset);
1717ae150435SJeff Kirsher imax = smc_ethtool_geteeprom_len(dev);
1718ae150435SJeff Kirsher for (i = 0; i < eeprom->len; i += 2) {
1719ae150435SJeff Kirsher int ret;
1720ae150435SJeff Kirsher u16 wbuf;
1721ae150435SJeff Kirsher int offset = i + eeprom->offset;
1722ae150435SJeff Kirsher if (offset > imax)
1723ae150435SJeff Kirsher break;
1724ae150435SJeff Kirsher wbuf = (data[i] << 8) | data[i + 1];
17256389aa45SBen Boeckel DBG(2, dev, "Writing 0x%x to 0x%x\n", wbuf, offset >> 1);
1726ae150435SJeff Kirsher ret = smc_write_eeprom_word(dev, offset >> 1, wbuf);
1727ae150435SJeff Kirsher if (ret != 0)
1728ae150435SJeff Kirsher return ret;
1729ae150435SJeff Kirsher }
1730ae150435SJeff Kirsher return 0;
1731ae150435SJeff Kirsher }
1732ae150435SJeff Kirsher
1733ae150435SJeff Kirsher
1734ae150435SJeff Kirsher static const struct ethtool_ops smc_ethtool_ops = {
1735ae150435SJeff Kirsher .get_drvinfo = smc_ethtool_getdrvinfo,
1736ae150435SJeff Kirsher
1737ae150435SJeff Kirsher .get_msglevel = smc_ethtool_getmsglevel,
1738ae150435SJeff Kirsher .set_msglevel = smc_ethtool_setmsglevel,
1739ae150435SJeff Kirsher .nway_reset = smc_ethtool_nwayreset,
1740ae150435SJeff Kirsher .get_link = ethtool_op_get_link,
1741ae150435SJeff Kirsher .get_eeprom_len = smc_ethtool_geteeprom_len,
1742ae150435SJeff Kirsher .get_eeprom = smc_ethtool_geteeprom,
1743ae150435SJeff Kirsher .set_eeprom = smc_ethtool_seteeprom,
17447b022a1bSPhilippe Reynes .get_link_ksettings = smc_ethtool_get_link_ksettings,
17457b022a1bSPhilippe Reynes .set_link_ksettings = smc_ethtool_set_link_ksettings,
1746ae150435SJeff Kirsher };
1747ae150435SJeff Kirsher
1748ae150435SJeff Kirsher static const struct net_device_ops smc_netdev_ops = {
1749ae150435SJeff Kirsher .ndo_open = smc_open,
1750ae150435SJeff Kirsher .ndo_stop = smc_close,
1751ae150435SJeff Kirsher .ndo_start_xmit = smc_hard_start_xmit,
1752ae150435SJeff Kirsher .ndo_tx_timeout = smc_timeout,
1753afc4b13dSJiri Pirko .ndo_set_rx_mode = smc_set_multicast_list,
1754ae150435SJeff Kirsher .ndo_validate_addr = eth_validate_addr,
1755ae150435SJeff Kirsher .ndo_set_mac_address = eth_mac_addr,
1756ae150435SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
1757ae150435SJeff Kirsher .ndo_poll_controller = smc_poll_controller,
1758ae150435SJeff Kirsher #endif
1759ae150435SJeff Kirsher };
1760ae150435SJeff Kirsher
1761ae150435SJeff Kirsher /*
1762ae150435SJeff Kirsher * smc_findirq
1763ae150435SJeff Kirsher *
1764ae150435SJeff Kirsher * This routine has a simple purpose -- make the SMC chip generate an
1765ae150435SJeff Kirsher * interrupt, so an auto-detect routine can detect it, and find the IRQ,
1766ae150435SJeff Kirsher */
1767ae150435SJeff Kirsher /*
1768ae150435SJeff Kirsher * does this still work?
1769ae150435SJeff Kirsher *
1770ae150435SJeff Kirsher * I just deleted auto_irq.c, since it was never built...
1771ae150435SJeff Kirsher * --jgarzik
1772ae150435SJeff Kirsher */
smc_findirq(struct smc_local * lp)17731e48fea4SBill Pemberton static int smc_findirq(struct smc_local *lp)
1774ae150435SJeff Kirsher {
1775ae150435SJeff Kirsher void __iomem *ioaddr = lp->base;
1776ae150435SJeff Kirsher int timeout = 20;
1777ae150435SJeff Kirsher unsigned long cookie;
1778ae150435SJeff Kirsher
1779702b3468SZi Shen Lim DBG(2, lp->dev, "%s: %s\n", CARDNAME, __func__);
1780ae150435SJeff Kirsher
1781ae150435SJeff Kirsher cookie = probe_irq_on();
1782ae150435SJeff Kirsher
1783ae150435SJeff Kirsher /*
1784ae150435SJeff Kirsher * What I try to do here is trigger an ALLOC_INT. This is done
1785ae150435SJeff Kirsher * by allocating a small chunk of memory, which will give an interrupt
1786ae150435SJeff Kirsher * when done.
1787ae150435SJeff Kirsher */
1788ae150435SJeff Kirsher /* enable ALLOCation interrupts ONLY */
1789ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 2);
1790ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, IM_ALLOC_INT);
1791ae150435SJeff Kirsher
1792ae150435SJeff Kirsher /*
1793ae150435SJeff Kirsher * Allocate 512 bytes of memory. Note that the chip was just
1794ae150435SJeff Kirsher * reset so all the memory is available
1795ae150435SJeff Kirsher */
1796ae150435SJeff Kirsher SMC_SET_MMU_CMD(lp, MC_ALLOC | 1);
1797ae150435SJeff Kirsher
1798ae150435SJeff Kirsher /*
1799ae150435SJeff Kirsher * Wait until positive that the interrupt has been generated
1800ae150435SJeff Kirsher */
1801ae150435SJeff Kirsher do {
1802ae150435SJeff Kirsher int int_status;
1803ae150435SJeff Kirsher udelay(10);
1804ae150435SJeff Kirsher int_status = SMC_GET_INT(lp);
1805ae150435SJeff Kirsher if (int_status & IM_ALLOC_INT)
1806ae150435SJeff Kirsher break; /* got the interrupt */
1807ae150435SJeff Kirsher } while (--timeout);
1808ae150435SJeff Kirsher
1809ae150435SJeff Kirsher /*
1810ae150435SJeff Kirsher * there is really nothing that I can do here if timeout fails,
1811ae150435SJeff Kirsher * as autoirq_report will return a 0 anyway, which is what I
1812ae150435SJeff Kirsher * want in this case. Plus, the clean up is needed in both
1813ae150435SJeff Kirsher * cases.
1814ae150435SJeff Kirsher */
1815ae150435SJeff Kirsher
1816ae150435SJeff Kirsher /* and disable all interrupts again */
1817ae150435SJeff Kirsher SMC_SET_INT_MASK(lp, 0);
1818ae150435SJeff Kirsher
1819ae150435SJeff Kirsher /* and return what I found */
1820ae150435SJeff Kirsher return probe_irq_off(cookie);
1821ae150435SJeff Kirsher }
1822ae150435SJeff Kirsher
1823ae150435SJeff Kirsher /*
1824ae150435SJeff Kirsher * Function: smc_probe(unsigned long ioaddr)
1825ae150435SJeff Kirsher *
1826ae150435SJeff Kirsher * Purpose:
1827ae150435SJeff Kirsher * Tests to see if a given ioaddr points to an SMC91x chip.
1828ae150435SJeff Kirsher * Returns a 0 on success
1829ae150435SJeff Kirsher *
1830ae150435SJeff Kirsher * Algorithm:
1831ae150435SJeff Kirsher * (1) see if the high byte of BANK_SELECT is 0x33
1832ae150435SJeff Kirsher * (2) compare the ioaddr with the base register's address
1833ae150435SJeff Kirsher * (3) see if I recognize the chip ID in the appropriate register
1834ae150435SJeff Kirsher *
1835ae150435SJeff Kirsher * Here I do typical initialization tasks.
1836ae150435SJeff Kirsher *
1837ae150435SJeff Kirsher * o Initialize the structure if needed
1838ae150435SJeff Kirsher * o print out my vanity message if not done so already
1839ae150435SJeff Kirsher * o print out what type of hardware is detected
1840ae150435SJeff Kirsher * o print out the ethernet address
1841ae150435SJeff Kirsher * o find the IRQ
1842ae150435SJeff Kirsher * o set up my private data
1843ae150435SJeff Kirsher * o configure the dev structure with my subroutines
1844ae150435SJeff Kirsher * o actually GRAB the irq.
1845ae150435SJeff Kirsher * o GRAB the region
1846ae150435SJeff Kirsher */
smc_probe(struct net_device * dev,void __iomem * ioaddr,unsigned long irq_flags)18471e48fea4SBill Pemberton static int smc_probe(struct net_device *dev, void __iomem *ioaddr,
1848ae150435SJeff Kirsher unsigned long irq_flags)
1849ae150435SJeff Kirsher {
1850ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(dev);
1851ae150435SJeff Kirsher int retval;
1852ae150435SJeff Kirsher unsigned int val, revision_register;
1853ae150435SJeff Kirsher const char *version_string;
185402bfb6beSJakub Kicinski u8 addr[ETH_ALEN];
1855ae150435SJeff Kirsher
18566389aa45SBen Boeckel DBG(2, dev, "%s: %s\n", CARDNAME, __func__);
1857ae150435SJeff Kirsher
1858ae150435SJeff Kirsher /* First, see if the high byte is 0x33 */
1859ae150435SJeff Kirsher val = SMC_CURRENT_BANK(lp);
18606389aa45SBen Boeckel DBG(2, dev, "%s: bank signature probe returned 0x%04x\n",
18616389aa45SBen Boeckel CARDNAME, val);
1862ae150435SJeff Kirsher if ((val & 0xFF00) != 0x3300) {
1863ae150435SJeff Kirsher if ((val & 0xFF) == 0x33) {
18646389aa45SBen Boeckel netdev_warn(dev,
18656389aa45SBen Boeckel "%s: Detected possible byte-swapped interface at IOADDR %p\n",
18666389aa45SBen Boeckel CARDNAME, ioaddr);
1867ae150435SJeff Kirsher }
1868ae150435SJeff Kirsher retval = -ENODEV;
1869ae150435SJeff Kirsher goto err_out;
1870ae150435SJeff Kirsher }
1871ae150435SJeff Kirsher
1872ae150435SJeff Kirsher /*
1873ae150435SJeff Kirsher * The above MIGHT indicate a device, but I need to write to
1874ae150435SJeff Kirsher * further test this.
1875ae150435SJeff Kirsher */
1876ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 0);
1877ae150435SJeff Kirsher val = SMC_CURRENT_BANK(lp);
1878ae150435SJeff Kirsher if ((val & 0xFF00) != 0x3300) {
1879ae150435SJeff Kirsher retval = -ENODEV;
1880ae150435SJeff Kirsher goto err_out;
1881ae150435SJeff Kirsher }
1882ae150435SJeff Kirsher
1883ae150435SJeff Kirsher /*
1884ae150435SJeff Kirsher * well, we've already written once, so hopefully another
1885ae150435SJeff Kirsher * time won't hurt. This time, I need to switch the bank
1886ae150435SJeff Kirsher * register to bank 1, so I can access the base address
1887ae150435SJeff Kirsher * register
1888ae150435SJeff Kirsher */
1889ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
1890ae150435SJeff Kirsher val = SMC_GET_BASE(lp);
1891ae150435SJeff Kirsher val = ((val & 0x1F00) >> 3) << SMC_IO_SHIFT;
18926bc21eedSPankaj Dubey if (((unsigned long)ioaddr & (0x3e0 << SMC_IO_SHIFT)) != val) {
18936389aa45SBen Boeckel netdev_warn(dev, "%s: IOADDR %p doesn't match configuration (%x).\n",
1894ae150435SJeff Kirsher CARDNAME, ioaddr, val);
1895ae150435SJeff Kirsher }
1896ae150435SJeff Kirsher
1897ae150435SJeff Kirsher /*
1898ae150435SJeff Kirsher * check if the revision register is something that I
1899ae150435SJeff Kirsher * recognize. These might need to be added to later,
1900ae150435SJeff Kirsher * as future revisions could be added.
1901ae150435SJeff Kirsher */
1902ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 3);
1903ae150435SJeff Kirsher revision_register = SMC_GET_REV(lp);
19046389aa45SBen Boeckel DBG(2, dev, "%s: revision = 0x%04x\n", CARDNAME, revision_register);
1905ae150435SJeff Kirsher version_string = chip_ids[ (revision_register >> 4) & 0xF];
1906ae150435SJeff Kirsher if (!version_string || (revision_register & 0xff00) != 0x3300) {
1907ae150435SJeff Kirsher /* I don't recognize this chip, so... */
19086389aa45SBen Boeckel netdev_warn(dev, "%s: IO %p: Unrecognized revision register 0x%04x, Contact author.\n",
19096389aa45SBen Boeckel CARDNAME, ioaddr, revision_register);
1910ae150435SJeff Kirsher
1911ae150435SJeff Kirsher retval = -ENODEV;
1912ae150435SJeff Kirsher goto err_out;
1913ae150435SJeff Kirsher }
1914ae150435SJeff Kirsher
1915ae150435SJeff Kirsher /* At this point I'll assume that the chip is an SMC91x. */
19166389aa45SBen Boeckel pr_info_once("%s\n", version);
1917ae150435SJeff Kirsher
1918ae150435SJeff Kirsher /* fill in some of the fields */
1919ae150435SJeff Kirsher dev->base_addr = (unsigned long)ioaddr;
1920ae150435SJeff Kirsher lp->base = ioaddr;
1921ae150435SJeff Kirsher lp->version = revision_register & 0xff;
1922ae150435SJeff Kirsher spin_lock_init(&lp->lock);
1923ae150435SJeff Kirsher
1924ae150435SJeff Kirsher /* Get the MAC address */
1925ae150435SJeff Kirsher SMC_SELECT_BANK(lp, 1);
192602bfb6beSJakub Kicinski SMC_GET_MAC_ADDR(lp, addr);
192702bfb6beSJakub Kicinski eth_hw_addr_set(dev, addr);
1928ae150435SJeff Kirsher
1929ae150435SJeff Kirsher /* now, reset the chip, and put it into a known state */
1930ae150435SJeff Kirsher smc_reset(dev);
1931ae150435SJeff Kirsher
1932ae150435SJeff Kirsher /*
1933ae150435SJeff Kirsher * If dev->irq is 0, then the device has to be banged on to see
1934ae150435SJeff Kirsher * what the IRQ is.
1935ae150435SJeff Kirsher *
1936ae150435SJeff Kirsher * This banging doesn't always detect the IRQ, for unknown reasons.
1937ae150435SJeff Kirsher * a workaround is to reset the chip and try again.
1938ae150435SJeff Kirsher *
1939ae150435SJeff Kirsher * Interestingly, the DOS packet driver *SETS* the IRQ on the card to
1940ae150435SJeff Kirsher * be what is requested on the command line. I don't do that, mostly
1941ae150435SJeff Kirsher * because the card that I have uses a non-standard method of accessing
1942ae150435SJeff Kirsher * the IRQs, and because this _should_ work in most configurations.
1943ae150435SJeff Kirsher *
1944ae150435SJeff Kirsher * Specifying an IRQ is done with the assumption that the user knows
1945ae150435SJeff Kirsher * what (s)he is doing. No checking is done!!!!
1946ae150435SJeff Kirsher */
1947ae150435SJeff Kirsher if (dev->irq < 1) {
1948ae150435SJeff Kirsher int trials;
1949ae150435SJeff Kirsher
1950ae150435SJeff Kirsher trials = 3;
1951ae150435SJeff Kirsher while (trials--) {
1952ae150435SJeff Kirsher dev->irq = smc_findirq(lp);
1953ae150435SJeff Kirsher if (dev->irq)
1954ae150435SJeff Kirsher break;
1955ae150435SJeff Kirsher /* kick the card and try again */
1956ae150435SJeff Kirsher smc_reset(dev);
1957ae150435SJeff Kirsher }
1958ae150435SJeff Kirsher }
1959ae150435SJeff Kirsher if (dev->irq == 0) {
19606389aa45SBen Boeckel netdev_warn(dev, "Couldn't autodetect your IRQ. Use irq=xx.\n");
1961ae150435SJeff Kirsher retval = -ENODEV;
1962ae150435SJeff Kirsher goto err_out;
1963ae150435SJeff Kirsher }
1964ae150435SJeff Kirsher dev->irq = irq_canonicalize(dev->irq);
1965ae150435SJeff Kirsher
1966ae150435SJeff Kirsher dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1967ae150435SJeff Kirsher dev->netdev_ops = &smc_netdev_ops;
1968ae150435SJeff Kirsher dev->ethtool_ops = &smc_ethtool_ops;
1969ae150435SJeff Kirsher
19707961fb10SAllen Pais tasklet_setup(&lp->tx_task, smc_hardware_send_pkt);
1971ae150435SJeff Kirsher INIT_WORK(&lp->phy_configure, smc_phy_configure);
1972ae150435SJeff Kirsher lp->dev = dev;
1973ae150435SJeff Kirsher lp->mii.phy_id_mask = 0x1f;
1974ae150435SJeff Kirsher lp->mii.reg_num_mask = 0x1f;
1975ae150435SJeff Kirsher lp->mii.force_media = 0;
1976ae150435SJeff Kirsher lp->mii.full_duplex = 0;
1977ae150435SJeff Kirsher lp->mii.dev = dev;
1978ae150435SJeff Kirsher lp->mii.mdio_read = smc_phy_read;
1979ae150435SJeff Kirsher lp->mii.mdio_write = smc_phy_write;
1980ae150435SJeff Kirsher
1981ae150435SJeff Kirsher /*
1982ae150435SJeff Kirsher * Locate the phy, if any.
1983ae150435SJeff Kirsher */
1984ae150435SJeff Kirsher if (lp->version >= (CHIP_91100 << 4))
1985ae150435SJeff Kirsher smc_phy_detect(dev);
1986ae150435SJeff Kirsher
1987ae150435SJeff Kirsher /* then shut everything down to save power */
1988ae150435SJeff Kirsher smc_shutdown(dev);
1989ae150435SJeff Kirsher smc_phy_powerdown(dev);
1990ae150435SJeff Kirsher
1991ae150435SJeff Kirsher /* Set default parameters */
1992ae150435SJeff Kirsher lp->msg_enable = NETIF_MSG_LINK;
1993ae150435SJeff Kirsher lp->ctl_rfduplx = 0;
1994ae150435SJeff Kirsher lp->ctl_rspeed = 10;
1995ae150435SJeff Kirsher
1996ae150435SJeff Kirsher if (lp->version >= (CHIP_91100 << 4)) {
1997ae150435SJeff Kirsher lp->ctl_rfduplx = 1;
1998ae150435SJeff Kirsher lp->ctl_rspeed = 100;
1999ae150435SJeff Kirsher }
2000ae150435SJeff Kirsher
2001ae150435SJeff Kirsher /* Grab the IRQ */
2002ae150435SJeff Kirsher retval = request_irq(dev->irq, smc_interrupt, irq_flags, dev->name, dev);
2003ae150435SJeff Kirsher if (retval)
2004ae150435SJeff Kirsher goto err_out;
2005ae150435SJeff Kirsher
2006ae150435SJeff Kirsher #ifdef CONFIG_ARCH_PXA
2007ae150435SJeff Kirsher # ifdef SMC_USE_PXA_DMA
2008ae150435SJeff Kirsher lp->cfg.flags |= SMC91X_USE_DMA;
2009ae150435SJeff Kirsher # endif
2010ae150435SJeff Kirsher if (lp->cfg.flags & SMC91X_USE_DMA) {
2011d24c8f24SRobert Jarzmik dma_cap_mask_t mask;
2012d24c8f24SRobert Jarzmik
2013d24c8f24SRobert Jarzmik dma_cap_zero(mask);
2014d24c8f24SRobert Jarzmik dma_cap_set(DMA_SLAVE, mask);
2015cbc654c8SRobert Jarzmik lp->dma_chan = dma_request_channel(mask, NULL, NULL);
2016ae150435SJeff Kirsher }
2017ae150435SJeff Kirsher #endif
2018ae150435SJeff Kirsher
2019ae150435SJeff Kirsher retval = register_netdev(dev);
2020ae150435SJeff Kirsher if (retval == 0) {
2021ae150435SJeff Kirsher /* now, print out the card info, in a short format.. */
20226389aa45SBen Boeckel netdev_info(dev, "%s (rev %d) at %p IRQ %d",
20236389aa45SBen Boeckel version_string, revision_register & 0x0f,
2024ae150435SJeff Kirsher lp->base, dev->irq);
2025ae150435SJeff Kirsher
2026d24c8f24SRobert Jarzmik if (lp->dma_chan)
2027d24c8f24SRobert Jarzmik pr_cont(" DMA %p", lp->dma_chan);
2028ae150435SJeff Kirsher
20296389aa45SBen Boeckel pr_cont("%s%s\n",
2030ae150435SJeff Kirsher lp->cfg.flags & SMC91X_NOWAIT ? " [nowait]" : "",
2031ae150435SJeff Kirsher THROTTLE_TX_PKTS ? " [throttle_tx]" : "");
2032ae150435SJeff Kirsher
2033ae150435SJeff Kirsher if (!is_valid_ether_addr(dev->dev_addr)) {
20346389aa45SBen Boeckel netdev_warn(dev, "Invalid ethernet MAC address. Please set using ifconfig\n");
2035ae150435SJeff Kirsher } else {
2036ae150435SJeff Kirsher /* Print the Ethernet address */
20376389aa45SBen Boeckel netdev_info(dev, "Ethernet addr: %pM\n",
20386389aa45SBen Boeckel dev->dev_addr);
2039ae150435SJeff Kirsher }
2040ae150435SJeff Kirsher
2041ae150435SJeff Kirsher if (lp->phy_type == 0) {
20426389aa45SBen Boeckel PRINTK(dev, "No PHY found\n");
2043ae150435SJeff Kirsher } else if ((lp->phy_type & 0xfffffff0) == 0x0016f840) {
20446389aa45SBen Boeckel PRINTK(dev, "PHY LAN83C183 (LAN91C111 Internal)\n");
2045ae150435SJeff Kirsher } else if ((lp->phy_type & 0xfffffff0) == 0x02821c50) {
20466389aa45SBen Boeckel PRINTK(dev, "PHY LAN83C180\n");
2047ae150435SJeff Kirsher }
2048ae150435SJeff Kirsher }
2049ae150435SJeff Kirsher
2050ae150435SJeff Kirsher err_out:
2051ae150435SJeff Kirsher #ifdef CONFIG_ARCH_PXA
2052d24c8f24SRobert Jarzmik if (retval && lp->dma_chan)
2053d24c8f24SRobert Jarzmik dma_release_channel(lp->dma_chan);
2054ae150435SJeff Kirsher #endif
2055ae150435SJeff Kirsher return retval;
2056ae150435SJeff Kirsher }
2057ae150435SJeff Kirsher
smc_enable_device(struct platform_device * pdev)2058ae150435SJeff Kirsher static int smc_enable_device(struct platform_device *pdev)
2059ae150435SJeff Kirsher {
2060ae150435SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev);
2061ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2062ae150435SJeff Kirsher unsigned long flags;
2063ae150435SJeff Kirsher unsigned char ecor, ecsr;
2064ae150435SJeff Kirsher void __iomem *addr;
2065ae150435SJeff Kirsher struct resource * res;
2066ae150435SJeff Kirsher
2067ae150435SJeff Kirsher res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2068ae150435SJeff Kirsher if (!res)
2069ae150435SJeff Kirsher return 0;
2070ae150435SJeff Kirsher
2071ae150435SJeff Kirsher /*
2072ae150435SJeff Kirsher * Map the attribute space. This is overkill, but clean.
2073ae150435SJeff Kirsher */
2074ae150435SJeff Kirsher addr = ioremap(res->start, ATTRIB_SIZE);
2075ae150435SJeff Kirsher if (!addr)
2076ae150435SJeff Kirsher return -ENOMEM;
2077ae150435SJeff Kirsher
2078ae150435SJeff Kirsher /*
2079ae150435SJeff Kirsher * Reset the device. We must disable IRQs around this
2080ae150435SJeff Kirsher * since a reset causes the IRQ line become active.
2081ae150435SJeff Kirsher */
2082ae150435SJeff Kirsher local_irq_save(flags);
2083ae150435SJeff Kirsher ecor = readb(addr + (ECOR << SMC_IO_SHIFT)) & ~ECOR_RESET;
2084ae150435SJeff Kirsher writeb(ecor | ECOR_RESET, addr + (ECOR << SMC_IO_SHIFT));
2085ae150435SJeff Kirsher readb(addr + (ECOR << SMC_IO_SHIFT));
2086ae150435SJeff Kirsher
2087ae150435SJeff Kirsher /*
2088ae150435SJeff Kirsher * Wait 100us for the chip to reset.
2089ae150435SJeff Kirsher */
2090ae150435SJeff Kirsher udelay(100);
2091ae150435SJeff Kirsher
2092ae150435SJeff Kirsher /*
2093ae150435SJeff Kirsher * The device will ignore all writes to the enable bit while
2094ae150435SJeff Kirsher * reset is asserted, even if the reset bit is cleared in the
2095ae150435SJeff Kirsher * same write. Must clear reset first, then enable the device.
2096ae150435SJeff Kirsher */
2097ae150435SJeff Kirsher writeb(ecor, addr + (ECOR << SMC_IO_SHIFT));
2098ae150435SJeff Kirsher writeb(ecor | ECOR_ENABLE, addr + (ECOR << SMC_IO_SHIFT));
2099ae150435SJeff Kirsher
2100ae150435SJeff Kirsher /*
2101ae150435SJeff Kirsher * Set the appropriate byte/word mode.
2102ae150435SJeff Kirsher */
2103ae150435SJeff Kirsher ecsr = readb(addr + (ECSR << SMC_IO_SHIFT)) & ~ECSR_IOIS8;
2104ae150435SJeff Kirsher if (!SMC_16BIT(lp))
2105ae150435SJeff Kirsher ecsr |= ECSR_IOIS8;
2106ae150435SJeff Kirsher writeb(ecsr, addr + (ECSR << SMC_IO_SHIFT));
2107ae150435SJeff Kirsher local_irq_restore(flags);
2108ae150435SJeff Kirsher
2109ae150435SJeff Kirsher iounmap(addr);
2110ae150435SJeff Kirsher
2111ae150435SJeff Kirsher /*
2112ae150435SJeff Kirsher * Wait for the chip to wake up. We could poll the control
2113ae150435SJeff Kirsher * register in the main register space, but that isn't mapped
2114ae150435SJeff Kirsher * yet. We know this is going to take 750us.
2115ae150435SJeff Kirsher */
2116ae150435SJeff Kirsher msleep(1);
2117ae150435SJeff Kirsher
2118ae150435SJeff Kirsher return 0;
2119ae150435SJeff Kirsher }
2120ae150435SJeff Kirsher
smc_request_attrib(struct platform_device * pdev,struct net_device * ndev)2121ae150435SJeff Kirsher static int smc_request_attrib(struct platform_device *pdev,
2122ae150435SJeff Kirsher struct net_device *ndev)
2123ae150435SJeff Kirsher {
2124ae150435SJeff Kirsher struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2125ae150435SJeff Kirsher struct smc_local *lp __maybe_unused = netdev_priv(ndev);
2126ae150435SJeff Kirsher
2127ae150435SJeff Kirsher if (!res)
2128ae150435SJeff Kirsher return 0;
2129ae150435SJeff Kirsher
2130ae150435SJeff Kirsher if (!request_mem_region(res->start, ATTRIB_SIZE, CARDNAME))
2131ae150435SJeff Kirsher return -EBUSY;
2132ae150435SJeff Kirsher
2133ae150435SJeff Kirsher return 0;
2134ae150435SJeff Kirsher }
2135ae150435SJeff Kirsher
smc_release_attrib(struct platform_device * pdev,struct net_device * ndev)2136ae150435SJeff Kirsher static void smc_release_attrib(struct platform_device *pdev,
2137ae150435SJeff Kirsher struct net_device *ndev)
2138ae150435SJeff Kirsher {
2139ae150435SJeff Kirsher struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-attrib");
2140ae150435SJeff Kirsher struct smc_local *lp __maybe_unused = netdev_priv(ndev);
2141ae150435SJeff Kirsher
2142ae150435SJeff Kirsher if (res)
2143ae150435SJeff Kirsher release_mem_region(res->start, ATTRIB_SIZE);
2144ae150435SJeff Kirsher }
2145ae150435SJeff Kirsher
smc_request_datacs(struct platform_device * pdev,struct net_device * ndev)2146ae150435SJeff Kirsher static inline void smc_request_datacs(struct platform_device *pdev, struct net_device *ndev)
2147ae150435SJeff Kirsher {
2148ae150435SJeff Kirsher if (SMC_CAN_USE_DATACS) {
2149ae150435SJeff Kirsher struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2150ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2151ae150435SJeff Kirsher
2152ae150435SJeff Kirsher if (!res)
2153ae150435SJeff Kirsher return;
2154ae150435SJeff Kirsher
2155ae150435SJeff Kirsher if(!request_mem_region(res->start, SMC_DATA_EXTENT, CARDNAME)) {
21566389aa45SBen Boeckel netdev_info(ndev, "%s: failed to request datacs memory region.\n",
21576389aa45SBen Boeckel CARDNAME);
2158ae150435SJeff Kirsher return;
2159ae150435SJeff Kirsher }
2160ae150435SJeff Kirsher
2161ae150435SJeff Kirsher lp->datacs = ioremap(res->start, SMC_DATA_EXTENT);
2162ae150435SJeff Kirsher }
2163ae150435SJeff Kirsher }
2164ae150435SJeff Kirsher
smc_release_datacs(struct platform_device * pdev,struct net_device * ndev)2165ae150435SJeff Kirsher static void smc_release_datacs(struct platform_device *pdev, struct net_device *ndev)
2166ae150435SJeff Kirsher {
2167ae150435SJeff Kirsher if (SMC_CAN_USE_DATACS) {
2168ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2169ae150435SJeff Kirsher struct resource * res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-data32");
2170ae150435SJeff Kirsher
2171ae150435SJeff Kirsher if (lp->datacs)
2172ae150435SJeff Kirsher iounmap(lp->datacs);
2173ae150435SJeff Kirsher
2174ae150435SJeff Kirsher lp->datacs = NULL;
2175ae150435SJeff Kirsher
2176ae150435SJeff Kirsher if (res)
2177ae150435SJeff Kirsher release_mem_region(res->start, SMC_DATA_EXTENT);
2178ae150435SJeff Kirsher }
2179ae150435SJeff Kirsher }
2180ae150435SJeff Kirsher
21818876d94bSJeremy Linton static const struct acpi_device_id smc91x_acpi_match[] = {
21828876d94bSJeremy Linton { "LNRO0003", 0 },
21838876d94bSJeremy Linton { }
21848876d94bSJeremy Linton };
21858876d94bSJeremy Linton MODULE_DEVICE_TABLE(acpi, smc91x_acpi_match);
21868876d94bSJeremy Linton
21873f823c15STony Lindgren #if IS_BUILTIN(CONFIG_OF)
21883f823c15STony Lindgren static const struct of_device_id smc91x_match[] = {
21893f823c15STony Lindgren { .compatible = "smsc,lan91c94", },
21903f823c15STony Lindgren { .compatible = "smsc,lan91c111", },
21913f823c15STony Lindgren {},
21923f823c15STony Lindgren };
21933f823c15STony Lindgren MODULE_DEVICE_TABLE(of, smc91x_match);
21947d2911c4STony Lindgren
21957d2911c4STony Lindgren /**
21967d2a9244SLee Jones * try_toggle_control_gpio - configure a gpio if it exists
2197606ddf1fSAndrew Lunn * @dev: net device
2198606ddf1fSAndrew Lunn * @desc: where to store the GPIO descriptor, if it exists
2199606ddf1fSAndrew Lunn * @name: name of the GPIO in DT
2200606ddf1fSAndrew Lunn * @index: index of the GPIO in DT
2201606ddf1fSAndrew Lunn * @value: set the GPIO to this value
2202606ddf1fSAndrew Lunn * @nsdelay: delay before setting the GPIO
22037d2911c4STony Lindgren */
try_toggle_control_gpio(struct device * dev,struct gpio_desc ** desc,const char * name,int index,int value,unsigned int nsdelay)22047d2911c4STony Lindgren static int try_toggle_control_gpio(struct device *dev,
22057d2911c4STony Lindgren struct gpio_desc **desc,
22067d2911c4STony Lindgren const char *name, int index,
22077d2911c4STony Lindgren int value, unsigned int nsdelay)
22087d2911c4STony Lindgren {
2209214037a1SColin Ian King struct gpio_desc *gpio;
2210cb6e0b36SUwe Kleine-König enum gpiod_flags flags = value ? GPIOD_OUT_LOW : GPIOD_OUT_HIGH;
22117d2911c4STony Lindgren
2212cb6e0b36SUwe Kleine-König gpio = devm_gpiod_get_index_optional(dev, name, index, flags);
2213cb6e0b36SUwe Kleine-König if (IS_ERR(gpio))
22147d2911c4STony Lindgren return PTR_ERR(gpio);
2215cb6e0b36SUwe Kleine-König
2216cb6e0b36SUwe Kleine-König if (gpio) {
22177d2911c4STony Lindgren if (nsdelay)
22187d2911c4STony Lindgren usleep_range(nsdelay, 2 * nsdelay);
22197d2911c4STony Lindgren gpiod_set_value_cansleep(gpio, value);
2220cb6e0b36SUwe Kleine-König }
22217d2911c4STony Lindgren *desc = gpio;
22227d2911c4STony Lindgren
22237d2911c4STony Lindgren return 0;
22247d2911c4STony Lindgren }
22253f823c15STony Lindgren #endif
22263f823c15STony Lindgren
2227ae150435SJeff Kirsher /*
2228ae150435SJeff Kirsher * smc_init(void)
2229ae150435SJeff Kirsher * Input parameters:
2230ae150435SJeff Kirsher * dev->base_addr == 0, try to find all possible locations
2231ae150435SJeff Kirsher * dev->base_addr > 0x1ff, this is the address to check
2232ae150435SJeff Kirsher * dev->base_addr == <anything else>, return failure code
2233ae150435SJeff Kirsher *
2234ae150435SJeff Kirsher * Output:
2235ae150435SJeff Kirsher * 0 --> there is a device
2236ae150435SJeff Kirsher * anything else, error
2237ae150435SJeff Kirsher */
smc_drv_probe(struct platform_device * pdev)22381e48fea4SBill Pemberton static int smc_drv_probe(struct platform_device *pdev)
2239ae150435SJeff Kirsher {
2240f64deacaSJingoo Han struct smc91x_platdata *pd = dev_get_platdata(&pdev->dev);
22413f823c15STony Lindgren const struct of_device_id *match = NULL;
2242ae150435SJeff Kirsher struct smc_local *lp;
2243ae150435SJeff Kirsher struct net_device *ndev;
2244a2d4fcb8SDavid S. Miller struct resource *res;
2245ae150435SJeff Kirsher unsigned int __iomem *addr;
2246ae150435SJeff Kirsher unsigned long irq_flags = SMC_IRQ_FLAGS;
2247a2d4fcb8SDavid S. Miller unsigned long irq_resflags;
2248ae150435SJeff Kirsher int ret;
2249ae150435SJeff Kirsher
2250ae150435SJeff Kirsher ndev = alloc_etherdev(sizeof(struct smc_local));
2251ae150435SJeff Kirsher if (!ndev) {
2252ae150435SJeff Kirsher ret = -ENOMEM;
2253ae150435SJeff Kirsher goto out;
2254ae150435SJeff Kirsher }
2255ae150435SJeff Kirsher SET_NETDEV_DEV(ndev, &pdev->dev);
2256ae150435SJeff Kirsher
2257ae150435SJeff Kirsher /* get configuration from platform data, only allow use of
2258ae150435SJeff Kirsher * bus width if both SMC_CAN_USE_xxx and SMC91X_USE_xxx are set.
2259ae150435SJeff Kirsher */
2260ae150435SJeff Kirsher
2261ae150435SJeff Kirsher lp = netdev_priv(ndev);
22623f823c15STony Lindgren lp->cfg.flags = 0;
2263ae150435SJeff Kirsher
2264ae150435SJeff Kirsher if (pd) {
2265ae150435SJeff Kirsher memcpy(&lp->cfg, pd, sizeof(lp->cfg));
2266ae150435SJeff Kirsher lp->io_shift = SMC91X_IO_SHIFT(lp->cfg.flags);
22672fb04fdfSRussell King
22682fb04fdfSRussell King if (!SMC_8BIT(lp) && !SMC_16BIT(lp)) {
22692fb04fdfSRussell King dev_err(&pdev->dev,
22702fb04fdfSRussell King "at least one of 8-bit or 16-bit access support is required.\n");
22712fb04fdfSRussell King ret = -ENXIO;
22722fb04fdfSRussell King goto out_free_netdev;
22732fb04fdfSRussell King }
22743f823c15STony Lindgren }
22753f823c15STony Lindgren
22763f823c15STony Lindgren #if IS_BUILTIN(CONFIG_OF)
22773f823c15STony Lindgren match = of_match_device(of_match_ptr(smc91x_match), &pdev->dev);
22783f823c15STony Lindgren if (match) {
22793f823c15STony Lindgren u32 val;
22803f823c15STony Lindgren
22817d2911c4STony Lindgren /* Optional pwrdwn GPIO configured? */
22827d2911c4STony Lindgren ret = try_toggle_control_gpio(&pdev->dev, &lp->power_gpio,
22837d2911c4STony Lindgren "power", 0, 0, 100);
22847d2911c4STony Lindgren if (ret)
2285bca9749bSWang Hai goto out_free_netdev;
22867d2911c4STony Lindgren
22877d2911c4STony Lindgren /*
22887d2911c4STony Lindgren * Optional reset GPIO configured? Minimum 100 ns reset needed
22897d2911c4STony Lindgren * according to LAN91C96 datasheet page 14.
22907d2911c4STony Lindgren */
22917d2911c4STony Lindgren ret = try_toggle_control_gpio(&pdev->dev, &lp->reset_gpio,
22927d2911c4STony Lindgren "reset", 0, 0, 100);
22937d2911c4STony Lindgren if (ret)
2294bca9749bSWang Hai goto out_free_netdev;
22957d2911c4STony Lindgren
22967d2911c4STony Lindgren /*
22977d2911c4STony Lindgren * Need to wait for optional EEPROM to load, max 750 us according
22987d2911c4STony Lindgren * to LAN91C96 datasheet page 55.
22997d2911c4STony Lindgren */
23007d2911c4STony Lindgren if (lp->reset_gpio)
23017d2911c4STony Lindgren usleep_range(750, 1000);
23027d2911c4STony Lindgren
23033f823c15STony Lindgren /* Combination of IO widths supported, default to 16-bit */
23048876d94bSJeremy Linton if (!device_property_read_u32(&pdev->dev, "reg-io-width",
23058876d94bSJeremy Linton &val)) {
23063f823c15STony Lindgren if (val & 1)
23073f823c15STony Lindgren lp->cfg.flags |= SMC91X_USE_8BIT;
23083f823c15STony Lindgren if ((val == 0) || (val & 2))
23093f823c15STony Lindgren lp->cfg.flags |= SMC91X_USE_16BIT;
23103f823c15STony Lindgren if (val & 4)
23113f823c15STony Lindgren lp->cfg.flags |= SMC91X_USE_32BIT;
2312ae150435SJeff Kirsher } else {
23133f823c15STony Lindgren lp->cfg.flags |= SMC91X_USE_16BIT;
23143f823c15STony Lindgren }
2315876a55b8SRobert Jarzmik if (!device_property_read_u32(&pdev->dev, "reg-shift",
2316876a55b8SRobert Jarzmik &val))
2317876a55b8SRobert Jarzmik lp->io_shift = val;
23189c365f31SRobert Jarzmik lp->cfg.pxa_u16_align4 =
23199c365f31SRobert Jarzmik device_property_read_bool(&pdev->dev, "pxa-u16-align4");
23203f823c15STony Lindgren }
23213f823c15STony Lindgren #endif
23223f823c15STony Lindgren
23233f823c15STony Lindgren if (!pd && !match) {
2324ae150435SJeff Kirsher lp->cfg.flags |= (SMC_CAN_USE_8BIT) ? SMC91X_USE_8BIT : 0;
2325ae150435SJeff Kirsher lp->cfg.flags |= (SMC_CAN_USE_16BIT) ? SMC91X_USE_16BIT : 0;
2326ae150435SJeff Kirsher lp->cfg.flags |= (SMC_CAN_USE_32BIT) ? SMC91X_USE_32BIT : 0;
2327ae150435SJeff Kirsher lp->cfg.flags |= (nowait) ? SMC91X_NOWAIT : 0;
2328ae150435SJeff Kirsher }
2329ae150435SJeff Kirsher
2330ae150435SJeff Kirsher if (!lp->cfg.leda && !lp->cfg.ledb) {
2331ae150435SJeff Kirsher lp->cfg.leda = RPC_LSA_DEFAULT;
2332ae150435SJeff Kirsher lp->cfg.ledb = RPC_LSB_DEFAULT;
2333ae150435SJeff Kirsher }
2334ae150435SJeff Kirsher
2335ae150435SJeff Kirsher ndev->dma = (unsigned char)-1;
2336ae150435SJeff Kirsher
2337ae150435SJeff Kirsher res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2338ae150435SJeff Kirsher if (!res)
2339ae150435SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2340ae150435SJeff Kirsher if (!res) {
2341ae150435SJeff Kirsher ret = -ENODEV;
2342ae150435SJeff Kirsher goto out_free_netdev;
2343ae150435SJeff Kirsher }
2344ae150435SJeff Kirsher
2345ae150435SJeff Kirsher
2346ae150435SJeff Kirsher if (!request_mem_region(res->start, SMC_IO_EXTENT, CARDNAME)) {
2347ae150435SJeff Kirsher ret = -EBUSY;
2348ae150435SJeff Kirsher goto out_free_netdev;
2349ae150435SJeff Kirsher }
2350ae150435SJeff Kirsher
2351a2d4fcb8SDavid S. Miller ndev->irq = platform_get_irq(pdev, 0);
2352bd59cfc5SRobert Jarzmik if (ndev->irq < 0) {
2353bd59cfc5SRobert Jarzmik ret = ndev->irq;
2354ae150435SJeff Kirsher goto out_release_io;
2355ae150435SJeff Kirsher }
2356a2d4fcb8SDavid S. Miller /*
2357a2d4fcb8SDavid S. Miller * If this platform does not specify any special irqflags, or if
2358a2d4fcb8SDavid S. Miller * the resource supplies a trigger, override the irqflags with
2359a2d4fcb8SDavid S. Miller * the trigger flags from the resource.
2360a2d4fcb8SDavid S. Miller */
2361a2d4fcb8SDavid S. Miller irq_resflags = irqd_get_trigger_type(irq_get_irq_data(ndev->irq));
2362a2d4fcb8SDavid S. Miller if (irq_flags == -1 || irq_resflags & IRQF_TRIGGER_MASK)
2363a2d4fcb8SDavid S. Miller irq_flags = irq_resflags & IRQF_TRIGGER_MASK;
2364ae150435SJeff Kirsher
2365ae150435SJeff Kirsher ret = smc_request_attrib(pdev, ndev);
2366ae150435SJeff Kirsher if (ret)
2367ae150435SJeff Kirsher goto out_release_io;
2368b70661c7SArnd Bergmann #if defined(CONFIG_ASSABET_NEPONSET)
2369b70661c7SArnd Bergmann if (machine_is_assabet() && machine_has_neponset())
23706ad1b614SRussell King neponset_ncr_set(NCR_ENET_OSC_EN);
2371ae150435SJeff Kirsher #endif
2372ae150435SJeff Kirsher platform_set_drvdata(pdev, ndev);
2373ae150435SJeff Kirsher ret = smc_enable_device(pdev);
2374ae150435SJeff Kirsher if (ret)
2375ae150435SJeff Kirsher goto out_release_attrib;
2376ae150435SJeff Kirsher
2377ae150435SJeff Kirsher addr = ioremap(res->start, SMC_IO_EXTENT);
2378ae150435SJeff Kirsher if (!addr) {
2379ae150435SJeff Kirsher ret = -ENOMEM;
2380ae150435SJeff Kirsher goto out_release_attrib;
2381ae150435SJeff Kirsher }
2382ae150435SJeff Kirsher
2383ae150435SJeff Kirsher #ifdef CONFIG_ARCH_PXA
2384ae150435SJeff Kirsher {
2385ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2386ae150435SJeff Kirsher lp->device = &pdev->dev;
2387ae150435SJeff Kirsher lp->physaddr = res->start;
2388d24c8f24SRobert Jarzmik
2389ae150435SJeff Kirsher }
2390ae150435SJeff Kirsher #endif
2391ae150435SJeff Kirsher
2392ae150435SJeff Kirsher ret = smc_probe(ndev, addr, irq_flags);
2393ae150435SJeff Kirsher if (ret != 0)
2394ae150435SJeff Kirsher goto out_iounmap;
2395ae150435SJeff Kirsher
2396ae150435SJeff Kirsher smc_request_datacs(pdev, ndev);
2397ae150435SJeff Kirsher
2398ae150435SJeff Kirsher return 0;
2399ae150435SJeff Kirsher
2400ae150435SJeff Kirsher out_iounmap:
2401ae150435SJeff Kirsher iounmap(addr);
2402ae150435SJeff Kirsher out_release_attrib:
2403ae150435SJeff Kirsher smc_release_attrib(pdev, ndev);
2404ae150435SJeff Kirsher out_release_io:
2405ae150435SJeff Kirsher release_mem_region(res->start, SMC_IO_EXTENT);
2406ae150435SJeff Kirsher out_free_netdev:
2407ae150435SJeff Kirsher free_netdev(ndev);
2408ae150435SJeff Kirsher out:
24096389aa45SBen Boeckel pr_info("%s: not found (%d).\n", CARDNAME, ret);
2410ae150435SJeff Kirsher
2411ae150435SJeff Kirsher return ret;
2412ae150435SJeff Kirsher }
2413ae150435SJeff Kirsher
smc_drv_remove(struct platform_device * pdev)24141e48fea4SBill Pemberton static int smc_drv_remove(struct platform_device *pdev)
2415ae150435SJeff Kirsher {
2416ae150435SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev);
2417ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2418ae150435SJeff Kirsher struct resource *res;
2419ae150435SJeff Kirsher
2420ae150435SJeff Kirsher unregister_netdev(ndev);
2421ae150435SJeff Kirsher
2422ae150435SJeff Kirsher free_irq(ndev->irq, ndev);
2423ae150435SJeff Kirsher
2424ae150435SJeff Kirsher #ifdef CONFIG_ARCH_PXA
2425d24c8f24SRobert Jarzmik if (lp->dma_chan)
2426d24c8f24SRobert Jarzmik dma_release_channel(lp->dma_chan);
2427ae150435SJeff Kirsher #endif
2428ae150435SJeff Kirsher iounmap(lp->base);
2429ae150435SJeff Kirsher
2430ae150435SJeff Kirsher smc_release_datacs(pdev,ndev);
2431ae150435SJeff Kirsher smc_release_attrib(pdev,ndev);
2432ae150435SJeff Kirsher
2433ae150435SJeff Kirsher res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smc91x-regs");
2434ae150435SJeff Kirsher if (!res)
2435ae150435SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2436ae150435SJeff Kirsher release_mem_region(res->start, SMC_IO_EXTENT);
2437ae150435SJeff Kirsher
2438ae150435SJeff Kirsher free_netdev(ndev);
2439ae150435SJeff Kirsher
2440ae150435SJeff Kirsher return 0;
2441ae150435SJeff Kirsher }
2442ae150435SJeff Kirsher
smc_drv_suspend(struct device * dev)2443ae150435SJeff Kirsher static int smc_drv_suspend(struct device *dev)
2444ae150435SJeff Kirsher {
24459dd8360cSWolfram Sang struct net_device *ndev = dev_get_drvdata(dev);
2446ae150435SJeff Kirsher
2447ae150435SJeff Kirsher if (ndev) {
2448ae150435SJeff Kirsher if (netif_running(ndev)) {
2449ae150435SJeff Kirsher netif_device_detach(ndev);
2450ae150435SJeff Kirsher smc_shutdown(ndev);
2451ae150435SJeff Kirsher smc_phy_powerdown(ndev);
2452ae150435SJeff Kirsher }
2453ae150435SJeff Kirsher }
2454ae150435SJeff Kirsher return 0;
2455ae150435SJeff Kirsher }
2456ae150435SJeff Kirsher
smc_drv_resume(struct device * dev)2457ae150435SJeff Kirsher static int smc_drv_resume(struct device *dev)
2458ae150435SJeff Kirsher {
2459ae150435SJeff Kirsher struct platform_device *pdev = to_platform_device(dev);
2460ae150435SJeff Kirsher struct net_device *ndev = platform_get_drvdata(pdev);
2461ae150435SJeff Kirsher
2462ae150435SJeff Kirsher if (ndev) {
2463ae150435SJeff Kirsher struct smc_local *lp = netdev_priv(ndev);
2464ae150435SJeff Kirsher smc_enable_device(pdev);
2465ae150435SJeff Kirsher if (netif_running(ndev)) {
2466ae150435SJeff Kirsher smc_reset(ndev);
2467ae150435SJeff Kirsher smc_enable(ndev);
2468ae150435SJeff Kirsher if (lp->phy_type != 0)
2469ae150435SJeff Kirsher smc_phy_configure(&lp->phy_configure);
2470ae150435SJeff Kirsher netif_device_attach(ndev);
2471ae150435SJeff Kirsher }
2472ae150435SJeff Kirsher }
2473ae150435SJeff Kirsher return 0;
2474ae150435SJeff Kirsher }
2475ae150435SJeff Kirsher
2476d19724ecSArvind Yadav static const struct dev_pm_ops smc_drv_pm_ops = {
2477ae150435SJeff Kirsher .suspend = smc_drv_suspend,
2478ae150435SJeff Kirsher .resume = smc_drv_resume,
2479ae150435SJeff Kirsher };
2480ae150435SJeff Kirsher
2481ae150435SJeff Kirsher static struct platform_driver smc_driver = {
2482ae150435SJeff Kirsher .probe = smc_drv_probe,
24831e48fea4SBill Pemberton .remove = smc_drv_remove,
2484ae150435SJeff Kirsher .driver = {
2485ae150435SJeff Kirsher .name = CARDNAME,
2486ae150435SJeff Kirsher .pm = &smc_drv_pm_ops,
248789ce376cSSachin Kamat .of_match_table = of_match_ptr(smc91x_match),
24888876d94bSJeremy Linton .acpi_match_table = smc91x_acpi_match,
2489ae150435SJeff Kirsher },
2490ae150435SJeff Kirsher };
2491ae150435SJeff Kirsher
2492db62f684SAxel Lin module_platform_driver(smc_driver);
2493