1c861ef83SShannon Nelson // SPDX-License-Identifier: GPL-2.0
2e689cf4aSJeff Kirsher /* sunhme.c: Sparc HME/BigMac 10/100baseT half/full duplex auto switching,
3e689cf4aSJeff Kirsher * auto carrier detecting ethernet driver. Also known as the
4e689cf4aSJeff Kirsher * "Happy Meal Ethernet" found on SunSwift SBUS cards.
5e689cf4aSJeff Kirsher *
6e689cf4aSJeff Kirsher * Copyright (C) 1996, 1998, 1999, 2002, 2003,
7e689cf4aSJeff Kirsher * 2006, 2008 David S. Miller (davem@davemloft.net)
8e689cf4aSJeff Kirsher *
9e689cf4aSJeff Kirsher * Changes :
10e689cf4aSJeff Kirsher * 2000/11/11 Willy Tarreau <willy AT meta-x.org>
11e689cf4aSJeff Kirsher * - port to non-sparc architectures. Tested only on x86 and
12e689cf4aSJeff Kirsher * only currently works with QFE PCI cards.
13e689cf4aSJeff Kirsher * - ability to specify the MAC address at module load time by passing this
14e689cf4aSJeff Kirsher * argument : macaddr=0x00,0x10,0x20,0x30,0x40,0x50
15e689cf4aSJeff Kirsher */
16e689cf4aSJeff Kirsher
171ff4f42aSSean Anderson #include <linux/bitops.h>
181ff4f42aSSean Anderson #include <linux/crc32.h>
191ff4f42aSSean Anderson #include <linux/delay.h>
201ff4f42aSSean Anderson #include <linux/dma-mapping.h>
211ff4f42aSSean Anderson #include <linux/errno.h>
221ff4f42aSSean Anderson #include <linux/etherdevice.h>
231ff4f42aSSean Anderson #include <linux/ethtool.h>
24e689cf4aSJeff Kirsher #include <linux/fcntl.h>
25e689cf4aSJeff Kirsher #include <linux/in.h>
261ff4f42aSSean Anderson #include <linux/init.h>
271ff4f42aSSean Anderson #include <linux/interrupt.h>
281ff4f42aSSean Anderson #include <linux/io.h>
291ff4f42aSSean Anderson #include <linux/ioport.h>
301ff4f42aSSean Anderson #include <linux/kernel.h>
311ff4f42aSSean Anderson #include <linux/mii.h>
321ff4f42aSSean Anderson #include <linux/mm.h>
331ff4f42aSSean Anderson #include <linux/module.h>
341ff4f42aSSean Anderson #include <linux/netdevice.h>
351ff4f42aSSean Anderson #include <linux/of.h>
36*3d40aed8SRob Herring #include <linux/of_device.h>
371ff4f42aSSean Anderson #include <linux/pci.h>
38*3d40aed8SRob Herring #include <linux/platform_device.h>
391ff4f42aSSean Anderson #include <linux/random.h>
401ff4f42aSSean Anderson #include <linux/skbuff.h>
41e689cf4aSJeff Kirsher #include <linux/slab.h>
42e689cf4aSJeff Kirsher #include <linux/string.h>
431ff4f42aSSean Anderson #include <linux/types.h>
441ff4f42aSSean Anderson #include <linux/uaccess.h>
45e689cf4aSJeff Kirsher
46f8b648bfSSimon Horman #include <asm/byteorder.h>
47f8b648bfSSimon Horman #include <asm/dma.h>
48f8b648bfSSimon Horman #include <asm/irq.h>
49f8b648bfSSimon Horman
50e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC
511ff4f42aSSean Anderson #include <asm/auxio.h>
52e689cf4aSJeff Kirsher #include <asm/idprom.h>
53e689cf4aSJeff Kirsher #include <asm/openprom.h>
54e689cf4aSJeff Kirsher #include <asm/oplib.h>
55e689cf4aSJeff Kirsher #include <asm/prom.h>
56e689cf4aSJeff Kirsher #endif
57e689cf4aSJeff Kirsher
58e689cf4aSJeff Kirsher #include "sunhme.h"
59e689cf4aSJeff Kirsher
60e689cf4aSJeff Kirsher #define DRV_NAME "sunhme"
61e689cf4aSJeff Kirsher
626478c6e9SSean Anderson MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
63e689cf4aSJeff Kirsher MODULE_DESCRIPTION("Sun HappyMealEthernet(HME) 10/100baseT ethernet driver");
64e689cf4aSJeff Kirsher MODULE_LICENSE("GPL");
65e689cf4aSJeff Kirsher
66e689cf4aSJeff Kirsher static int macaddr[6];
67e689cf4aSJeff Kirsher
68e689cf4aSJeff Kirsher /* accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
69e689cf4aSJeff Kirsher module_param_array(macaddr, int, NULL, 0);
70e689cf4aSJeff Kirsher MODULE_PARM_DESC(macaddr, "Happy Meal MAC address to set");
71e689cf4aSJeff Kirsher
72e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
73e689cf4aSJeff Kirsher static struct quattro *qfe_sbus_list;
74e689cf4aSJeff Kirsher #endif
75e689cf4aSJeff Kirsher
76e689cf4aSJeff Kirsher #ifdef CONFIG_PCI
77e689cf4aSJeff Kirsher static struct quattro *qfe_pci_list;
78e689cf4aSJeff Kirsher #endif
79e689cf4aSJeff Kirsher
8024cddbc3SSean Anderson #define hme_debug(fmt, ...) pr_debug("%s: " fmt, __func__, ##__VA_ARGS__)
8124cddbc3SSean Anderson #define HMD hme_debug
82e689cf4aSJeff Kirsher
8330931367SSean Anderson /* "Auto Switch Debug" aka phy debug */
8424cddbc3SSean Anderson #if 1
8524cddbc3SSean Anderson #define ASD hme_debug
8630931367SSean Anderson #else
8730931367SSean Anderson #define ASD(...)
8830931367SSean Anderson #endif
8930931367SSean Anderson
9030931367SSean Anderson #if 0
91e689cf4aSJeff Kirsher struct hme_tx_logent {
92e689cf4aSJeff Kirsher unsigned int tstamp;
93e689cf4aSJeff Kirsher int tx_new, tx_old;
94e689cf4aSJeff Kirsher unsigned int action;
95e689cf4aSJeff Kirsher #define TXLOG_ACTION_IRQ 0x01
96e689cf4aSJeff Kirsher #define TXLOG_ACTION_TXMIT 0x02
97e689cf4aSJeff Kirsher #define TXLOG_ACTION_TBUSY 0x04
98e689cf4aSJeff Kirsher #define TXLOG_ACTION_NBUFS 0x08
99e689cf4aSJeff Kirsher unsigned int status;
100e689cf4aSJeff Kirsher };
101e689cf4aSJeff Kirsher #define TX_LOG_LEN 128
102e689cf4aSJeff Kirsher static struct hme_tx_logent tx_log[TX_LOG_LEN];
103e689cf4aSJeff Kirsher static int txlog_cur_entry;
104e689cf4aSJeff Kirsher static __inline__ void tx_add_log(struct happy_meal *hp, unsigned int a, unsigned int s)
105e689cf4aSJeff Kirsher {
106e689cf4aSJeff Kirsher struct hme_tx_logent *tlp;
107e689cf4aSJeff Kirsher unsigned long flags;
108e689cf4aSJeff Kirsher
109e689cf4aSJeff Kirsher local_irq_save(flags);
110e689cf4aSJeff Kirsher tlp = &tx_log[txlog_cur_entry];
111e689cf4aSJeff Kirsher tlp->tstamp = (unsigned int)jiffies;
112e689cf4aSJeff Kirsher tlp->tx_new = hp->tx_new;
113e689cf4aSJeff Kirsher tlp->tx_old = hp->tx_old;
114e689cf4aSJeff Kirsher tlp->action = a;
115e689cf4aSJeff Kirsher tlp->status = s;
116e689cf4aSJeff Kirsher txlog_cur_entry = (txlog_cur_entry + 1) & (TX_LOG_LEN - 1);
117e689cf4aSJeff Kirsher local_irq_restore(flags);
118e689cf4aSJeff Kirsher }
119e689cf4aSJeff Kirsher static __inline__ void tx_dump_log(void)
120e689cf4aSJeff Kirsher {
121e689cf4aSJeff Kirsher int i, this;
122e689cf4aSJeff Kirsher
123e689cf4aSJeff Kirsher this = txlog_cur_entry;
124e689cf4aSJeff Kirsher for (i = 0; i < TX_LOG_LEN; i++) {
1250bc1f454SSean Anderson pr_err("TXLOG[%d]: j[%08x] tx[N(%d)O(%d)] action[%08x] stat[%08x]\n", i,
126e689cf4aSJeff Kirsher tx_log[this].tstamp,
127e689cf4aSJeff Kirsher tx_log[this].tx_new, tx_log[this].tx_old,
128e689cf4aSJeff Kirsher tx_log[this].action, tx_log[this].status);
129e689cf4aSJeff Kirsher this = (this + 1) & (TX_LOG_LEN - 1);
130e689cf4aSJeff Kirsher }
131e689cf4aSJeff Kirsher }
132e689cf4aSJeff Kirsher #else
13330931367SSean Anderson #define tx_add_log(hp, a, s)
13430931367SSean Anderson #define tx_dump_log()
135e689cf4aSJeff Kirsher #endif
136e689cf4aSJeff Kirsher
137e689cf4aSJeff Kirsher #define DEFAULT_IPG0 16 /* For lance-mode only */
138e689cf4aSJeff Kirsher #define DEFAULT_IPG1 8 /* For all modes */
139e689cf4aSJeff Kirsher #define DEFAULT_IPG2 4 /* For all modes */
140e689cf4aSJeff Kirsher #define DEFAULT_JAMSIZE 4 /* Toe jam */
141e689cf4aSJeff Kirsher
142e689cf4aSJeff Kirsher /* NOTE: In the descriptor writes one _must_ write the address
143e689cf4aSJeff Kirsher * member _first_. The card must not be allowed to see
144e689cf4aSJeff Kirsher * the updated descriptor flags until the address is
145e689cf4aSJeff Kirsher * correct. I've added a write memory barrier between
146e689cf4aSJeff Kirsher * the two stores so that I can sleep well at night... -DaveM
147e689cf4aSJeff Kirsher */
148e689cf4aSJeff Kirsher
149e689cf4aSJeff Kirsher #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
sbus_hme_write32(void __iomem * reg,u32 val)150e689cf4aSJeff Kirsher static void sbus_hme_write32(void __iomem *reg, u32 val)
151e689cf4aSJeff Kirsher {
152e689cf4aSJeff Kirsher sbus_writel(val, reg);
153e689cf4aSJeff Kirsher }
154e689cf4aSJeff Kirsher
sbus_hme_read32(void __iomem * reg)155e689cf4aSJeff Kirsher static u32 sbus_hme_read32(void __iomem *reg)
156e689cf4aSJeff Kirsher {
157e689cf4aSJeff Kirsher return sbus_readl(reg);
158e689cf4aSJeff Kirsher }
159e689cf4aSJeff Kirsher
sbus_hme_write_rxd(struct happy_meal_rxd * rxd,u32 flags,u32 addr)160e689cf4aSJeff Kirsher static void sbus_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
161e689cf4aSJeff Kirsher {
162e689cf4aSJeff Kirsher rxd->rx_addr = (__force hme32)addr;
163b4468cc6SAlexander Duyck dma_wmb();
164e689cf4aSJeff Kirsher rxd->rx_flags = (__force hme32)flags;
165e689cf4aSJeff Kirsher }
166e689cf4aSJeff Kirsher
sbus_hme_write_txd(struct happy_meal_txd * txd,u32 flags,u32 addr)167e689cf4aSJeff Kirsher static void sbus_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
168e689cf4aSJeff Kirsher {
169e689cf4aSJeff Kirsher txd->tx_addr = (__force hme32)addr;
170b4468cc6SAlexander Duyck dma_wmb();
171e689cf4aSJeff Kirsher txd->tx_flags = (__force hme32)flags;
172e689cf4aSJeff Kirsher }
173e689cf4aSJeff Kirsher
sbus_hme_read_desc32(hme32 * p)174e689cf4aSJeff Kirsher static u32 sbus_hme_read_desc32(hme32 *p)
175e689cf4aSJeff Kirsher {
176e689cf4aSJeff Kirsher return (__force u32)*p;
177e689cf4aSJeff Kirsher }
178e689cf4aSJeff Kirsher
pci_hme_write32(void __iomem * reg,u32 val)179e689cf4aSJeff Kirsher static void pci_hme_write32(void __iomem *reg, u32 val)
180e689cf4aSJeff Kirsher {
181e689cf4aSJeff Kirsher writel(val, reg);
182e689cf4aSJeff Kirsher }
183e689cf4aSJeff Kirsher
pci_hme_read32(void __iomem * reg)184e689cf4aSJeff Kirsher static u32 pci_hme_read32(void __iomem *reg)
185e689cf4aSJeff Kirsher {
186e689cf4aSJeff Kirsher return readl(reg);
187e689cf4aSJeff Kirsher }
188e689cf4aSJeff Kirsher
pci_hme_write_rxd(struct happy_meal_rxd * rxd,u32 flags,u32 addr)189e689cf4aSJeff Kirsher static void pci_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
190e689cf4aSJeff Kirsher {
191e689cf4aSJeff Kirsher rxd->rx_addr = (__force hme32)cpu_to_le32(addr);
192b4468cc6SAlexander Duyck dma_wmb();
193e689cf4aSJeff Kirsher rxd->rx_flags = (__force hme32)cpu_to_le32(flags);
194e689cf4aSJeff Kirsher }
195e689cf4aSJeff Kirsher
pci_hme_write_txd(struct happy_meal_txd * txd,u32 flags,u32 addr)196e689cf4aSJeff Kirsher static void pci_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
197e689cf4aSJeff Kirsher {
198e689cf4aSJeff Kirsher txd->tx_addr = (__force hme32)cpu_to_le32(addr);
199b4468cc6SAlexander Duyck dma_wmb();
200e689cf4aSJeff Kirsher txd->tx_flags = (__force hme32)cpu_to_le32(flags);
201e689cf4aSJeff Kirsher }
202e689cf4aSJeff Kirsher
pci_hme_read_desc32(hme32 * p)203e689cf4aSJeff Kirsher static u32 pci_hme_read_desc32(hme32 *p)
204e689cf4aSJeff Kirsher {
205e689cf4aSJeff Kirsher return le32_to_cpup((__le32 *)p);
206e689cf4aSJeff Kirsher }
207e689cf4aSJeff Kirsher
208e689cf4aSJeff Kirsher #define hme_write32(__hp, __reg, __val) \
209e689cf4aSJeff Kirsher ((__hp)->write32((__reg), (__val)))
210e689cf4aSJeff Kirsher #define hme_read32(__hp, __reg) \
211e689cf4aSJeff Kirsher ((__hp)->read32(__reg))
212e689cf4aSJeff Kirsher #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
213e689cf4aSJeff Kirsher ((__hp)->write_rxd((__rxd), (__flags), (__addr)))
214e689cf4aSJeff Kirsher #define hme_write_txd(__hp, __txd, __flags, __addr) \
215e689cf4aSJeff Kirsher ((__hp)->write_txd((__txd), (__flags), (__addr)))
216e689cf4aSJeff Kirsher #define hme_read_desc32(__hp, __p) \
217e689cf4aSJeff Kirsher ((__hp)->read_desc32(__p))
218e689cf4aSJeff Kirsher #else
219e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
220e689cf4aSJeff Kirsher /* SBUS only compilation */
221e689cf4aSJeff Kirsher #define hme_write32(__hp, __reg, __val) \
222e689cf4aSJeff Kirsher sbus_writel((__val), (__reg))
223e689cf4aSJeff Kirsher #define hme_read32(__hp, __reg) \
224e689cf4aSJeff Kirsher sbus_readl(__reg)
225e689cf4aSJeff Kirsher #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
226e689cf4aSJeff Kirsher do { (__rxd)->rx_addr = (__force hme32)(u32)(__addr); \
227b4468cc6SAlexander Duyck dma_wmb(); \
228e689cf4aSJeff Kirsher (__rxd)->rx_flags = (__force hme32)(u32)(__flags); \
229e689cf4aSJeff Kirsher } while(0)
230e689cf4aSJeff Kirsher #define hme_write_txd(__hp, __txd, __flags, __addr) \
231e689cf4aSJeff Kirsher do { (__txd)->tx_addr = (__force hme32)(u32)(__addr); \
232b4468cc6SAlexander Duyck dma_wmb(); \
233e689cf4aSJeff Kirsher (__txd)->tx_flags = (__force hme32)(u32)(__flags); \
234e689cf4aSJeff Kirsher } while(0)
235e689cf4aSJeff Kirsher #define hme_read_desc32(__hp, __p) ((__force u32)(hme32)*(__p))
236e689cf4aSJeff Kirsher #else
237e689cf4aSJeff Kirsher /* PCI only compilation */
238e689cf4aSJeff Kirsher #define hme_write32(__hp, __reg, __val) \
239e689cf4aSJeff Kirsher writel((__val), (__reg))
240e689cf4aSJeff Kirsher #define hme_read32(__hp, __reg) \
241e689cf4aSJeff Kirsher readl(__reg)
242e689cf4aSJeff Kirsher #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
243e689cf4aSJeff Kirsher do { (__rxd)->rx_addr = (__force hme32)cpu_to_le32(__addr); \
244b4468cc6SAlexander Duyck dma_wmb(); \
245e689cf4aSJeff Kirsher (__rxd)->rx_flags = (__force hme32)cpu_to_le32(__flags); \
246e689cf4aSJeff Kirsher } while(0)
247e689cf4aSJeff Kirsher #define hme_write_txd(__hp, __txd, __flags, __addr) \
248e689cf4aSJeff Kirsher do { (__txd)->tx_addr = (__force hme32)cpu_to_le32(__addr); \
249b4468cc6SAlexander Duyck dma_wmb(); \
250e689cf4aSJeff Kirsher (__txd)->tx_flags = (__force hme32)cpu_to_le32(__flags); \
251e689cf4aSJeff Kirsher } while(0)
hme_read_desc32(struct happy_meal * hp,hme32 * p)252e689cf4aSJeff Kirsher static inline u32 hme_read_desc32(struct happy_meal *hp, hme32 *p)
253e689cf4aSJeff Kirsher {
254e689cf4aSJeff Kirsher return le32_to_cpup((__le32 *)p);
255e689cf4aSJeff Kirsher }
256e689cf4aSJeff Kirsher #endif
257e689cf4aSJeff Kirsher #endif
258e689cf4aSJeff Kirsher
259e689cf4aSJeff Kirsher
260e689cf4aSJeff Kirsher /* Oh yes, the MIF BitBang is mighty fun to program. BitBucket is more like it. */
BB_PUT_BIT(struct happy_meal * hp,void __iomem * tregs,int bit)261e689cf4aSJeff Kirsher static void BB_PUT_BIT(struct happy_meal *hp, void __iomem *tregs, int bit)
262e689cf4aSJeff Kirsher {
263e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBDATA, bit);
264e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
265e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
266e689cf4aSJeff Kirsher }
267e689cf4aSJeff Kirsher
268e689cf4aSJeff Kirsher #if 0
269e689cf4aSJeff Kirsher static u32 BB_GET_BIT(struct happy_meal *hp, void __iomem *tregs, int internal)
270e689cf4aSJeff Kirsher {
271e689cf4aSJeff Kirsher u32 ret;
272e689cf4aSJeff Kirsher
273e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
274e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
275e689cf4aSJeff Kirsher ret = hme_read32(hp, tregs + TCVR_CFG);
276e689cf4aSJeff Kirsher if (internal)
277e689cf4aSJeff Kirsher ret &= TCV_CFG_MDIO0;
278e689cf4aSJeff Kirsher else
279e689cf4aSJeff Kirsher ret &= TCV_CFG_MDIO1;
280e689cf4aSJeff Kirsher
281e689cf4aSJeff Kirsher return ret;
282e689cf4aSJeff Kirsher }
283e689cf4aSJeff Kirsher #endif
284e689cf4aSJeff Kirsher
BB_GET_BIT2(struct happy_meal * hp,void __iomem * tregs,int internal)285e689cf4aSJeff Kirsher static u32 BB_GET_BIT2(struct happy_meal *hp, void __iomem *tregs, int internal)
286e689cf4aSJeff Kirsher {
287e689cf4aSJeff Kirsher u32 retval;
288e689cf4aSJeff Kirsher
289e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
290e689cf4aSJeff Kirsher udelay(1);
291e689cf4aSJeff Kirsher retval = hme_read32(hp, tregs + TCVR_CFG);
292e689cf4aSJeff Kirsher if (internal)
293e689cf4aSJeff Kirsher retval &= TCV_CFG_MDIO0;
294e689cf4aSJeff Kirsher else
295e689cf4aSJeff Kirsher retval &= TCV_CFG_MDIO1;
296e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
297e689cf4aSJeff Kirsher
298e689cf4aSJeff Kirsher return retval;
299e689cf4aSJeff Kirsher }
300e689cf4aSJeff Kirsher
301e689cf4aSJeff Kirsher #define TCVR_FAILURE 0x80000000 /* Impossible MIF read value */
302e689cf4aSJeff Kirsher
happy_meal_bb_read(struct happy_meal * hp,void __iomem * tregs,int reg)303e689cf4aSJeff Kirsher static int happy_meal_bb_read(struct happy_meal *hp,
304e689cf4aSJeff Kirsher void __iomem *tregs, int reg)
305e689cf4aSJeff Kirsher {
306e689cf4aSJeff Kirsher u32 tmp;
307e689cf4aSJeff Kirsher int retval = 0;
308e689cf4aSJeff Kirsher int i;
309e689cf4aSJeff Kirsher
310e689cf4aSJeff Kirsher /* Enable the MIF BitBang outputs. */
311e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBOENAB, 1);
312e689cf4aSJeff Kirsher
313e689cf4aSJeff Kirsher /* Force BitBang into the idle state. */
314e689cf4aSJeff Kirsher for (i = 0; i < 32; i++)
315e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
316e689cf4aSJeff Kirsher
317e689cf4aSJeff Kirsher /* Give it the read sequence. */
318e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 0);
319e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
320e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
321e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 0);
322e689cf4aSJeff Kirsher
323e689cf4aSJeff Kirsher /* Give it the PHY address. */
324e689cf4aSJeff Kirsher tmp = hp->paddr & 0xff;
325e689cf4aSJeff Kirsher for (i = 4; i >= 0; i--)
326e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
327e689cf4aSJeff Kirsher
328e689cf4aSJeff Kirsher /* Tell it what register we want to read. */
329e689cf4aSJeff Kirsher tmp = (reg & 0xff);
330e689cf4aSJeff Kirsher for (i = 4; i >= 0; i--)
331e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
332e689cf4aSJeff Kirsher
333e689cf4aSJeff Kirsher /* Close down the MIF BitBang outputs. */
334e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBOENAB, 0);
335e689cf4aSJeff Kirsher
336e689cf4aSJeff Kirsher /* Now read in the value. */
337e689cf4aSJeff Kirsher (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
338e689cf4aSJeff Kirsher for (i = 15; i >= 0; i--)
339e689cf4aSJeff Kirsher retval |= BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
340e689cf4aSJeff Kirsher (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
341e689cf4aSJeff Kirsher (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
342e689cf4aSJeff Kirsher (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
34324cddbc3SSean Anderson ASD("reg=%d value=%x\n", reg, retval);
344e689cf4aSJeff Kirsher return retval;
345e689cf4aSJeff Kirsher }
346e689cf4aSJeff Kirsher
happy_meal_bb_write(struct happy_meal * hp,void __iomem * tregs,int reg,unsigned short value)347e689cf4aSJeff Kirsher static void happy_meal_bb_write(struct happy_meal *hp,
348e689cf4aSJeff Kirsher void __iomem *tregs, int reg,
349e689cf4aSJeff Kirsher unsigned short value)
350e689cf4aSJeff Kirsher {
351e689cf4aSJeff Kirsher u32 tmp;
352e689cf4aSJeff Kirsher int i;
353e689cf4aSJeff Kirsher
35424cddbc3SSean Anderson ASD("reg=%d value=%x\n", reg, value);
355e689cf4aSJeff Kirsher
356e689cf4aSJeff Kirsher /* Enable the MIF BitBang outputs. */
357e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBOENAB, 1);
358e689cf4aSJeff Kirsher
359e689cf4aSJeff Kirsher /* Force BitBang into the idle state. */
360e689cf4aSJeff Kirsher for (i = 0; i < 32; i++)
361e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
362e689cf4aSJeff Kirsher
363e689cf4aSJeff Kirsher /* Give it write sequence. */
364e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 0);
365e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
366e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 0);
367e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
368e689cf4aSJeff Kirsher
369e689cf4aSJeff Kirsher /* Give it the PHY address. */
370e689cf4aSJeff Kirsher tmp = (hp->paddr & 0xff);
371e689cf4aSJeff Kirsher for (i = 4; i >= 0; i--)
372e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
373e689cf4aSJeff Kirsher
374e689cf4aSJeff Kirsher /* Tell it what register we will be writing. */
375e689cf4aSJeff Kirsher tmp = (reg & 0xff);
376e689cf4aSJeff Kirsher for (i = 4; i >= 0; i--)
377e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
378e689cf4aSJeff Kirsher
379e689cf4aSJeff Kirsher /* Tell it to become ready for the bits. */
380e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 1);
381e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, 0);
382e689cf4aSJeff Kirsher
383e689cf4aSJeff Kirsher for (i = 15; i >= 0; i--)
384e689cf4aSJeff Kirsher BB_PUT_BIT(hp, tregs, ((value >> i) & 1));
385e689cf4aSJeff Kirsher
386e689cf4aSJeff Kirsher /* Close down the MIF BitBang outputs. */
387e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_BBOENAB, 0);
388e689cf4aSJeff Kirsher }
389e689cf4aSJeff Kirsher
390e689cf4aSJeff Kirsher #define TCVR_READ_TRIES 16
391e689cf4aSJeff Kirsher
happy_meal_tcvr_read(struct happy_meal * hp,void __iomem * tregs,int reg)392e689cf4aSJeff Kirsher static int happy_meal_tcvr_read(struct happy_meal *hp,
393e689cf4aSJeff Kirsher void __iomem *tregs, int reg)
394e689cf4aSJeff Kirsher {
395e689cf4aSJeff Kirsher int tries = TCVR_READ_TRIES;
396e689cf4aSJeff Kirsher int retval;
397e689cf4aSJeff Kirsher
398e689cf4aSJeff Kirsher if (hp->tcvr_type == none) {
39903290907SSean Anderson ASD("no transceiver, value=TCVR_FAILURE\n");
400e689cf4aSJeff Kirsher return TCVR_FAILURE;
401e689cf4aSJeff Kirsher }
402e689cf4aSJeff Kirsher
403e689cf4aSJeff Kirsher if (!(hp->happy_flags & HFLAG_FENABLE)) {
40403290907SSean Anderson ASD("doing bit bang\n");
405e689cf4aSJeff Kirsher return happy_meal_bb_read(hp, tregs, reg);
406e689cf4aSJeff Kirsher }
407e689cf4aSJeff Kirsher
408e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_FRAME,
409e689cf4aSJeff Kirsher (FRAME_READ | (hp->paddr << 23) | ((reg & 0xff) << 18)));
410e689cf4aSJeff Kirsher while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
411e689cf4aSJeff Kirsher udelay(20);
412e689cf4aSJeff Kirsher if (!tries) {
4138acf878fSSean Anderson netdev_err(hp->dev, "Aieee, transceiver MIF read bolixed\n");
414e689cf4aSJeff Kirsher return TCVR_FAILURE;
415e689cf4aSJeff Kirsher }
416e689cf4aSJeff Kirsher retval = hme_read32(hp, tregs + TCVR_FRAME) & 0xffff;
41724cddbc3SSean Anderson ASD("reg=0x%02x value=%04x\n", reg, retval);
418e689cf4aSJeff Kirsher return retval;
419e689cf4aSJeff Kirsher }
420e689cf4aSJeff Kirsher
421e689cf4aSJeff Kirsher #define TCVR_WRITE_TRIES 16
422e689cf4aSJeff Kirsher
happy_meal_tcvr_write(struct happy_meal * hp,void __iomem * tregs,int reg,unsigned short value)423e689cf4aSJeff Kirsher static void happy_meal_tcvr_write(struct happy_meal *hp,
424e689cf4aSJeff Kirsher void __iomem *tregs, int reg,
425e689cf4aSJeff Kirsher unsigned short value)
426e689cf4aSJeff Kirsher {
427e689cf4aSJeff Kirsher int tries = TCVR_WRITE_TRIES;
428e689cf4aSJeff Kirsher
42924cddbc3SSean Anderson ASD("reg=0x%02x value=%04x\n", reg, value);
430e689cf4aSJeff Kirsher
431e689cf4aSJeff Kirsher /* Welcome to Sun Microsystems, can I take your order please? */
432e689cf4aSJeff Kirsher if (!(hp->happy_flags & HFLAG_FENABLE)) {
433e689cf4aSJeff Kirsher happy_meal_bb_write(hp, tregs, reg, value);
434e689cf4aSJeff Kirsher return;
435e689cf4aSJeff Kirsher }
436e689cf4aSJeff Kirsher
437e689cf4aSJeff Kirsher /* Would you like fries with that? */
438e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_FRAME,
439e689cf4aSJeff Kirsher (FRAME_WRITE | (hp->paddr << 23) |
440e689cf4aSJeff Kirsher ((reg & 0xff) << 18) | (value & 0xffff)));
441e689cf4aSJeff Kirsher while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
442e689cf4aSJeff Kirsher udelay(20);
443e689cf4aSJeff Kirsher
444e689cf4aSJeff Kirsher /* Anything else? */
445e689cf4aSJeff Kirsher if (!tries)
4468acf878fSSean Anderson netdev_err(hp->dev, "Aieee, transceiver MIF write bolixed\n");
447e689cf4aSJeff Kirsher
448e689cf4aSJeff Kirsher /* Fifty-two cents is your change, have a nice day. */
449e689cf4aSJeff Kirsher }
450e689cf4aSJeff Kirsher
451e689cf4aSJeff Kirsher /* Auto negotiation. The scheme is very simple. We have a timer routine
452e689cf4aSJeff Kirsher * that keeps watching the auto negotiation process as it progresses.
453e689cf4aSJeff Kirsher * The DP83840 is first told to start doing it's thing, we set up the time
454e689cf4aSJeff Kirsher * and place the timer state machine in it's initial state.
455e689cf4aSJeff Kirsher *
456e689cf4aSJeff Kirsher * Here the timer peeks at the DP83840 status registers at each click to see
457e689cf4aSJeff Kirsher * if the auto negotiation has completed, we assume here that the DP83840 PHY
458e689cf4aSJeff Kirsher * will time out at some point and just tell us what (didn't) happen. For
459e689cf4aSJeff Kirsher * complete coverage we only allow so many of the ticks at this level to run,
460e689cf4aSJeff Kirsher * when this has expired we print a warning message and try another strategy.
461e689cf4aSJeff Kirsher * This "other" strategy is to force the interface into various speed/duplex
462e689cf4aSJeff Kirsher * configurations and we stop when we see a link-up condition before the
463e689cf4aSJeff Kirsher * maximum number of "peek" ticks have occurred.
464e689cf4aSJeff Kirsher *
465e689cf4aSJeff Kirsher * Once a valid link status has been detected we configure the BigMAC and
466e689cf4aSJeff Kirsher * the rest of the Happy Meal to speak the most efficient protocol we could
467e689cf4aSJeff Kirsher * get a clean link for. The priority for link configurations, highest first
468e689cf4aSJeff Kirsher * is:
469e689cf4aSJeff Kirsher * 100 Base-T Full Duplex
470e689cf4aSJeff Kirsher * 100 Base-T Half Duplex
471e689cf4aSJeff Kirsher * 10 Base-T Full Duplex
472e689cf4aSJeff Kirsher * 10 Base-T Half Duplex
473e689cf4aSJeff Kirsher *
474e689cf4aSJeff Kirsher * We start a new timer now, after a successful auto negotiation status has
475e689cf4aSJeff Kirsher * been detected. This timer just waits for the link-up bit to get set in
476e689cf4aSJeff Kirsher * the BMCR of the DP83840. When this occurs we print a kernel log message
477e689cf4aSJeff Kirsher * describing the link type in use and the fact that it is up.
478e689cf4aSJeff Kirsher *
479e689cf4aSJeff Kirsher * If a fatal error of some sort is signalled and detected in the interrupt
480e689cf4aSJeff Kirsher * service routine, and the chip is reset, or the link is ifconfig'd down
481e689cf4aSJeff Kirsher * and then back up, this entire process repeats itself all over again.
482e689cf4aSJeff Kirsher */
try_next_permutation(struct happy_meal * hp,void __iomem * tregs)483e689cf4aSJeff Kirsher static int try_next_permutation(struct happy_meal *hp, void __iomem *tregs)
484e689cf4aSJeff Kirsher {
485e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
486e689cf4aSJeff Kirsher
487e689cf4aSJeff Kirsher /* Downgrade from full to half duplex. Only possible
488e689cf4aSJeff Kirsher * via ethtool.
489e689cf4aSJeff Kirsher */
490e689cf4aSJeff Kirsher if (hp->sw_bmcr & BMCR_FULLDPLX) {
491e689cf4aSJeff Kirsher hp->sw_bmcr &= ~(BMCR_FULLDPLX);
492e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
493e689cf4aSJeff Kirsher return 0;
494e689cf4aSJeff Kirsher }
495e689cf4aSJeff Kirsher
496e689cf4aSJeff Kirsher /* Downgrade from 100 to 10. */
497e689cf4aSJeff Kirsher if (hp->sw_bmcr & BMCR_SPEED100) {
498e689cf4aSJeff Kirsher hp->sw_bmcr &= ~(BMCR_SPEED100);
499e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
500e689cf4aSJeff Kirsher return 0;
501e689cf4aSJeff Kirsher }
502e689cf4aSJeff Kirsher
503e689cf4aSJeff Kirsher /* We've tried everything. */
504e689cf4aSJeff Kirsher return -1;
505e689cf4aSJeff Kirsher }
506e689cf4aSJeff Kirsher
display_link_mode(struct happy_meal * hp,void __iomem * tregs)507e689cf4aSJeff Kirsher static void display_link_mode(struct happy_meal *hp, void __iomem *tregs)
508e689cf4aSJeff Kirsher {
509e689cf4aSJeff Kirsher hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
510b11e5f6aSNick Bowler
511b11e5f6aSNick Bowler netdev_info(hp->dev,
512b11e5f6aSNick Bowler "Link is up using %s transceiver at %dMb/s, %s Duplex.\n",
513b11e5f6aSNick Bowler hp->tcvr_type == external ? "external" : "internal",
514b11e5f6aSNick Bowler hp->sw_lpa & (LPA_100HALF | LPA_100FULL) ? 100 : 10,
515b11e5f6aSNick Bowler hp->sw_lpa & (LPA_100FULL | LPA_10FULL) ? "Full" : "Half");
516e689cf4aSJeff Kirsher }
517e689cf4aSJeff Kirsher
display_forced_link_mode(struct happy_meal * hp,void __iomem * tregs)518e689cf4aSJeff Kirsher static void display_forced_link_mode(struct happy_meal *hp, void __iomem *tregs)
519e689cf4aSJeff Kirsher {
520e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
521b11e5f6aSNick Bowler
522b11e5f6aSNick Bowler netdev_info(hp->dev,
523b11e5f6aSNick Bowler "Link has been forced up using %s transceiver at %dMb/s, %s Duplex.\n",
524b11e5f6aSNick Bowler hp->tcvr_type == external ? "external" : "internal",
525b11e5f6aSNick Bowler hp->sw_bmcr & BMCR_SPEED100 ? 100 : 10,
526b11e5f6aSNick Bowler hp->sw_bmcr & BMCR_FULLDPLX ? "Full" : "Half");
527e689cf4aSJeff Kirsher }
528e689cf4aSJeff Kirsher
set_happy_link_modes(struct happy_meal * hp,void __iomem * tregs)529e689cf4aSJeff Kirsher static int set_happy_link_modes(struct happy_meal *hp, void __iomem *tregs)
530e689cf4aSJeff Kirsher {
531e689cf4aSJeff Kirsher int full;
532e689cf4aSJeff Kirsher
533e689cf4aSJeff Kirsher /* All we care about is making sure the bigmac tx_cfg has a
534e689cf4aSJeff Kirsher * proper duplex setting.
535e689cf4aSJeff Kirsher */
536e689cf4aSJeff Kirsher if (hp->timer_state == arbwait) {
537e689cf4aSJeff Kirsher hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
538e689cf4aSJeff Kirsher if (!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
539e689cf4aSJeff Kirsher goto no_response;
540e689cf4aSJeff Kirsher if (hp->sw_lpa & LPA_100FULL)
541e689cf4aSJeff Kirsher full = 1;
542e689cf4aSJeff Kirsher else if (hp->sw_lpa & LPA_100HALF)
543e689cf4aSJeff Kirsher full = 0;
544e689cf4aSJeff Kirsher else if (hp->sw_lpa & LPA_10FULL)
545e689cf4aSJeff Kirsher full = 1;
546e689cf4aSJeff Kirsher else
547e689cf4aSJeff Kirsher full = 0;
548e689cf4aSJeff Kirsher } else {
549e689cf4aSJeff Kirsher /* Forcing a link mode. */
550e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
551e689cf4aSJeff Kirsher if (hp->sw_bmcr & BMCR_FULLDPLX)
552e689cf4aSJeff Kirsher full = 1;
553e689cf4aSJeff Kirsher else
554e689cf4aSJeff Kirsher full = 0;
555e689cf4aSJeff Kirsher }
556e689cf4aSJeff Kirsher
557e689cf4aSJeff Kirsher /* Before changing other bits in the tx_cfg register, and in
558e689cf4aSJeff Kirsher * general any of other the TX config registers too, you
559e689cf4aSJeff Kirsher * must:
560e689cf4aSJeff Kirsher * 1) Clear Enable
561e689cf4aSJeff Kirsher * 2) Poll with reads until that bit reads back as zero
562e689cf4aSJeff Kirsher * 3) Make TX configuration changes
563e689cf4aSJeff Kirsher * 4) Set Enable once more
564e689cf4aSJeff Kirsher */
565e689cf4aSJeff Kirsher hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
566e689cf4aSJeff Kirsher hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
567e689cf4aSJeff Kirsher ~(BIGMAC_TXCFG_ENABLE));
568e689cf4aSJeff Kirsher while (hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) & BIGMAC_TXCFG_ENABLE)
569e689cf4aSJeff Kirsher barrier();
570e689cf4aSJeff Kirsher if (full) {
571e689cf4aSJeff Kirsher hp->happy_flags |= HFLAG_FULL;
572e689cf4aSJeff Kirsher hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
573e689cf4aSJeff Kirsher hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
574e689cf4aSJeff Kirsher BIGMAC_TXCFG_FULLDPLX);
575e689cf4aSJeff Kirsher } else {
576e689cf4aSJeff Kirsher hp->happy_flags &= ~(HFLAG_FULL);
577e689cf4aSJeff Kirsher hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
578e689cf4aSJeff Kirsher hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
579e689cf4aSJeff Kirsher ~(BIGMAC_TXCFG_FULLDPLX));
580e689cf4aSJeff Kirsher }
581e689cf4aSJeff Kirsher hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
582e689cf4aSJeff Kirsher hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
583e689cf4aSJeff Kirsher BIGMAC_TXCFG_ENABLE);
584e689cf4aSJeff Kirsher return 0;
585e689cf4aSJeff Kirsher no_response:
586e689cf4aSJeff Kirsher return 1;
587e689cf4aSJeff Kirsher }
588e689cf4aSJeff Kirsher
is_lucent_phy(struct happy_meal * hp)589e689cf4aSJeff Kirsher static int is_lucent_phy(struct happy_meal *hp)
590e689cf4aSJeff Kirsher {
591e689cf4aSJeff Kirsher void __iomem *tregs = hp->tcvregs;
592e689cf4aSJeff Kirsher unsigned short mr2, mr3;
593e689cf4aSJeff Kirsher int ret = 0;
594e689cf4aSJeff Kirsher
595e689cf4aSJeff Kirsher mr2 = happy_meal_tcvr_read(hp, tregs, 2);
596e689cf4aSJeff Kirsher mr3 = happy_meal_tcvr_read(hp, tregs, 3);
597e689cf4aSJeff Kirsher if ((mr2 & 0xffff) == 0x0180 &&
598e689cf4aSJeff Kirsher ((mr3 & 0xffff) >> 10) == 0x1d)
599e689cf4aSJeff Kirsher ret = 1;
600e689cf4aSJeff Kirsher
601e689cf4aSJeff Kirsher return ret;
602e689cf4aSJeff Kirsher }
603e689cf4aSJeff Kirsher
60470b1b4b8SSean Anderson /* hp->happy_lock must be held */
60570b1b4b8SSean Anderson static void
happy_meal_begin_auto_negotiation(struct happy_meal * hp,void __iomem * tregs,const struct ethtool_link_ksettings * ep)60670b1b4b8SSean Anderson happy_meal_begin_auto_negotiation(struct happy_meal *hp,
60770b1b4b8SSean Anderson void __iomem *tregs,
60870b1b4b8SSean Anderson const struct ethtool_link_ksettings *ep)
60970b1b4b8SSean Anderson {
61070b1b4b8SSean Anderson int timeout;
61170b1b4b8SSean Anderson
61270b1b4b8SSean Anderson /* Read all of the registers we are interested in now. */
61370b1b4b8SSean Anderson hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
61470b1b4b8SSean Anderson hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
61570b1b4b8SSean Anderson hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
61670b1b4b8SSean Anderson hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
61770b1b4b8SSean Anderson
61870b1b4b8SSean Anderson /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
61970b1b4b8SSean Anderson
62070b1b4b8SSean Anderson hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
62170b1b4b8SSean Anderson if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
62270b1b4b8SSean Anderson /* Advertise everything we can support. */
62370b1b4b8SSean Anderson if (hp->sw_bmsr & BMSR_10HALF)
62470b1b4b8SSean Anderson hp->sw_advertise |= (ADVERTISE_10HALF);
62570b1b4b8SSean Anderson else
62670b1b4b8SSean Anderson hp->sw_advertise &= ~(ADVERTISE_10HALF);
62770b1b4b8SSean Anderson
62870b1b4b8SSean Anderson if (hp->sw_bmsr & BMSR_10FULL)
62970b1b4b8SSean Anderson hp->sw_advertise |= (ADVERTISE_10FULL);
63070b1b4b8SSean Anderson else
63170b1b4b8SSean Anderson hp->sw_advertise &= ~(ADVERTISE_10FULL);
63270b1b4b8SSean Anderson if (hp->sw_bmsr & BMSR_100HALF)
63370b1b4b8SSean Anderson hp->sw_advertise |= (ADVERTISE_100HALF);
63470b1b4b8SSean Anderson else
63570b1b4b8SSean Anderson hp->sw_advertise &= ~(ADVERTISE_100HALF);
63670b1b4b8SSean Anderson if (hp->sw_bmsr & BMSR_100FULL)
63770b1b4b8SSean Anderson hp->sw_advertise |= (ADVERTISE_100FULL);
63870b1b4b8SSean Anderson else
63970b1b4b8SSean Anderson hp->sw_advertise &= ~(ADVERTISE_100FULL);
64070b1b4b8SSean Anderson happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
64170b1b4b8SSean Anderson
64270b1b4b8SSean Anderson /* XXX Currently no Happy Meal cards I know off support 100BaseT4,
64370b1b4b8SSean Anderson * XXX and this is because the DP83840 does not support it, changes
64470b1b4b8SSean Anderson * XXX would need to be made to the tx/rx logic in the driver as well
64570b1b4b8SSean Anderson * XXX so I completely skip checking for it in the BMSR for now.
64670b1b4b8SSean Anderson */
64770b1b4b8SSean Anderson
64870b1b4b8SSean Anderson ASD("Advertising [ %s%s%s%s]\n",
64970b1b4b8SSean Anderson hp->sw_advertise & ADVERTISE_10HALF ? "10H " : "",
65070b1b4b8SSean Anderson hp->sw_advertise & ADVERTISE_10FULL ? "10F " : "",
65170b1b4b8SSean Anderson hp->sw_advertise & ADVERTISE_100HALF ? "100H " : "",
65270b1b4b8SSean Anderson hp->sw_advertise & ADVERTISE_100FULL ? "100F " : "");
65370b1b4b8SSean Anderson
65470b1b4b8SSean Anderson /* Enable Auto-Negotiation, this is usually on already... */
65570b1b4b8SSean Anderson hp->sw_bmcr |= BMCR_ANENABLE;
65670b1b4b8SSean Anderson happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
65770b1b4b8SSean Anderson
65870b1b4b8SSean Anderson /* Restart it to make sure it is going. */
65970b1b4b8SSean Anderson hp->sw_bmcr |= BMCR_ANRESTART;
66070b1b4b8SSean Anderson happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
66170b1b4b8SSean Anderson
66270b1b4b8SSean Anderson /* BMCR_ANRESTART self clears when the process has begun. */
66370b1b4b8SSean Anderson
66470b1b4b8SSean Anderson timeout = 64; /* More than enough. */
66570b1b4b8SSean Anderson while (--timeout) {
66670b1b4b8SSean Anderson hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
66770b1b4b8SSean Anderson if (!(hp->sw_bmcr & BMCR_ANRESTART))
66870b1b4b8SSean Anderson break; /* got it. */
66970b1b4b8SSean Anderson udelay(10);
67070b1b4b8SSean Anderson }
67170b1b4b8SSean Anderson if (!timeout) {
67270b1b4b8SSean Anderson netdev_err(hp->dev,
67370b1b4b8SSean Anderson "Happy Meal would not start auto negotiation BMCR=0x%04x\n",
67470b1b4b8SSean Anderson hp->sw_bmcr);
67570b1b4b8SSean Anderson netdev_notice(hp->dev,
67670b1b4b8SSean Anderson "Performing force link detection.\n");
67770b1b4b8SSean Anderson goto force_link;
67870b1b4b8SSean Anderson } else {
67970b1b4b8SSean Anderson hp->timer_state = arbwait;
68070b1b4b8SSean Anderson }
68170b1b4b8SSean Anderson } else {
68270b1b4b8SSean Anderson force_link:
68370b1b4b8SSean Anderson /* Force the link up, trying first a particular mode.
68470b1b4b8SSean Anderson * Either we are here at the request of ethtool or
68570b1b4b8SSean Anderson * because the Happy Meal would not start to autoneg.
68670b1b4b8SSean Anderson */
68770b1b4b8SSean Anderson
68870b1b4b8SSean Anderson /* Disable auto-negotiation in BMCR, enable the duplex and
68970b1b4b8SSean Anderson * speed setting, init the timer state machine, and fire it off.
69070b1b4b8SSean Anderson */
69170b1b4b8SSean Anderson if (!ep || ep->base.autoneg == AUTONEG_ENABLE) {
69270b1b4b8SSean Anderson hp->sw_bmcr = BMCR_SPEED100;
69370b1b4b8SSean Anderson } else {
69470b1b4b8SSean Anderson if (ep->base.speed == SPEED_100)
69570b1b4b8SSean Anderson hp->sw_bmcr = BMCR_SPEED100;
69670b1b4b8SSean Anderson else
69770b1b4b8SSean Anderson hp->sw_bmcr = 0;
69870b1b4b8SSean Anderson if (ep->base.duplex == DUPLEX_FULL)
69970b1b4b8SSean Anderson hp->sw_bmcr |= BMCR_FULLDPLX;
70070b1b4b8SSean Anderson }
70170b1b4b8SSean Anderson happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
70270b1b4b8SSean Anderson
70370b1b4b8SSean Anderson if (!is_lucent_phy(hp)) {
70470b1b4b8SSean Anderson /* OK, seems we need do disable the transceiver for the first
70570b1b4b8SSean Anderson * tick to make sure we get an accurate link state at the
70670b1b4b8SSean Anderson * second tick.
70770b1b4b8SSean Anderson */
70870b1b4b8SSean Anderson hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
70970b1b4b8SSean Anderson DP83840_CSCONFIG);
71070b1b4b8SSean Anderson hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
71170b1b4b8SSean Anderson happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
71270b1b4b8SSean Anderson hp->sw_csconfig);
71370b1b4b8SSean Anderson }
71470b1b4b8SSean Anderson hp->timer_state = ltrywait;
71570b1b4b8SSean Anderson }
71670b1b4b8SSean Anderson
71770b1b4b8SSean Anderson hp->timer_ticks = 0;
71870b1b4b8SSean Anderson hp->happy_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
71970b1b4b8SSean Anderson add_timer(&hp->happy_timer);
72070b1b4b8SSean Anderson }
72170b1b4b8SSean Anderson
happy_meal_timer(struct timer_list * t)7220822c5d9SKees Cook static void happy_meal_timer(struct timer_list *t)
723e689cf4aSJeff Kirsher {
7240822c5d9SKees Cook struct happy_meal *hp = from_timer(hp, t, happy_timer);
725e689cf4aSJeff Kirsher void __iomem *tregs = hp->tcvregs;
726e689cf4aSJeff Kirsher int restart_timer = 0;
727e689cf4aSJeff Kirsher
728e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
729e689cf4aSJeff Kirsher
730e689cf4aSJeff Kirsher hp->timer_ticks++;
731e689cf4aSJeff Kirsher switch(hp->timer_state) {
732e689cf4aSJeff Kirsher case arbwait:
733e689cf4aSJeff Kirsher /* Only allow for 5 ticks, thats 10 seconds and much too
734e689cf4aSJeff Kirsher * long to wait for arbitration to complete.
735e689cf4aSJeff Kirsher */
736e689cf4aSJeff Kirsher if (hp->timer_ticks >= 10) {
737e689cf4aSJeff Kirsher /* Enter force mode. */
738e689cf4aSJeff Kirsher do_force_mode:
739e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
7408acf878fSSean Anderson netdev_notice(hp->dev,
7418acf878fSSean Anderson "Auto-Negotiation unsuccessful, trying force link mode\n");
742e689cf4aSJeff Kirsher hp->sw_bmcr = BMCR_SPEED100;
743e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
744e689cf4aSJeff Kirsher
745e689cf4aSJeff Kirsher if (!is_lucent_phy(hp)) {
746e689cf4aSJeff Kirsher /* OK, seems we need do disable the transceiver for the first
747e689cf4aSJeff Kirsher * tick to make sure we get an accurate link state at the
748e689cf4aSJeff Kirsher * second tick.
749e689cf4aSJeff Kirsher */
750e689cf4aSJeff Kirsher hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
751e689cf4aSJeff Kirsher hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
752e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG, hp->sw_csconfig);
753e689cf4aSJeff Kirsher }
754e689cf4aSJeff Kirsher hp->timer_state = ltrywait;
755e689cf4aSJeff Kirsher hp->timer_ticks = 0;
756e689cf4aSJeff Kirsher restart_timer = 1;
757e689cf4aSJeff Kirsher } else {
758e689cf4aSJeff Kirsher /* Anything interesting happen? */
759e689cf4aSJeff Kirsher hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
760e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
761e689cf4aSJeff Kirsher int ret;
762e689cf4aSJeff Kirsher
763e689cf4aSJeff Kirsher /* Just what we've been waiting for... */
764e689cf4aSJeff Kirsher ret = set_happy_link_modes(hp, tregs);
765e689cf4aSJeff Kirsher if (ret) {
766e689cf4aSJeff Kirsher /* Ooops, something bad happened, go to force
767e689cf4aSJeff Kirsher * mode.
768e689cf4aSJeff Kirsher *
769e689cf4aSJeff Kirsher * XXX Broken hubs which don't support 802.3u
770e689cf4aSJeff Kirsher * XXX auto-negotiation make this happen as well.
771e689cf4aSJeff Kirsher */
772e689cf4aSJeff Kirsher goto do_force_mode;
773e689cf4aSJeff Kirsher }
774e689cf4aSJeff Kirsher
775e689cf4aSJeff Kirsher /* Success, at least so far, advance our state engine. */
776e689cf4aSJeff Kirsher hp->timer_state = lupwait;
777e689cf4aSJeff Kirsher restart_timer = 1;
778e689cf4aSJeff Kirsher } else {
779e689cf4aSJeff Kirsher restart_timer = 1;
780e689cf4aSJeff Kirsher }
781e689cf4aSJeff Kirsher }
782e689cf4aSJeff Kirsher break;
783e689cf4aSJeff Kirsher
784e689cf4aSJeff Kirsher case lupwait:
785e689cf4aSJeff Kirsher /* Auto negotiation was successful and we are awaiting a
786e689cf4aSJeff Kirsher * link up status. I have decided to let this timer run
787e689cf4aSJeff Kirsher * forever until some sort of error is signalled, reporting
788e689cf4aSJeff Kirsher * a message to the user at 10 second intervals.
789e689cf4aSJeff Kirsher */
790e689cf4aSJeff Kirsher hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
791e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_LSTATUS) {
792e689cf4aSJeff Kirsher /* Wheee, it's up, display the link mode in use and put
793e689cf4aSJeff Kirsher * the timer to sleep.
794e689cf4aSJeff Kirsher */
795e689cf4aSJeff Kirsher display_link_mode(hp, tregs);
796e689cf4aSJeff Kirsher hp->timer_state = asleep;
797e689cf4aSJeff Kirsher restart_timer = 0;
798e689cf4aSJeff Kirsher } else {
799e689cf4aSJeff Kirsher if (hp->timer_ticks >= 10) {
8008acf878fSSean Anderson netdev_notice(hp->dev,
8018acf878fSSean Anderson "Auto negotiation successful, link still not completely up.\n");
802e689cf4aSJeff Kirsher hp->timer_ticks = 0;
803e689cf4aSJeff Kirsher restart_timer = 1;
804e689cf4aSJeff Kirsher } else {
805e689cf4aSJeff Kirsher restart_timer = 1;
806e689cf4aSJeff Kirsher }
807e689cf4aSJeff Kirsher }
808e689cf4aSJeff Kirsher break;
809e689cf4aSJeff Kirsher
810e689cf4aSJeff Kirsher case ltrywait:
811e689cf4aSJeff Kirsher /* Making the timeout here too long can make it take
812e689cf4aSJeff Kirsher * annoyingly long to attempt all of the link mode
813e689cf4aSJeff Kirsher * permutations, but then again this is essentially
814e689cf4aSJeff Kirsher * error recovery code for the most part.
815e689cf4aSJeff Kirsher */
816e689cf4aSJeff Kirsher hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
817e689cf4aSJeff Kirsher hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
818e689cf4aSJeff Kirsher if (hp->timer_ticks == 1) {
819e689cf4aSJeff Kirsher if (!is_lucent_phy(hp)) {
820e689cf4aSJeff Kirsher /* Re-enable transceiver, we'll re-enable the transceiver next
821e689cf4aSJeff Kirsher * tick, then check link state on the following tick.
822e689cf4aSJeff Kirsher */
823e689cf4aSJeff Kirsher hp->sw_csconfig |= CSCONFIG_TCVDISAB;
824e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs,
825e689cf4aSJeff Kirsher DP83840_CSCONFIG, hp->sw_csconfig);
826e689cf4aSJeff Kirsher }
827e689cf4aSJeff Kirsher restart_timer = 1;
828e689cf4aSJeff Kirsher break;
829e689cf4aSJeff Kirsher }
830e689cf4aSJeff Kirsher if (hp->timer_ticks == 2) {
831e689cf4aSJeff Kirsher if (!is_lucent_phy(hp)) {
832e689cf4aSJeff Kirsher hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
833e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs,
834e689cf4aSJeff Kirsher DP83840_CSCONFIG, hp->sw_csconfig);
835e689cf4aSJeff Kirsher }
836e689cf4aSJeff Kirsher restart_timer = 1;
837e689cf4aSJeff Kirsher break;
838e689cf4aSJeff Kirsher }
839e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_LSTATUS) {
840e689cf4aSJeff Kirsher /* Force mode selection success. */
841e689cf4aSJeff Kirsher display_forced_link_mode(hp, tregs);
842e689cf4aSJeff Kirsher set_happy_link_modes(hp, tregs); /* XXX error? then what? */
843e689cf4aSJeff Kirsher hp->timer_state = asleep;
844e689cf4aSJeff Kirsher restart_timer = 0;
845e689cf4aSJeff Kirsher } else {
846e689cf4aSJeff Kirsher if (hp->timer_ticks >= 4) { /* 6 seconds or so... */
847e689cf4aSJeff Kirsher int ret;
848e689cf4aSJeff Kirsher
849e689cf4aSJeff Kirsher ret = try_next_permutation(hp, tregs);
850e689cf4aSJeff Kirsher if (ret == -1) {
851e689cf4aSJeff Kirsher /* Aieee, tried them all, reset the
852e689cf4aSJeff Kirsher * chip and try all over again.
853e689cf4aSJeff Kirsher */
854e689cf4aSJeff Kirsher
855e689cf4aSJeff Kirsher /* Let the user know... */
8568acf878fSSean Anderson netdev_notice(hp->dev,
8578acf878fSSean Anderson "Link down, cable problem?\n");
858e689cf4aSJeff Kirsher
85970b1b4b8SSean Anderson happy_meal_begin_auto_negotiation(hp, tregs, NULL);
860e689cf4aSJeff Kirsher goto out;
861e689cf4aSJeff Kirsher }
862e689cf4aSJeff Kirsher if (!is_lucent_phy(hp)) {
863e689cf4aSJeff Kirsher hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
864e689cf4aSJeff Kirsher DP83840_CSCONFIG);
865e689cf4aSJeff Kirsher hp->sw_csconfig |= CSCONFIG_TCVDISAB;
866e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs,
867e689cf4aSJeff Kirsher DP83840_CSCONFIG, hp->sw_csconfig);
868e689cf4aSJeff Kirsher }
869e689cf4aSJeff Kirsher hp->timer_ticks = 0;
870e689cf4aSJeff Kirsher restart_timer = 1;
871e689cf4aSJeff Kirsher } else {
872e689cf4aSJeff Kirsher restart_timer = 1;
873e689cf4aSJeff Kirsher }
874e689cf4aSJeff Kirsher }
875e689cf4aSJeff Kirsher break;
876e689cf4aSJeff Kirsher
877e689cf4aSJeff Kirsher case asleep:
878e689cf4aSJeff Kirsher default:
879e689cf4aSJeff Kirsher /* Can't happens.... */
8808acf878fSSean Anderson netdev_err(hp->dev,
8818acf878fSSean Anderson "Aieee, link timer is asleep but we got one anyways!\n");
882e689cf4aSJeff Kirsher restart_timer = 0;
883e689cf4aSJeff Kirsher hp->timer_ticks = 0;
884e689cf4aSJeff Kirsher hp->timer_state = asleep; /* foo on you */
885e689cf4aSJeff Kirsher break;
886e689cf4aSJeff Kirsher }
887e689cf4aSJeff Kirsher
888e689cf4aSJeff Kirsher if (restart_timer) {
889e689cf4aSJeff Kirsher hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
890e689cf4aSJeff Kirsher add_timer(&hp->happy_timer);
891e689cf4aSJeff Kirsher }
892e689cf4aSJeff Kirsher
893e689cf4aSJeff Kirsher out:
894e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
895e689cf4aSJeff Kirsher }
896e689cf4aSJeff Kirsher
897e689cf4aSJeff Kirsher #define TX_RESET_TRIES 32
898e689cf4aSJeff Kirsher #define RX_RESET_TRIES 32
899e689cf4aSJeff Kirsher
900e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_tx_reset(struct happy_meal * hp,void __iomem * bregs)901e689cf4aSJeff Kirsher static void happy_meal_tx_reset(struct happy_meal *hp, void __iomem *bregs)
902e689cf4aSJeff Kirsher {
903e689cf4aSJeff Kirsher int tries = TX_RESET_TRIES;
904e689cf4aSJeff Kirsher
90524cddbc3SSean Anderson HMD("reset...\n");
906e689cf4aSJeff Kirsher
907e689cf4aSJeff Kirsher /* Would you like to try our SMCC Delux? */
908e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_TXSWRESET, 0);
909e689cf4aSJeff Kirsher while ((hme_read32(hp, bregs + BMAC_TXSWRESET) & 1) && --tries)
910e689cf4aSJeff Kirsher udelay(20);
911e689cf4aSJeff Kirsher
912e689cf4aSJeff Kirsher /* Lettuce, tomato, buggy hardware (no extra charge)? */
913e689cf4aSJeff Kirsher if (!tries)
9148acf878fSSean Anderson netdev_err(hp->dev, "Transceiver BigMac ATTACK!");
915e689cf4aSJeff Kirsher
916e689cf4aSJeff Kirsher /* Take care. */
91703290907SSean Anderson HMD("done\n");
918e689cf4aSJeff Kirsher }
919e689cf4aSJeff Kirsher
920e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_rx_reset(struct happy_meal * hp,void __iomem * bregs)921e689cf4aSJeff Kirsher static void happy_meal_rx_reset(struct happy_meal *hp, void __iomem *bregs)
922e689cf4aSJeff Kirsher {
923e689cf4aSJeff Kirsher int tries = RX_RESET_TRIES;
924e689cf4aSJeff Kirsher
92524cddbc3SSean Anderson HMD("reset...\n");
926e689cf4aSJeff Kirsher
927e689cf4aSJeff Kirsher /* We have a special on GNU/Viking hardware bugs today. */
928e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RXSWRESET, 0);
929e689cf4aSJeff Kirsher while ((hme_read32(hp, bregs + BMAC_RXSWRESET) & 1) && --tries)
930e689cf4aSJeff Kirsher udelay(20);
931e689cf4aSJeff Kirsher
932e689cf4aSJeff Kirsher /* Will that be all? */
933e689cf4aSJeff Kirsher if (!tries)
93424cddbc3SSean Anderson netdev_err(hp->dev, "Receiver BigMac ATTACK!\n");
935e689cf4aSJeff Kirsher
936e689cf4aSJeff Kirsher /* Don't forget your vik_1137125_wa. Have a nice day. */
93703290907SSean Anderson HMD("done\n");
938e689cf4aSJeff Kirsher }
939e689cf4aSJeff Kirsher
940e689cf4aSJeff Kirsher #define STOP_TRIES 16
941e689cf4aSJeff Kirsher
942e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_stop(struct happy_meal * hp,void __iomem * gregs)943e689cf4aSJeff Kirsher static void happy_meal_stop(struct happy_meal *hp, void __iomem *gregs)
944e689cf4aSJeff Kirsher {
945e689cf4aSJeff Kirsher int tries = STOP_TRIES;
946e689cf4aSJeff Kirsher
94724cddbc3SSean Anderson HMD("reset...\n");
948e689cf4aSJeff Kirsher
949e689cf4aSJeff Kirsher /* We're consolidating our STB products, it's your lucky day. */
950e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_SWRESET, GREG_RESET_ALL);
951e689cf4aSJeff Kirsher while (hme_read32(hp, gregs + GREG_SWRESET) && --tries)
952e689cf4aSJeff Kirsher udelay(20);
953e689cf4aSJeff Kirsher
954e689cf4aSJeff Kirsher /* Come back next week when we are "Sun Microelectronics". */
955e689cf4aSJeff Kirsher if (!tries)
95624cddbc3SSean Anderson netdev_err(hp->dev, "Fry guys.\n");
957e689cf4aSJeff Kirsher
958e689cf4aSJeff Kirsher /* Remember: "Different name, same old buggy as shit hardware." */
95903290907SSean Anderson HMD("done\n");
960e689cf4aSJeff Kirsher }
961e689cf4aSJeff Kirsher
962e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_get_counters(struct happy_meal * hp,void __iomem * bregs)963e689cf4aSJeff Kirsher static void happy_meal_get_counters(struct happy_meal *hp, void __iomem *bregs)
964e689cf4aSJeff Kirsher {
965e807bcc7STobias Klauser struct net_device_stats *stats = &hp->dev->stats;
966e689cf4aSJeff Kirsher
967e689cf4aSJeff Kirsher stats->rx_crc_errors += hme_read32(hp, bregs + BMAC_RCRCECTR);
968e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RCRCECTR, 0);
969e689cf4aSJeff Kirsher
970e689cf4aSJeff Kirsher stats->rx_frame_errors += hme_read32(hp, bregs + BMAC_UNALECTR);
971e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_UNALECTR, 0);
972e689cf4aSJeff Kirsher
973e689cf4aSJeff Kirsher stats->rx_length_errors += hme_read32(hp, bregs + BMAC_GLECTR);
974e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_GLECTR, 0);
975e689cf4aSJeff Kirsher
976e689cf4aSJeff Kirsher stats->tx_aborted_errors += hme_read32(hp, bregs + BMAC_EXCTR);
977e689cf4aSJeff Kirsher
978e689cf4aSJeff Kirsher stats->collisions +=
979e689cf4aSJeff Kirsher (hme_read32(hp, bregs + BMAC_EXCTR) +
980e689cf4aSJeff Kirsher hme_read32(hp, bregs + BMAC_LTCTR));
981e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_EXCTR, 0);
982e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_LTCTR, 0);
983e689cf4aSJeff Kirsher }
984e689cf4aSJeff Kirsher
985e689cf4aSJeff Kirsher /* Only Sun can take such nice parts and fuck up the programming interface
986e689cf4aSJeff Kirsher * like this. Good job guys...
987e689cf4aSJeff Kirsher */
988e689cf4aSJeff Kirsher #define TCVR_RESET_TRIES 16 /* It should reset quickly */
989e689cf4aSJeff Kirsher #define TCVR_UNISOLATE_TRIES 32 /* Dis-isolation can take longer. */
990e689cf4aSJeff Kirsher
991e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_tcvr_reset(struct happy_meal * hp,void __iomem * tregs)992e689cf4aSJeff Kirsher static int happy_meal_tcvr_reset(struct happy_meal *hp, void __iomem *tregs)
993e689cf4aSJeff Kirsher {
994e689cf4aSJeff Kirsher u32 tconfig;
995e689cf4aSJeff Kirsher int result, tries = TCVR_RESET_TRIES;
996e689cf4aSJeff Kirsher
997e689cf4aSJeff Kirsher tconfig = hme_read32(hp, tregs + TCVR_CFG);
99824cddbc3SSean Anderson ASD("tcfg=%08x\n", tconfig);
999e689cf4aSJeff Kirsher if (hp->tcvr_type == external) {
1000e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG, tconfig & ~(TCV_CFG_PSELECT));
1001e689cf4aSJeff Kirsher hp->tcvr_type = internal;
1002e689cf4aSJeff Kirsher hp->paddr = TCV_PADDR_ITX;
1003e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR,
1004e689cf4aSJeff Kirsher (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
1005e689cf4aSJeff Kirsher result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1006e689cf4aSJeff Kirsher if (result == TCVR_FAILURE) {
100724cddbc3SSean Anderson ASD("phyread_fail\n");
1008e689cf4aSJeff Kirsher return -1;
1009e689cf4aSJeff Kirsher }
101024cddbc3SSean Anderson ASD("external: ISOLATE, phyread_ok, PSELECT\n");
1011e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
1012e689cf4aSJeff Kirsher hp->tcvr_type = external;
1013e689cf4aSJeff Kirsher hp->paddr = TCV_PADDR_ETX;
1014e689cf4aSJeff Kirsher } else {
1015e689cf4aSJeff Kirsher if (tconfig & TCV_CFG_MDIO1) {
1016e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG, (tconfig | TCV_CFG_PSELECT));
1017e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR,
1018e689cf4aSJeff Kirsher (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
1019e689cf4aSJeff Kirsher result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1020e689cf4aSJeff Kirsher if (result == TCVR_FAILURE) {
102103290907SSean Anderson ASD("phyread_fail>\n");
1022e689cf4aSJeff Kirsher return -1;
1023e689cf4aSJeff Kirsher }
102424cddbc3SSean Anderson ASD("internal: PSELECT, ISOLATE, phyread_ok, ~PSELECT\n");
1025e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG, (tconfig & ~(TCV_CFG_PSELECT)));
1026e689cf4aSJeff Kirsher hp->tcvr_type = internal;
1027e689cf4aSJeff Kirsher hp->paddr = TCV_PADDR_ITX;
1028e689cf4aSJeff Kirsher }
1029e689cf4aSJeff Kirsher }
1030e689cf4aSJeff Kirsher
103124cddbc3SSean Anderson ASD("BMCR_RESET...\n");
1032e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR, BMCR_RESET);
1033e689cf4aSJeff Kirsher
1034e689cf4aSJeff Kirsher while (--tries) {
1035e689cf4aSJeff Kirsher result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1036e689cf4aSJeff Kirsher if (result == TCVR_FAILURE)
1037e689cf4aSJeff Kirsher return -1;
1038e689cf4aSJeff Kirsher hp->sw_bmcr = result;
1039e689cf4aSJeff Kirsher if (!(result & BMCR_RESET))
1040e689cf4aSJeff Kirsher break;
1041e689cf4aSJeff Kirsher udelay(20);
1042e689cf4aSJeff Kirsher }
1043e689cf4aSJeff Kirsher if (!tries) {
104403290907SSean Anderson ASD("BMCR RESET FAILED!\n");
1045e689cf4aSJeff Kirsher return -1;
1046e689cf4aSJeff Kirsher }
104703290907SSean Anderson ASD("RESET_OK\n");
1048e689cf4aSJeff Kirsher
1049e689cf4aSJeff Kirsher /* Get fresh copies of the PHY registers. */
1050e689cf4aSJeff Kirsher hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1051e689cf4aSJeff Kirsher hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
1052e689cf4aSJeff Kirsher hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
1053e689cf4aSJeff Kirsher hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1054e689cf4aSJeff Kirsher
105524cddbc3SSean Anderson ASD("UNISOLATE...\n");
1056e689cf4aSJeff Kirsher hp->sw_bmcr &= ~(BMCR_ISOLATE);
1057e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
1058e689cf4aSJeff Kirsher
1059e689cf4aSJeff Kirsher tries = TCVR_UNISOLATE_TRIES;
1060e689cf4aSJeff Kirsher while (--tries) {
1061e689cf4aSJeff Kirsher result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
1062e689cf4aSJeff Kirsher if (result == TCVR_FAILURE)
1063e689cf4aSJeff Kirsher return -1;
1064e689cf4aSJeff Kirsher if (!(result & BMCR_ISOLATE))
1065e689cf4aSJeff Kirsher break;
1066e689cf4aSJeff Kirsher udelay(20);
1067e689cf4aSJeff Kirsher }
1068e689cf4aSJeff Kirsher if (!tries) {
106924cddbc3SSean Anderson ASD("UNISOLATE FAILED!\n");
1070e689cf4aSJeff Kirsher return -1;
1071e689cf4aSJeff Kirsher }
107203290907SSean Anderson ASD("SUCCESS and CSCONFIG_DFBYPASS\n");
1073e689cf4aSJeff Kirsher if (!is_lucent_phy(hp)) {
1074e689cf4aSJeff Kirsher result = happy_meal_tcvr_read(hp, tregs,
1075e689cf4aSJeff Kirsher DP83840_CSCONFIG);
1076e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs,
1077e689cf4aSJeff Kirsher DP83840_CSCONFIG, (result | CSCONFIG_DFBYPASS));
1078e689cf4aSJeff Kirsher }
1079e689cf4aSJeff Kirsher return 0;
1080e689cf4aSJeff Kirsher }
1081e689cf4aSJeff Kirsher
1082e689cf4aSJeff Kirsher /* Figure out whether we have an internal or external transceiver.
1083e689cf4aSJeff Kirsher *
1084e689cf4aSJeff Kirsher * hp->happy_lock must be held
1085e689cf4aSJeff Kirsher */
happy_meal_transceiver_check(struct happy_meal * hp,void __iomem * tregs)1086e689cf4aSJeff Kirsher static void happy_meal_transceiver_check(struct happy_meal *hp, void __iomem *tregs)
1087e689cf4aSJeff Kirsher {
1088e689cf4aSJeff Kirsher unsigned long tconfig = hme_read32(hp, tregs + TCVR_CFG);
1089e689cf4aSJeff Kirsher u32 reread = hme_read32(hp, tregs + TCVR_CFG);
1090e689cf4aSJeff Kirsher
10913427372dSSean Anderson ASD("tcfg=%08lx\n", tconfig);
1092e689cf4aSJeff Kirsher if (reread & TCV_CFG_MDIO1) {
1093e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
1094e689cf4aSJeff Kirsher hp->paddr = TCV_PADDR_ETX;
1095e689cf4aSJeff Kirsher hp->tcvr_type = external;
109624cddbc3SSean Anderson ASD("not polling, external\n");
1097e689cf4aSJeff Kirsher } else {
1098e689cf4aSJeff Kirsher if (reread & TCV_CFG_MDIO0) {
1099e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG,
1100e689cf4aSJeff Kirsher tconfig & ~(TCV_CFG_PSELECT));
1101e689cf4aSJeff Kirsher hp->paddr = TCV_PADDR_ITX;
1102e689cf4aSJeff Kirsher hp->tcvr_type = internal;
110324cddbc3SSean Anderson ASD("not polling, internal\n");
1104e689cf4aSJeff Kirsher } else {
11058acf878fSSean Anderson netdev_err(hp->dev,
11068acf878fSSean Anderson "Transceiver and a coke please.");
1107e689cf4aSJeff Kirsher hp->tcvr_type = none; /* Grrr... */
110824cddbc3SSean Anderson ASD("not polling, none\n");
1109e689cf4aSJeff Kirsher }
1110e689cf4aSJeff Kirsher }
1111e689cf4aSJeff Kirsher }
1112e689cf4aSJeff Kirsher
1113e689cf4aSJeff Kirsher /* The receive ring buffers are a bit tricky to get right. Here goes...
1114e689cf4aSJeff Kirsher *
1115e689cf4aSJeff Kirsher * The buffers we dma into must be 64 byte aligned. So we use a special
1116e689cf4aSJeff Kirsher * alloc_skb() routine for the happy meal to allocate 64 bytes more than
1117e689cf4aSJeff Kirsher * we really need.
1118e689cf4aSJeff Kirsher *
1119e689cf4aSJeff Kirsher * We use skb_reserve() to align the data block we get in the skb. We
1120e689cf4aSJeff Kirsher * also program the etxregs->cfg register to use an offset of 2. This
1121e689cf4aSJeff Kirsher * imperical constant plus the ethernet header size will always leave
1122e689cf4aSJeff Kirsher * us with a nicely aligned ip header once we pass things up to the
1123e689cf4aSJeff Kirsher * protocol layers.
1124e689cf4aSJeff Kirsher *
1125e689cf4aSJeff Kirsher * The numbers work out to:
1126e689cf4aSJeff Kirsher *
1127e689cf4aSJeff Kirsher * Max ethernet frame size 1518
1128e689cf4aSJeff Kirsher * Ethernet header size 14
1129e689cf4aSJeff Kirsher * Happy Meal base offset 2
1130e689cf4aSJeff Kirsher *
1131e689cf4aSJeff Kirsher * Say a skb data area is at 0xf001b010, and its size alloced is
1132e689cf4aSJeff Kirsher * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
1133e689cf4aSJeff Kirsher *
1134e689cf4aSJeff Kirsher * First our alloc_skb() routine aligns the data base to a 64 byte
1135e689cf4aSJeff Kirsher * boundary. We now have 0xf001b040 as our skb data address. We
1136e689cf4aSJeff Kirsher * plug this into the receive descriptor address.
1137e689cf4aSJeff Kirsher *
1138e689cf4aSJeff Kirsher * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
1139e689cf4aSJeff Kirsher * So now the data we will end up looking at starts at 0xf001b042. When
1140e689cf4aSJeff Kirsher * the packet arrives, we will check out the size received and subtract
1141e689cf4aSJeff Kirsher * this from the skb->length. Then we just pass the packet up to the
1142e689cf4aSJeff Kirsher * protocols as is, and allocate a new skb to replace this slot we have
1143e689cf4aSJeff Kirsher * just received from.
1144e689cf4aSJeff Kirsher *
1145e689cf4aSJeff Kirsher * The ethernet layer will strip the ether header from the front of the
1146e689cf4aSJeff Kirsher * skb we just sent to it, this leaves us with the ip header sitting
1147e689cf4aSJeff Kirsher * nicely aligned at 0xf001b050. Also, for tcp and udp packets the
1148e689cf4aSJeff Kirsher * Happy Meal has even checksummed the tcp/udp data for us. The 16
1149e689cf4aSJeff Kirsher * bit checksum is obtained from the low bits of the receive descriptor
1150e689cf4aSJeff Kirsher * flags, thus:
1151e689cf4aSJeff Kirsher *
1152e689cf4aSJeff Kirsher * skb->csum = rxd->rx_flags & 0xffff;
1153e689cf4aSJeff Kirsher * skb->ip_summed = CHECKSUM_COMPLETE;
1154e689cf4aSJeff Kirsher *
1155e689cf4aSJeff Kirsher * before sending off the skb to the protocols, and we are good as gold.
1156e689cf4aSJeff Kirsher */
happy_meal_clean_rings(struct happy_meal * hp)1157e689cf4aSJeff Kirsher static void happy_meal_clean_rings(struct happy_meal *hp)
1158e689cf4aSJeff Kirsher {
1159e689cf4aSJeff Kirsher int i;
1160e689cf4aSJeff Kirsher
1161e689cf4aSJeff Kirsher for (i = 0; i < RX_RING_SIZE; i++) {
1162e689cf4aSJeff Kirsher if (hp->rx_skbs[i] != NULL) {
1163e689cf4aSJeff Kirsher struct sk_buff *skb = hp->rx_skbs[i];
1164e689cf4aSJeff Kirsher struct happy_meal_rxd *rxd;
1165e689cf4aSJeff Kirsher u32 dma_addr;
1166e689cf4aSJeff Kirsher
1167e689cf4aSJeff Kirsher rxd = &hp->happy_block->happy_meal_rxd[i];
1168e689cf4aSJeff Kirsher dma_addr = hme_read_desc32(hp, &rxd->rx_addr);
1169e689cf4aSJeff Kirsher dma_unmap_single(hp->dma_dev, dma_addr,
1170e689cf4aSJeff Kirsher RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
1171e689cf4aSJeff Kirsher dev_kfree_skb_any(skb);
1172e689cf4aSJeff Kirsher hp->rx_skbs[i] = NULL;
1173e689cf4aSJeff Kirsher }
1174e689cf4aSJeff Kirsher }
1175e689cf4aSJeff Kirsher
1176e689cf4aSJeff Kirsher for (i = 0; i < TX_RING_SIZE; i++) {
1177e689cf4aSJeff Kirsher if (hp->tx_skbs[i] != NULL) {
1178e689cf4aSJeff Kirsher struct sk_buff *skb = hp->tx_skbs[i];
1179e689cf4aSJeff Kirsher struct happy_meal_txd *txd;
1180e689cf4aSJeff Kirsher u32 dma_addr;
1181e689cf4aSJeff Kirsher int frag;
1182e689cf4aSJeff Kirsher
1183e689cf4aSJeff Kirsher hp->tx_skbs[i] = NULL;
1184e689cf4aSJeff Kirsher
1185e689cf4aSJeff Kirsher for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1186e689cf4aSJeff Kirsher txd = &hp->happy_block->happy_meal_txd[i];
1187e689cf4aSJeff Kirsher dma_addr = hme_read_desc32(hp, &txd->tx_addr);
1188e689cf4aSJeff Kirsher if (!frag)
1189e689cf4aSJeff Kirsher dma_unmap_single(hp->dma_dev, dma_addr,
1190e689cf4aSJeff Kirsher (hme_read_desc32(hp, &txd->tx_flags)
1191e689cf4aSJeff Kirsher & TXFLAG_SIZE),
1192e689cf4aSJeff Kirsher DMA_TO_DEVICE);
1193e689cf4aSJeff Kirsher else
1194e689cf4aSJeff Kirsher dma_unmap_page(hp->dma_dev, dma_addr,
1195e689cf4aSJeff Kirsher (hme_read_desc32(hp, &txd->tx_flags)
1196e689cf4aSJeff Kirsher & TXFLAG_SIZE),
1197e689cf4aSJeff Kirsher DMA_TO_DEVICE);
1198e689cf4aSJeff Kirsher
1199e689cf4aSJeff Kirsher if (frag != skb_shinfo(skb)->nr_frags)
1200e689cf4aSJeff Kirsher i++;
1201e689cf4aSJeff Kirsher }
1202e689cf4aSJeff Kirsher
1203e689cf4aSJeff Kirsher dev_kfree_skb_any(skb);
1204e689cf4aSJeff Kirsher }
1205e689cf4aSJeff Kirsher }
1206e689cf4aSJeff Kirsher }
1207e689cf4aSJeff Kirsher
1208e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_init_rings(struct happy_meal * hp)1209e689cf4aSJeff Kirsher static void happy_meal_init_rings(struct happy_meal *hp)
1210e689cf4aSJeff Kirsher {
1211e689cf4aSJeff Kirsher struct hmeal_init_block *hb = hp->happy_block;
1212e689cf4aSJeff Kirsher int i;
1213e689cf4aSJeff Kirsher
121424cddbc3SSean Anderson HMD("counters to zero\n");
1215e689cf4aSJeff Kirsher hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;
1216e689cf4aSJeff Kirsher
1217e689cf4aSJeff Kirsher /* Free any skippy bufs left around in the rings. */
1218e689cf4aSJeff Kirsher happy_meal_clean_rings(hp);
1219e689cf4aSJeff Kirsher
1220e689cf4aSJeff Kirsher /* Now get new skippy bufs for the receive ring. */
122124cddbc3SSean Anderson HMD("init rxring\n");
1222e689cf4aSJeff Kirsher for (i = 0; i < RX_RING_SIZE; i++) {
1223e689cf4aSJeff Kirsher struct sk_buff *skb;
1224ec1f1276SDavid S. Miller u32 mapping;
1225e689cf4aSJeff Kirsher
1226e689cf4aSJeff Kirsher skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1227e689cf4aSJeff Kirsher if (!skb) {
1228e689cf4aSJeff Kirsher hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1229e689cf4aSJeff Kirsher continue;
1230e689cf4aSJeff Kirsher }
1231e689cf4aSJeff Kirsher hp->rx_skbs[i] = skb;
1232e689cf4aSJeff Kirsher
1233e689cf4aSJeff Kirsher /* Because we reserve afterwards. */
1234e689cf4aSJeff Kirsher skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
1235ec1f1276SDavid S. Miller mapping = dma_map_single(hp->dma_dev, skb->data, RX_BUF_ALLOC_SIZE,
1236ec1f1276SDavid S. Miller DMA_FROM_DEVICE);
1237ec1f1276SDavid S. Miller if (dma_mapping_error(hp->dma_dev, mapping)) {
1238ec1f1276SDavid S. Miller dev_kfree_skb_any(skb);
1239ec1f1276SDavid S. Miller hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
1240ec1f1276SDavid S. Miller continue;
1241ec1f1276SDavid S. Miller }
1242e689cf4aSJeff Kirsher hme_write_rxd(hp, &hb->happy_meal_rxd[i],
1243e689cf4aSJeff Kirsher (RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16)),
1244ec1f1276SDavid S. Miller mapping);
1245e689cf4aSJeff Kirsher skb_reserve(skb, RX_OFFSET);
1246e689cf4aSJeff Kirsher }
1247e689cf4aSJeff Kirsher
124824cddbc3SSean Anderson HMD("init txring\n");
1249e689cf4aSJeff Kirsher for (i = 0; i < TX_RING_SIZE; i++)
1250e689cf4aSJeff Kirsher hme_write_txd(hp, &hb->happy_meal_txd[i], 0, 0);
1251e689cf4aSJeff Kirsher
125203290907SSean Anderson HMD("done\n");
1253e689cf4aSJeff Kirsher }
1254e689cf4aSJeff Kirsher
1255e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_init(struct happy_meal * hp)1256e689cf4aSJeff Kirsher static int happy_meal_init(struct happy_meal *hp)
1257e689cf4aSJeff Kirsher {
1258a7639279SJakub Kicinski const unsigned char *e = &hp->dev->dev_addr[0];
1259e689cf4aSJeff Kirsher void __iomem *gregs = hp->gregs;
1260e689cf4aSJeff Kirsher void __iomem *etxregs = hp->etxregs;
1261e689cf4aSJeff Kirsher void __iomem *erxregs = hp->erxregs;
1262e689cf4aSJeff Kirsher void __iomem *bregs = hp->bigmacregs;
1263e689cf4aSJeff Kirsher void __iomem *tregs = hp->tcvregs;
12649408f3d3SDan Carpenter const char *bursts = "64";
1265e689cf4aSJeff Kirsher u32 regtmp, rxcfg;
1266e689cf4aSJeff Kirsher
1267e689cf4aSJeff Kirsher /* If auto-negotiation timer is running, kill it. */
1268e689cf4aSJeff Kirsher del_timer(&hp->happy_timer);
1269e689cf4aSJeff Kirsher
127024cddbc3SSean Anderson HMD("happy_flags[%08x]\n", hp->happy_flags);
1271e689cf4aSJeff Kirsher if (!(hp->happy_flags & HFLAG_INIT)) {
127224cddbc3SSean Anderson HMD("set HFLAG_INIT\n");
1273e689cf4aSJeff Kirsher hp->happy_flags |= HFLAG_INIT;
1274e689cf4aSJeff Kirsher happy_meal_get_counters(hp, bregs);
1275e689cf4aSJeff Kirsher }
1276e689cf4aSJeff Kirsher
1277e689cf4aSJeff Kirsher /* Stop transmitter and receiver. */
127824cddbc3SSean Anderson HMD("to happy_meal_stop\n");
1279e689cf4aSJeff Kirsher happy_meal_stop(hp, gregs);
1280e689cf4aSJeff Kirsher
1281e689cf4aSJeff Kirsher /* Alloc and reset the tx/rx descriptor chains. */
128224cddbc3SSean Anderson HMD("to happy_meal_init_rings\n");
1283e689cf4aSJeff Kirsher happy_meal_init_rings(hp);
1284e689cf4aSJeff Kirsher
1285e689cf4aSJeff Kirsher /* See if we can enable the MIF frame on this card to speak to the DP83840. */
1286e689cf4aSJeff Kirsher if (hp->happy_flags & HFLAG_FENABLE) {
128724cddbc3SSean Anderson HMD("use frame old[%08x]\n",
128803290907SSean Anderson hme_read32(hp, tregs + TCVR_CFG));
1289e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG,
1290e689cf4aSJeff Kirsher hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1291e689cf4aSJeff Kirsher } else {
129224cddbc3SSean Anderson HMD("use bitbang old[%08x]\n",
129303290907SSean Anderson hme_read32(hp, tregs + TCVR_CFG));
1294e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG,
1295e689cf4aSJeff Kirsher hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1296e689cf4aSJeff Kirsher }
1297e689cf4aSJeff Kirsher
1298e689cf4aSJeff Kirsher /* Check the state of the transceiver. */
129903290907SSean Anderson HMD("to happy_meal_transceiver_check\n");
1300e689cf4aSJeff Kirsher happy_meal_transceiver_check(hp, tregs);
1301e689cf4aSJeff Kirsher
1302e689cf4aSJeff Kirsher /* Put the Big Mac into a sane state. */
1303e689cf4aSJeff Kirsher switch(hp->tcvr_type) {
1304e689cf4aSJeff Kirsher case none:
1305e689cf4aSJeff Kirsher /* Cannot operate if we don't know the transceiver type! */
130624cddbc3SSean Anderson HMD("AAIEEE no transceiver type, EAGAIN\n");
1307e689cf4aSJeff Kirsher return -EAGAIN;
1308e689cf4aSJeff Kirsher
1309e689cf4aSJeff Kirsher case internal:
1310e689cf4aSJeff Kirsher /* Using the MII buffers. */
131124cddbc3SSean Anderson HMD("internal, using MII\n");
1312e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1313e689cf4aSJeff Kirsher break;
1314e689cf4aSJeff Kirsher
1315e689cf4aSJeff Kirsher case external:
1316e689cf4aSJeff Kirsher /* Not using the MII, disable it. */
131724cddbc3SSean Anderson HMD("external, disable MII\n");
1318e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1319e689cf4aSJeff Kirsher break;
1320e689cf4aSJeff Kirsher }
1321e689cf4aSJeff Kirsher
1322e689cf4aSJeff Kirsher if (happy_meal_tcvr_reset(hp, tregs))
1323e689cf4aSJeff Kirsher return -EAGAIN;
1324e689cf4aSJeff Kirsher
1325e689cf4aSJeff Kirsher /* Reset the Happy Meal Big Mac transceiver and the receiver. */
132624cddbc3SSean Anderson HMD("tx/rx reset\n");
1327e689cf4aSJeff Kirsher happy_meal_tx_reset(hp, bregs);
1328e689cf4aSJeff Kirsher happy_meal_rx_reset(hp, bregs);
1329e689cf4aSJeff Kirsher
1330e689cf4aSJeff Kirsher /* Set jam size and inter-packet gaps to reasonable defaults. */
1331e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_JSIZE, DEFAULT_JAMSIZE);
1332e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_IGAP1, DEFAULT_IPG1);
1333e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_IGAP2, DEFAULT_IPG2);
1334e689cf4aSJeff Kirsher
1335e689cf4aSJeff Kirsher /* Load up the MAC address and random seed. */
1336e689cf4aSJeff Kirsher
1337e689cf4aSJeff Kirsher /* The docs recommend to use the 10LSB of our MAC here. */
1338e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RSEED, ((e[5] | e[4]<<8)&0x3ff));
1339e689cf4aSJeff Kirsher
1340e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_MACADDR2, ((e[4] << 8) | e[5]));
1341e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_MACADDR1, ((e[2] << 8) | e[3]));
1342e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_MACADDR0, ((e[0] << 8) | e[1]));
1343e689cf4aSJeff Kirsher
1344e689cf4aSJeff Kirsher if ((hp->dev->flags & IFF_ALLMULTI) ||
1345e689cf4aSJeff Kirsher (netdev_mc_count(hp->dev) > 64)) {
1346e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
1347e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
1348e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
1349e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
1350e689cf4aSJeff Kirsher } else if ((hp->dev->flags & IFF_PROMISC) == 0) {
1351e689cf4aSJeff Kirsher u16 hash_table[4];
1352e689cf4aSJeff Kirsher struct netdev_hw_addr *ha;
1353e689cf4aSJeff Kirsher u32 crc;
1354e689cf4aSJeff Kirsher
1355e689cf4aSJeff Kirsher memset(hash_table, 0, sizeof(hash_table));
1356e689cf4aSJeff Kirsher netdev_for_each_mc_addr(ha, hp->dev) {
1357e689cf4aSJeff Kirsher crc = ether_crc_le(6, ha->addr);
1358e689cf4aSJeff Kirsher crc >>= 26;
1359e689cf4aSJeff Kirsher hash_table[crc >> 4] |= 1 << (crc & 0xf);
1360e689cf4aSJeff Kirsher }
1361e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
1362e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
1363e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
1364e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
1365e689cf4aSJeff Kirsher } else {
1366e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE3, 0);
1367e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE2, 0);
1368e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE1, 0);
1369e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE0, 0);
1370e689cf4aSJeff Kirsher }
1371e689cf4aSJeff Kirsher
1372e689cf4aSJeff Kirsher /* Set the RX and TX ring ptrs. */
137303290907SSean Anderson HMD("ring ptrs rxr[%08x] txr[%08x]\n",
1374e689cf4aSJeff Kirsher ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
137503290907SSean Anderson ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
1376e689cf4aSJeff Kirsher hme_write32(hp, erxregs + ERX_RING,
1377e689cf4aSJeff Kirsher ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
1378e689cf4aSJeff Kirsher hme_write32(hp, etxregs + ETX_RING,
1379e689cf4aSJeff Kirsher ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
1380e689cf4aSJeff Kirsher
1381e689cf4aSJeff Kirsher /* Parity issues in the ERX unit of some HME revisions can cause some
1382e689cf4aSJeff Kirsher * registers to not be written unless their parity is even. Detect such
1383e689cf4aSJeff Kirsher * lost writes and simply rewrite with a low bit set (which will be ignored
1384e689cf4aSJeff Kirsher * since the rxring needs to be 2K aligned).
1385e689cf4aSJeff Kirsher */
1386e689cf4aSJeff Kirsher if (hme_read32(hp, erxregs + ERX_RING) !=
1387e689cf4aSJeff Kirsher ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)))
1388e689cf4aSJeff Kirsher hme_write32(hp, erxregs + ERX_RING,
1389e689cf4aSJeff Kirsher ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0))
1390e689cf4aSJeff Kirsher | 0x4);
1391e689cf4aSJeff Kirsher
1392e689cf4aSJeff Kirsher /* Set the supported burst sizes. */
1393e689cf4aSJeff Kirsher #ifndef CONFIG_SPARC
1394e689cf4aSJeff Kirsher /* It is always PCI and can handle 64byte bursts. */
1395e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST64);
1396e689cf4aSJeff Kirsher #else
1397e689cf4aSJeff Kirsher if ((hp->happy_bursts & DMA_BURST64) &&
1398e689cf4aSJeff Kirsher ((hp->happy_flags & HFLAG_PCI) != 0
1399e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
1400e689cf4aSJeff Kirsher || sbus_can_burst64()
1401e689cf4aSJeff Kirsher #endif
1402e689cf4aSJeff Kirsher || 0)) {
1403e689cf4aSJeff Kirsher u32 gcfg = GREG_CFG_BURST64;
1404e689cf4aSJeff Kirsher
1405e689cf4aSJeff Kirsher /* I have no idea if I should set the extended
1406e689cf4aSJeff Kirsher * transfer mode bit for Cheerio, so for now I
1407e689cf4aSJeff Kirsher * do not. -DaveM
1408e689cf4aSJeff Kirsher */
1409e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
1410e689cf4aSJeff Kirsher if ((hp->happy_flags & HFLAG_PCI) == 0) {
1411e689cf4aSJeff Kirsher struct platform_device *op = hp->happy_dev;
1412e689cf4aSJeff Kirsher if (sbus_can_dma_64bit()) {
1413e689cf4aSJeff Kirsher sbus_set_sbus64(&op->dev,
1414e689cf4aSJeff Kirsher hp->happy_bursts);
1415e689cf4aSJeff Kirsher gcfg |= GREG_CFG_64BIT;
1416e689cf4aSJeff Kirsher }
1417e689cf4aSJeff Kirsher }
1418e689cf4aSJeff Kirsher #endif
1419e689cf4aSJeff Kirsher
142024cddbc3SSean Anderson bursts = "64";
1421e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_CFG, gcfg);
1422e689cf4aSJeff Kirsher } else if (hp->happy_bursts & DMA_BURST32) {
142324cddbc3SSean Anderson bursts = "32";
1424e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST32);
1425e689cf4aSJeff Kirsher } else if (hp->happy_bursts & DMA_BURST16) {
142624cddbc3SSean Anderson bursts = "16";
1427e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST16);
1428e689cf4aSJeff Kirsher } else {
142924cddbc3SSean Anderson bursts = "XXX";
1430e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_CFG, 0);
1431e689cf4aSJeff Kirsher }
1432e689cf4aSJeff Kirsher #endif /* CONFIG_SPARC */
1433e689cf4aSJeff Kirsher
143424cddbc3SSean Anderson HMD("old[%08x] bursts<%s>\n",
143524cddbc3SSean Anderson hme_read32(hp, gregs + GREG_CFG), bursts);
143624cddbc3SSean Anderson
1437e689cf4aSJeff Kirsher /* Turn off interrupts we do not want to hear. */
1438e689cf4aSJeff Kirsher hme_write32(hp, gregs + GREG_IMASK,
1439e689cf4aSJeff Kirsher (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
1440e689cf4aSJeff Kirsher GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));
1441e689cf4aSJeff Kirsher
1442e689cf4aSJeff Kirsher /* Set the transmit ring buffer size. */
144324cddbc3SSean Anderson HMD("tx rsize=%d oreg[%08x]\n", (int)TX_RING_SIZE,
144403290907SSean Anderson hme_read32(hp, etxregs + ETX_RSIZE));
1445e689cf4aSJeff Kirsher hme_write32(hp, etxregs + ETX_RSIZE, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);
1446e689cf4aSJeff Kirsher
1447e689cf4aSJeff Kirsher /* Enable transmitter DVMA. */
144824cddbc3SSean Anderson HMD("tx dma enable old[%08x]\n", hme_read32(hp, etxregs + ETX_CFG));
1449e689cf4aSJeff Kirsher hme_write32(hp, etxregs + ETX_CFG,
1450e689cf4aSJeff Kirsher hme_read32(hp, etxregs + ETX_CFG) | ETX_CFG_DMAENABLE);
1451e689cf4aSJeff Kirsher
1452e689cf4aSJeff Kirsher /* This chip really rots, for the receiver sometimes when you
1453e689cf4aSJeff Kirsher * write to its control registers not all the bits get there
1454e689cf4aSJeff Kirsher * properly. I cannot think of a sane way to provide complete
1455e689cf4aSJeff Kirsher * coverage for this hardware bug yet.
1456e689cf4aSJeff Kirsher */
145703290907SSean Anderson HMD("erx regs bug old[%08x]\n",
145803290907SSean Anderson hme_read32(hp, erxregs + ERX_CFG));
1459e689cf4aSJeff Kirsher hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1460e689cf4aSJeff Kirsher regtmp = hme_read32(hp, erxregs + ERX_CFG);
1461e689cf4aSJeff Kirsher hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
1462e689cf4aSJeff Kirsher if (hme_read32(hp, erxregs + ERX_CFG) != ERX_CFG_DEFAULT(RX_OFFSET)) {
14638acf878fSSean Anderson netdev_err(hp->dev,
14648acf878fSSean Anderson "Eieee, rx config register gets greasy fries.\n");
14658acf878fSSean Anderson netdev_err(hp->dev,
14668acf878fSSean Anderson "Trying to set %08x, reread gives %08x\n",
1467e689cf4aSJeff Kirsher ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
1468e689cf4aSJeff Kirsher /* XXX Should return failure here... */
1469e689cf4aSJeff Kirsher }
1470e689cf4aSJeff Kirsher
1471e689cf4aSJeff Kirsher /* Enable Big Mac hash table filter. */
147224cddbc3SSean Anderson HMD("enable hash rx_cfg_old[%08x]\n",
147303290907SSean Anderson hme_read32(hp, bregs + BMAC_RXCFG));
1474e689cf4aSJeff Kirsher rxcfg = BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_REJME;
1475e689cf4aSJeff Kirsher if (hp->dev->flags & IFF_PROMISC)
1476e689cf4aSJeff Kirsher rxcfg |= BIGMAC_RXCFG_PMISC;
1477e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RXCFG, rxcfg);
1478e689cf4aSJeff Kirsher
1479e689cf4aSJeff Kirsher /* Let the bits settle in the chip. */
1480e689cf4aSJeff Kirsher udelay(10);
1481e689cf4aSJeff Kirsher
1482e689cf4aSJeff Kirsher /* Ok, configure the Big Mac transmitter. */
148324cddbc3SSean Anderson HMD("BIGMAC init\n");
1484e689cf4aSJeff Kirsher regtmp = 0;
1485e689cf4aSJeff Kirsher if (hp->happy_flags & HFLAG_FULL)
1486e689cf4aSJeff Kirsher regtmp |= BIGMAC_TXCFG_FULLDPLX;
1487e689cf4aSJeff Kirsher
1488e689cf4aSJeff Kirsher /* Don't turn on the "don't give up" bit for now. It could cause hme
1489e689cf4aSJeff Kirsher * to deadlock with the PHY if a Jabber occurs.
1490e689cf4aSJeff Kirsher */
1491e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_TXCFG, regtmp /*| BIGMAC_TXCFG_DGIVEUP*/);
1492e689cf4aSJeff Kirsher
1493e689cf4aSJeff Kirsher /* Give up after 16 TX attempts. */
1494e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_ALIMIT, 16);
1495e689cf4aSJeff Kirsher
1496e689cf4aSJeff Kirsher /* Enable the output drivers no matter what. */
1497e689cf4aSJeff Kirsher regtmp = BIGMAC_XCFG_ODENABLE;
1498e689cf4aSJeff Kirsher
1499e689cf4aSJeff Kirsher /* If card can do lance mode, enable it. */
1500e689cf4aSJeff Kirsher if (hp->happy_flags & HFLAG_LANCE)
1501e689cf4aSJeff Kirsher regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;
1502e689cf4aSJeff Kirsher
1503e689cf4aSJeff Kirsher /* Disable the MII buffers if using external transceiver. */
1504e689cf4aSJeff Kirsher if (hp->tcvr_type == external)
1505e689cf4aSJeff Kirsher regtmp |= BIGMAC_XCFG_MIIDISAB;
1506e689cf4aSJeff Kirsher
150724cddbc3SSean Anderson HMD("XIF config old[%08x]\n", hme_read32(hp, bregs + BMAC_XIFCFG));
1508e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_XIFCFG, regtmp);
1509e689cf4aSJeff Kirsher
1510e689cf4aSJeff Kirsher /* Start things up. */
151103290907SSean Anderson HMD("tx old[%08x] and rx [%08x] ON!\n",
1512e689cf4aSJeff Kirsher hme_read32(hp, bregs + BMAC_TXCFG),
151303290907SSean Anderson hme_read32(hp, bregs + BMAC_RXCFG));
1514e689cf4aSJeff Kirsher
1515e689cf4aSJeff Kirsher /* Set larger TX/RX size to allow for 802.1q */
1516e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_TXMAX, ETH_FRAME_LEN + 8);
1517e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RXMAX, ETH_FRAME_LEN + 8);
1518e689cf4aSJeff Kirsher
1519e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_TXCFG,
1520e689cf4aSJeff Kirsher hme_read32(hp, bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE);
1521e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RXCFG,
1522e689cf4aSJeff Kirsher hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE);
1523e689cf4aSJeff Kirsher
1524e689cf4aSJeff Kirsher /* Get the autonegotiation started, and the watch timer ticking. */
1525e689cf4aSJeff Kirsher happy_meal_begin_auto_negotiation(hp, tregs, NULL);
1526e689cf4aSJeff Kirsher
1527e689cf4aSJeff Kirsher /* Success. */
1528e689cf4aSJeff Kirsher return 0;
1529e689cf4aSJeff Kirsher }
1530e689cf4aSJeff Kirsher
1531e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_set_initial_advertisement(struct happy_meal * hp)1532e689cf4aSJeff Kirsher static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
1533e689cf4aSJeff Kirsher {
1534e689cf4aSJeff Kirsher void __iomem *tregs = hp->tcvregs;
1535e689cf4aSJeff Kirsher void __iomem *bregs = hp->bigmacregs;
1536e689cf4aSJeff Kirsher void __iomem *gregs = hp->gregs;
1537e689cf4aSJeff Kirsher
1538e689cf4aSJeff Kirsher happy_meal_stop(hp, gregs);
1539e689cf4aSJeff Kirsher if (hp->happy_flags & HFLAG_FENABLE)
1540e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG,
1541e689cf4aSJeff Kirsher hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
1542e689cf4aSJeff Kirsher else
1543e689cf4aSJeff Kirsher hme_write32(hp, tregs + TCVR_CFG,
1544e689cf4aSJeff Kirsher hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
1545e689cf4aSJeff Kirsher happy_meal_transceiver_check(hp, tregs);
1546e689cf4aSJeff Kirsher switch(hp->tcvr_type) {
1547e689cf4aSJeff Kirsher case none:
1548e689cf4aSJeff Kirsher return;
1549e689cf4aSJeff Kirsher case internal:
1550e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_XIFCFG, 0);
1551e689cf4aSJeff Kirsher break;
1552e689cf4aSJeff Kirsher case external:
1553e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
1554e689cf4aSJeff Kirsher break;
1555e689cf4aSJeff Kirsher }
1556e689cf4aSJeff Kirsher if (happy_meal_tcvr_reset(hp, tregs))
1557e689cf4aSJeff Kirsher return;
1558e689cf4aSJeff Kirsher
1559e689cf4aSJeff Kirsher /* Latch PHY registers as of now. */
1560e689cf4aSJeff Kirsher hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
1561e689cf4aSJeff Kirsher hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
1562e689cf4aSJeff Kirsher
1563e689cf4aSJeff Kirsher /* Advertise everything we can support. */
1564e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_10HALF)
1565e689cf4aSJeff Kirsher hp->sw_advertise |= (ADVERTISE_10HALF);
1566e689cf4aSJeff Kirsher else
1567e689cf4aSJeff Kirsher hp->sw_advertise &= ~(ADVERTISE_10HALF);
1568e689cf4aSJeff Kirsher
1569e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_10FULL)
1570e689cf4aSJeff Kirsher hp->sw_advertise |= (ADVERTISE_10FULL);
1571e689cf4aSJeff Kirsher else
1572e689cf4aSJeff Kirsher hp->sw_advertise &= ~(ADVERTISE_10FULL);
1573e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_100HALF)
1574e689cf4aSJeff Kirsher hp->sw_advertise |= (ADVERTISE_100HALF);
1575e689cf4aSJeff Kirsher else
1576e689cf4aSJeff Kirsher hp->sw_advertise &= ~(ADVERTISE_100HALF);
1577e689cf4aSJeff Kirsher if (hp->sw_bmsr & BMSR_100FULL)
1578e689cf4aSJeff Kirsher hp->sw_advertise |= (ADVERTISE_100FULL);
1579e689cf4aSJeff Kirsher else
1580e689cf4aSJeff Kirsher hp->sw_advertise &= ~(ADVERTISE_100FULL);
1581e689cf4aSJeff Kirsher
1582e689cf4aSJeff Kirsher /* Update the PHY advertisement register. */
1583e689cf4aSJeff Kirsher happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
1584e689cf4aSJeff Kirsher }
1585e689cf4aSJeff Kirsher
1586e689cf4aSJeff Kirsher /* Once status is latched (by happy_meal_interrupt) it is cleared by
1587e689cf4aSJeff Kirsher * the hardware, so we cannot re-read it and get a correct value.
1588e689cf4aSJeff Kirsher *
1589e689cf4aSJeff Kirsher * hp->happy_lock must be held
1590e689cf4aSJeff Kirsher */
happy_meal_is_not_so_happy(struct happy_meal * hp,u32 status)1591e689cf4aSJeff Kirsher static int happy_meal_is_not_so_happy(struct happy_meal *hp, u32 status)
1592e689cf4aSJeff Kirsher {
1593e689cf4aSJeff Kirsher int reset = 0;
1594e689cf4aSJeff Kirsher
1595e689cf4aSJeff Kirsher /* Only print messages for non-counter related interrupts. */
1596e689cf4aSJeff Kirsher if (status & (GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
1597e689cf4aSJeff Kirsher GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
1598e689cf4aSJeff Kirsher GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
1599e689cf4aSJeff Kirsher GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
1600e689cf4aSJeff Kirsher GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
1601e689cf4aSJeff Kirsher GREG_STAT_SLVPERR))
16028acf878fSSean Anderson netdev_err(hp->dev,
16038acf878fSSean Anderson "Error interrupt for happy meal, status = %08x\n",
16048acf878fSSean Anderson status);
1605e689cf4aSJeff Kirsher
1606e689cf4aSJeff Kirsher if (status & GREG_STAT_RFIFOVF) {
1607e689cf4aSJeff Kirsher /* Receive FIFO overflow is harmless and the hardware will take
1608e689cf4aSJeff Kirsher care of it, just some packets are lost. Who cares. */
16098acf878fSSean Anderson netdev_dbg(hp->dev, "Happy Meal receive FIFO overflow.\n");
1610e689cf4aSJeff Kirsher }
1611e689cf4aSJeff Kirsher
1612e689cf4aSJeff Kirsher if (status & GREG_STAT_STSTERR) {
1613e689cf4aSJeff Kirsher /* BigMAC SQE link test failed. */
16148acf878fSSean Anderson netdev_err(hp->dev, "Happy Meal BigMAC SQE test failed.\n");
1615e689cf4aSJeff Kirsher reset = 1;
1616e689cf4aSJeff Kirsher }
1617e689cf4aSJeff Kirsher
1618e689cf4aSJeff Kirsher if (status & GREG_STAT_TFIFO_UND) {
1619e689cf4aSJeff Kirsher /* Transmit FIFO underrun, again DMA error likely. */
16208acf878fSSean Anderson netdev_err(hp->dev,
16218acf878fSSean Anderson "Happy Meal transmitter FIFO underrun, DMA error.\n");
1622e689cf4aSJeff Kirsher reset = 1;
1623e689cf4aSJeff Kirsher }
1624e689cf4aSJeff Kirsher
1625e689cf4aSJeff Kirsher if (status & GREG_STAT_MAXPKTERR) {
1626e689cf4aSJeff Kirsher /* Driver error, tried to transmit something larger
1627e689cf4aSJeff Kirsher * than ethernet max mtu.
1628e689cf4aSJeff Kirsher */
16298acf878fSSean Anderson netdev_err(hp->dev, "Happy Meal MAX Packet size error.\n");
1630e689cf4aSJeff Kirsher reset = 1;
1631e689cf4aSJeff Kirsher }
1632e689cf4aSJeff Kirsher
1633e689cf4aSJeff Kirsher if (status & GREG_STAT_NORXD) {
1634e689cf4aSJeff Kirsher /* This is harmless, it just means the system is
1635e689cf4aSJeff Kirsher * quite loaded and the incoming packet rate was
1636e689cf4aSJeff Kirsher * faster than the interrupt handler could keep up
1637e689cf4aSJeff Kirsher * with.
1638e689cf4aSJeff Kirsher */
16398acf878fSSean Anderson netdev_info(hp->dev,
16408acf878fSSean Anderson "Happy Meal out of receive descriptors, packet dropped.\n");
1641e689cf4aSJeff Kirsher }
1642e689cf4aSJeff Kirsher
1643e689cf4aSJeff Kirsher if (status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
1644e689cf4aSJeff Kirsher /* All sorts of DMA receive errors. */
164524cddbc3SSean Anderson netdev_err(hp->dev, "Happy Meal rx DMA errors [ %s%s%s]\n",
164624cddbc3SSean Anderson status & GREG_STAT_RXERR ? "GenericError " : "",
164724cddbc3SSean Anderson status & GREG_STAT_RXPERR ? "ParityError " : "",
164824cddbc3SSean Anderson status & GREG_STAT_RXTERR ? "RxTagBotch " : "");
1649e689cf4aSJeff Kirsher reset = 1;
1650e689cf4aSJeff Kirsher }
1651e689cf4aSJeff Kirsher
1652e689cf4aSJeff Kirsher if (status & GREG_STAT_EOPERR) {
1653e689cf4aSJeff Kirsher /* Driver bug, didn't set EOP bit in tx descriptor given
1654e689cf4aSJeff Kirsher * to the happy meal.
1655e689cf4aSJeff Kirsher */
16568acf878fSSean Anderson netdev_err(hp->dev,
16578acf878fSSean Anderson "EOP not set in happy meal transmit descriptor!\n");
1658e689cf4aSJeff Kirsher reset = 1;
1659e689cf4aSJeff Kirsher }
1660e689cf4aSJeff Kirsher
1661e689cf4aSJeff Kirsher if (status & GREG_STAT_MIFIRQ) {
1662e689cf4aSJeff Kirsher /* MIF signalled an interrupt, were we polling it? */
16638acf878fSSean Anderson netdev_err(hp->dev, "Happy Meal MIF interrupt.\n");
1664e689cf4aSJeff Kirsher }
1665e689cf4aSJeff Kirsher
1666e689cf4aSJeff Kirsher if (status &
1667e689cf4aSJeff Kirsher (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
1668e689cf4aSJeff Kirsher /* All sorts of transmit DMA errors. */
166924cddbc3SSean Anderson netdev_err(hp->dev, "Happy Meal tx DMA errors [ %s%s%s%s]\n",
167024cddbc3SSean Anderson status & GREG_STAT_TXEACK ? "GenericError " : "",
167124cddbc3SSean Anderson status & GREG_STAT_TXLERR ? "LateError " : "",
167224cddbc3SSean Anderson status & GREG_STAT_TXPERR ? "ParityError " : "",
167324cddbc3SSean Anderson status & GREG_STAT_TXTERR ? "TagBotch " : "");
1674e689cf4aSJeff Kirsher reset = 1;
1675e689cf4aSJeff Kirsher }
1676e689cf4aSJeff Kirsher
1677e689cf4aSJeff Kirsher if (status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
1678e689cf4aSJeff Kirsher /* Bus or parity error when cpu accessed happy meal registers
1679e689cf4aSJeff Kirsher * or it's internal FIFO's. Should never see this.
1680e689cf4aSJeff Kirsher */
16818acf878fSSean Anderson netdev_err(hp->dev,
16828acf878fSSean Anderson "Happy Meal register access SBUS slave (%s) error.\n",
1683e689cf4aSJeff Kirsher (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
1684e689cf4aSJeff Kirsher reset = 1;
1685e689cf4aSJeff Kirsher }
1686e689cf4aSJeff Kirsher
1687e689cf4aSJeff Kirsher if (reset) {
16888acf878fSSean Anderson netdev_notice(hp->dev, "Resetting...\n");
1689e689cf4aSJeff Kirsher happy_meal_init(hp);
1690e689cf4aSJeff Kirsher return 1;
1691e689cf4aSJeff Kirsher }
1692e689cf4aSJeff Kirsher return 0;
1693e689cf4aSJeff Kirsher }
1694e689cf4aSJeff Kirsher
1695e689cf4aSJeff Kirsher /* hp->happy_lock must be held */
happy_meal_tx(struct happy_meal * hp)1696e689cf4aSJeff Kirsher static void happy_meal_tx(struct happy_meal *hp)
1697e689cf4aSJeff Kirsher {
1698e689cf4aSJeff Kirsher struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
1699e689cf4aSJeff Kirsher struct happy_meal_txd *this;
1700e689cf4aSJeff Kirsher struct net_device *dev = hp->dev;
1701e689cf4aSJeff Kirsher int elem;
1702e689cf4aSJeff Kirsher
1703e689cf4aSJeff Kirsher elem = hp->tx_old;
1704e689cf4aSJeff Kirsher while (elem != hp->tx_new) {
1705e689cf4aSJeff Kirsher struct sk_buff *skb;
1706e689cf4aSJeff Kirsher u32 flags, dma_addr, dma_len;
1707e689cf4aSJeff Kirsher int frag;
1708e689cf4aSJeff Kirsher
170926657c70SSean Anderson netdev_vdbg(hp->dev, "TX[%d]\n", elem);
1710e689cf4aSJeff Kirsher this = &txbase[elem];
1711e689cf4aSJeff Kirsher flags = hme_read_desc32(hp, &this->tx_flags);
1712e689cf4aSJeff Kirsher if (flags & TXFLAG_OWN)
1713e689cf4aSJeff Kirsher break;
1714e689cf4aSJeff Kirsher skb = hp->tx_skbs[elem];
1715e689cf4aSJeff Kirsher if (skb_shinfo(skb)->nr_frags) {
1716e689cf4aSJeff Kirsher int last;
1717e689cf4aSJeff Kirsher
1718e689cf4aSJeff Kirsher last = elem + skb_shinfo(skb)->nr_frags;
1719e689cf4aSJeff Kirsher last &= (TX_RING_SIZE - 1);
1720e689cf4aSJeff Kirsher flags = hme_read_desc32(hp, &txbase[last].tx_flags);
1721e689cf4aSJeff Kirsher if (flags & TXFLAG_OWN)
1722e689cf4aSJeff Kirsher break;
1723e689cf4aSJeff Kirsher }
1724e689cf4aSJeff Kirsher hp->tx_skbs[elem] = NULL;
1725e807bcc7STobias Klauser dev->stats.tx_bytes += skb->len;
1726e689cf4aSJeff Kirsher
1727e689cf4aSJeff Kirsher for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1728e689cf4aSJeff Kirsher dma_addr = hme_read_desc32(hp, &this->tx_addr);
1729e689cf4aSJeff Kirsher dma_len = hme_read_desc32(hp, &this->tx_flags);
1730e689cf4aSJeff Kirsher
1731e689cf4aSJeff Kirsher dma_len &= TXFLAG_SIZE;
1732e689cf4aSJeff Kirsher if (!frag)
1733e689cf4aSJeff Kirsher dma_unmap_single(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1734e689cf4aSJeff Kirsher else
1735e689cf4aSJeff Kirsher dma_unmap_page(hp->dma_dev, dma_addr, dma_len, DMA_TO_DEVICE);
1736e689cf4aSJeff Kirsher
1737e689cf4aSJeff Kirsher elem = NEXT_TX(elem);
1738e689cf4aSJeff Kirsher this = &txbase[elem];
1739e689cf4aSJeff Kirsher }
1740e689cf4aSJeff Kirsher
174198fcd70bSYang Wei dev_consume_skb_irq(skb);
1742e807bcc7STobias Klauser dev->stats.tx_packets++;
1743e689cf4aSJeff Kirsher }
1744e689cf4aSJeff Kirsher hp->tx_old = elem;
1745e689cf4aSJeff Kirsher
1746e689cf4aSJeff Kirsher if (netif_queue_stopped(dev) &&
1747e689cf4aSJeff Kirsher TX_BUFFS_AVAIL(hp) > (MAX_SKB_FRAGS + 1))
1748e689cf4aSJeff Kirsher netif_wake_queue(dev);
1749e689cf4aSJeff Kirsher }
1750e689cf4aSJeff Kirsher
1751e689cf4aSJeff Kirsher /* Originally I used to handle the allocation failure by just giving back just
1752e689cf4aSJeff Kirsher * that one ring buffer to the happy meal. Problem is that usually when that
1753e689cf4aSJeff Kirsher * condition is triggered, the happy meal expects you to do something reasonable
1754e689cf4aSJeff Kirsher * with all of the packets it has DMA'd in. So now I just drop the entire
1755e689cf4aSJeff Kirsher * ring when we cannot get a new skb and give them all back to the happy meal,
1756e689cf4aSJeff Kirsher * maybe things will be "happier" now.
1757e689cf4aSJeff Kirsher *
1758e689cf4aSJeff Kirsher * hp->happy_lock must be held
1759e689cf4aSJeff Kirsher */
happy_meal_rx(struct happy_meal * hp,struct net_device * dev)1760e689cf4aSJeff Kirsher static void happy_meal_rx(struct happy_meal *hp, struct net_device *dev)
1761e689cf4aSJeff Kirsher {
1762e689cf4aSJeff Kirsher struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
1763e689cf4aSJeff Kirsher struct happy_meal_rxd *this;
1764e689cf4aSJeff Kirsher int elem = hp->rx_new, drops = 0;
1765e689cf4aSJeff Kirsher u32 flags;
1766e689cf4aSJeff Kirsher
1767e689cf4aSJeff Kirsher this = &rxbase[elem];
1768e689cf4aSJeff Kirsher while (!((flags = hme_read_desc32(hp, &this->rx_flags)) & RXFLAG_OWN)) {
1769e689cf4aSJeff Kirsher struct sk_buff *skb;
1770e689cf4aSJeff Kirsher int len = flags >> 16;
1771e689cf4aSJeff Kirsher u16 csum = flags & RXFLAG_CSUM;
1772e689cf4aSJeff Kirsher u32 dma_addr = hme_read_desc32(hp, &this->rx_addr);
1773e689cf4aSJeff Kirsher
1774e689cf4aSJeff Kirsher /* Check for errors. */
1775e689cf4aSJeff Kirsher if ((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
177626657c70SSean Anderson netdev_vdbg(dev, "RX[%d ERR(%08x)]", elem, flags);
1777e807bcc7STobias Klauser dev->stats.rx_errors++;
1778e689cf4aSJeff Kirsher if (len < ETH_ZLEN)
1779e807bcc7STobias Klauser dev->stats.rx_length_errors++;
1780e689cf4aSJeff Kirsher if (len & (RXFLAG_OVERFLOW >> 16)) {
1781e807bcc7STobias Klauser dev->stats.rx_over_errors++;
1782e807bcc7STobias Klauser dev->stats.rx_fifo_errors++;
1783e689cf4aSJeff Kirsher }
1784e689cf4aSJeff Kirsher
1785e689cf4aSJeff Kirsher /* Return it to the Happy meal. */
1786e689cf4aSJeff Kirsher drop_it:
1787e807bcc7STobias Klauser dev->stats.rx_dropped++;
1788e689cf4aSJeff Kirsher hme_write_rxd(hp, this,
1789e689cf4aSJeff Kirsher (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
1790e689cf4aSJeff Kirsher dma_addr);
1791e689cf4aSJeff Kirsher goto next;
1792e689cf4aSJeff Kirsher }
1793e689cf4aSJeff Kirsher skb = hp->rx_skbs[elem];
1794e689cf4aSJeff Kirsher if (len > RX_COPY_THRESHOLD) {
1795e689cf4aSJeff Kirsher struct sk_buff *new_skb;
1796ec1f1276SDavid S. Miller u32 mapping;
1797e689cf4aSJeff Kirsher
1798e689cf4aSJeff Kirsher /* Now refill the entry, if we can. */
1799e689cf4aSJeff Kirsher new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
1800e689cf4aSJeff Kirsher if (new_skb == NULL) {
1801e689cf4aSJeff Kirsher drops++;
1802e689cf4aSJeff Kirsher goto drop_it;
1803e689cf4aSJeff Kirsher }
1804ec1f1276SDavid S. Miller skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
1805ec1f1276SDavid S. Miller mapping = dma_map_single(hp->dma_dev, new_skb->data,
1806ec1f1276SDavid S. Miller RX_BUF_ALLOC_SIZE,
1807ec1f1276SDavid S. Miller DMA_FROM_DEVICE);
1808ec1f1276SDavid S. Miller if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
1809ec1f1276SDavid S. Miller dev_kfree_skb_any(new_skb);
1810ec1f1276SDavid S. Miller drops++;
1811ec1f1276SDavid S. Miller goto drop_it;
1812ec1f1276SDavid S. Miller }
1813ec1f1276SDavid S. Miller
1814e689cf4aSJeff Kirsher dma_unmap_single(hp->dma_dev, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROM_DEVICE);
1815e689cf4aSJeff Kirsher hp->rx_skbs[elem] = new_skb;
1816e689cf4aSJeff Kirsher hme_write_rxd(hp, this,
1817e689cf4aSJeff Kirsher (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
1818ec1f1276SDavid S. Miller mapping);
1819e689cf4aSJeff Kirsher skb_reserve(new_skb, RX_OFFSET);
1820e689cf4aSJeff Kirsher
1821e689cf4aSJeff Kirsher /* Trim the original skb for the netif. */
1822e689cf4aSJeff Kirsher skb_trim(skb, len);
1823e689cf4aSJeff Kirsher } else {
1824dae2e9f4SPradeep A. Dalvi struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
1825e689cf4aSJeff Kirsher
1826e689cf4aSJeff Kirsher if (copy_skb == NULL) {
1827e689cf4aSJeff Kirsher drops++;
1828e689cf4aSJeff Kirsher goto drop_it;
1829e689cf4aSJeff Kirsher }
1830e689cf4aSJeff Kirsher
1831e689cf4aSJeff Kirsher skb_reserve(copy_skb, 2);
1832e689cf4aSJeff Kirsher skb_put(copy_skb, len);
1833878e2405SSean Anderson dma_sync_single_for_cpu(hp->dma_dev, dma_addr, len + 2, DMA_FROM_DEVICE);
1834e689cf4aSJeff Kirsher skb_copy_from_linear_data(skb, copy_skb->data, len);
1835878e2405SSean Anderson dma_sync_single_for_device(hp->dma_dev, dma_addr, len + 2, DMA_FROM_DEVICE);
1836e689cf4aSJeff Kirsher /* Reuse original ring buffer. */
1837e689cf4aSJeff Kirsher hme_write_rxd(hp, this,
1838e689cf4aSJeff Kirsher (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
1839e689cf4aSJeff Kirsher dma_addr);
1840e689cf4aSJeff Kirsher
1841e689cf4aSJeff Kirsher skb = copy_skb;
1842e689cf4aSJeff Kirsher }
1843e689cf4aSJeff Kirsher
1844e689cf4aSJeff Kirsher /* This card is _fucking_ hot... */
1845e689cf4aSJeff Kirsher skb->csum = csum_unfold(~(__force __sum16)htons(csum));
1846e689cf4aSJeff Kirsher skb->ip_summed = CHECKSUM_COMPLETE;
1847e689cf4aSJeff Kirsher
184826657c70SSean Anderson netdev_vdbg(dev, "RX[%d len=%d csum=%4x]", elem, len, csum);
1849e689cf4aSJeff Kirsher skb->protocol = eth_type_trans(skb, dev);
1850e689cf4aSJeff Kirsher netif_rx(skb);
1851e689cf4aSJeff Kirsher
1852e807bcc7STobias Klauser dev->stats.rx_packets++;
1853e807bcc7STobias Klauser dev->stats.rx_bytes += len;
1854e689cf4aSJeff Kirsher next:
1855e689cf4aSJeff Kirsher elem = NEXT_RX(elem);
1856e689cf4aSJeff Kirsher this = &rxbase[elem];
1857e689cf4aSJeff Kirsher }
1858e689cf4aSJeff Kirsher hp->rx_new = elem;
1859e689cf4aSJeff Kirsher if (drops)
18608acf878fSSean Anderson netdev_info(hp->dev, "Memory squeeze, deferring packet.\n");
1861e689cf4aSJeff Kirsher }
1862e689cf4aSJeff Kirsher
happy_meal_interrupt(int irq,void * dev_id)1863e689cf4aSJeff Kirsher static irqreturn_t happy_meal_interrupt(int irq, void *dev_id)
1864e689cf4aSJeff Kirsher {
1865e689cf4aSJeff Kirsher struct net_device *dev = dev_id;
1866e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
1867e689cf4aSJeff Kirsher u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
1868e689cf4aSJeff Kirsher
186924cddbc3SSean Anderson HMD("status=%08x\n", happy_status);
187027b9ea8fSSean Anderson if (!happy_status)
187127b9ea8fSSean Anderson return IRQ_NONE;
1872e689cf4aSJeff Kirsher
1873e689cf4aSJeff Kirsher spin_lock(&hp->happy_lock);
1874e689cf4aSJeff Kirsher
1875e689cf4aSJeff Kirsher if (happy_status & GREG_STAT_ERRORS) {
1876e689cf4aSJeff Kirsher if (happy_meal_is_not_so_happy(hp, /* un- */ happy_status))
1877e689cf4aSJeff Kirsher goto out;
1878e689cf4aSJeff Kirsher }
1879e689cf4aSJeff Kirsher
188024cddbc3SSean Anderson if (happy_status & GREG_STAT_TXALL)
1881e689cf4aSJeff Kirsher happy_meal_tx(hp);
1882e689cf4aSJeff Kirsher
188324cddbc3SSean Anderson if (happy_status & GREG_STAT_RXTOHOST)
1884e689cf4aSJeff Kirsher happy_meal_rx(hp, dev);
1885e689cf4aSJeff Kirsher
188603290907SSean Anderson HMD("done\n");
1887e689cf4aSJeff Kirsher out:
1888e689cf4aSJeff Kirsher spin_unlock(&hp->happy_lock);
1889e689cf4aSJeff Kirsher
1890e689cf4aSJeff Kirsher return IRQ_HANDLED;
1891e689cf4aSJeff Kirsher }
1892e689cf4aSJeff Kirsher
happy_meal_open(struct net_device * dev)1893e689cf4aSJeff Kirsher static int happy_meal_open(struct net_device *dev)
1894e689cf4aSJeff Kirsher {
1895e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
1896e689cf4aSJeff Kirsher int res;
1897e689cf4aSJeff Kirsher
18987deb1182SFrancois Romieu res = request_irq(hp->irq, happy_meal_interrupt, IRQF_SHARED,
18997deb1182SFrancois Romieu dev->name, dev);
19007deb1182SFrancois Romieu if (res) {
19018acf878fSSean Anderson netdev_err(dev, "Can't order irq %d to go.\n", hp->irq);
190227b9ea8fSSean Anderson return res;
1903e689cf4aSJeff Kirsher }
1904e689cf4aSJeff Kirsher
190503290907SSean Anderson HMD("to happy_meal_init\n");
1906e689cf4aSJeff Kirsher
1907e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
1908e689cf4aSJeff Kirsher res = happy_meal_init(hp);
1909e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
1910e689cf4aSJeff Kirsher
191127b9ea8fSSean Anderson if (res)
19127deb1182SFrancois Romieu free_irq(hp->irq, dev);
1913e689cf4aSJeff Kirsher return res;
1914e689cf4aSJeff Kirsher }
1915e689cf4aSJeff Kirsher
happy_meal_close(struct net_device * dev)1916e689cf4aSJeff Kirsher static int happy_meal_close(struct net_device *dev)
1917e689cf4aSJeff Kirsher {
1918e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
1919e689cf4aSJeff Kirsher
1920e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
1921e689cf4aSJeff Kirsher happy_meal_stop(hp, hp->gregs);
1922e689cf4aSJeff Kirsher happy_meal_clean_rings(hp);
1923e689cf4aSJeff Kirsher
1924e689cf4aSJeff Kirsher /* If auto-negotiation timer is running, kill it. */
1925e689cf4aSJeff Kirsher del_timer(&hp->happy_timer);
1926e689cf4aSJeff Kirsher
1927e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
1928e689cf4aSJeff Kirsher
19297deb1182SFrancois Romieu free_irq(hp->irq, dev);
1930e689cf4aSJeff Kirsher
1931e689cf4aSJeff Kirsher return 0;
1932e689cf4aSJeff Kirsher }
1933e689cf4aSJeff Kirsher
happy_meal_tx_timeout(struct net_device * dev,unsigned int txqueue)19340290bd29SMichael S. Tsirkin static void happy_meal_tx_timeout(struct net_device *dev, unsigned int txqueue)
1935e689cf4aSJeff Kirsher {
1936e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
1937e689cf4aSJeff Kirsher
19388acf878fSSean Anderson netdev_err(dev, "transmit timed out, resetting\n");
1939e689cf4aSJeff Kirsher tx_dump_log();
19408acf878fSSean Anderson netdev_err(dev, "Happy Status %08x TX[%08x:%08x]\n",
1941e689cf4aSJeff Kirsher hme_read32(hp, hp->gregs + GREG_STAT),
1942e689cf4aSJeff Kirsher hme_read32(hp, hp->etxregs + ETX_CFG),
1943e689cf4aSJeff Kirsher hme_read32(hp, hp->bigmacregs + BMAC_TXCFG));
1944e689cf4aSJeff Kirsher
1945e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
1946e689cf4aSJeff Kirsher happy_meal_init(hp);
1947e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
1948e689cf4aSJeff Kirsher
1949e689cf4aSJeff Kirsher netif_wake_queue(dev);
1950e689cf4aSJeff Kirsher }
1951e689cf4aSJeff Kirsher
unmap_partial_tx_skb(struct happy_meal * hp,u32 first_mapping,u32 first_len,u32 first_entry,u32 entry)1952ec1f1276SDavid S. Miller static void unmap_partial_tx_skb(struct happy_meal *hp, u32 first_mapping,
1953ec1f1276SDavid S. Miller u32 first_len, u32 first_entry, u32 entry)
1954ec1f1276SDavid S. Miller {
1955ec1f1276SDavid S. Miller struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
1956ec1f1276SDavid S. Miller
1957ec1f1276SDavid S. Miller dma_unmap_single(hp->dma_dev, first_mapping, first_len, DMA_TO_DEVICE);
1958ec1f1276SDavid S. Miller
1959ec1f1276SDavid S. Miller first_entry = NEXT_TX(first_entry);
1960ec1f1276SDavid S. Miller while (first_entry != entry) {
1961ec1f1276SDavid S. Miller struct happy_meal_txd *this = &txbase[first_entry];
1962ec1f1276SDavid S. Miller u32 addr, len;
1963ec1f1276SDavid S. Miller
1964ec1f1276SDavid S. Miller addr = hme_read_desc32(hp, &this->tx_addr);
1965ec1f1276SDavid S. Miller len = hme_read_desc32(hp, &this->tx_flags);
1966ec1f1276SDavid S. Miller len &= TXFLAG_SIZE;
1967ec1f1276SDavid S. Miller dma_unmap_page(hp->dma_dev, addr, len, DMA_TO_DEVICE);
1968ec1f1276SDavid S. Miller }
1969ec1f1276SDavid S. Miller }
1970ec1f1276SDavid S. Miller
happy_meal_start_xmit(struct sk_buff * skb,struct net_device * dev)1971e689cf4aSJeff Kirsher static netdev_tx_t happy_meal_start_xmit(struct sk_buff *skb,
1972e689cf4aSJeff Kirsher struct net_device *dev)
1973e689cf4aSJeff Kirsher {
1974e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
1975e689cf4aSJeff Kirsher int entry;
1976e689cf4aSJeff Kirsher u32 tx_flags;
1977e689cf4aSJeff Kirsher
1978e689cf4aSJeff Kirsher tx_flags = TXFLAG_OWN;
1979e689cf4aSJeff Kirsher if (skb->ip_summed == CHECKSUM_PARTIAL) {
1980e689cf4aSJeff Kirsher const u32 csum_start_off = skb_checksum_start_offset(skb);
1981e689cf4aSJeff Kirsher const u32 csum_stuff_off = csum_start_off + skb->csum_offset;
1982e689cf4aSJeff Kirsher
1983e689cf4aSJeff Kirsher tx_flags = (TXFLAG_OWN | TXFLAG_CSENABLE |
1984e689cf4aSJeff Kirsher ((csum_start_off << 14) & TXFLAG_CSBUFBEGIN) |
1985e689cf4aSJeff Kirsher ((csum_stuff_off << 20) & TXFLAG_CSLOCATION));
1986e689cf4aSJeff Kirsher }
1987e689cf4aSJeff Kirsher
1988e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
1989e689cf4aSJeff Kirsher
1990e689cf4aSJeff Kirsher if (TX_BUFFS_AVAIL(hp) <= (skb_shinfo(skb)->nr_frags + 1)) {
1991e689cf4aSJeff Kirsher netif_stop_queue(dev);
1992e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
19938acf878fSSean Anderson netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1994e689cf4aSJeff Kirsher return NETDEV_TX_BUSY;
1995e689cf4aSJeff Kirsher }
1996e689cf4aSJeff Kirsher
1997e689cf4aSJeff Kirsher entry = hp->tx_new;
199826657c70SSean Anderson netdev_vdbg(dev, "SX<l[%d]e[%d]>\n", skb->len, entry);
1999e689cf4aSJeff Kirsher hp->tx_skbs[entry] = skb;
2000e689cf4aSJeff Kirsher
2001e689cf4aSJeff Kirsher if (skb_shinfo(skb)->nr_frags == 0) {
2002e689cf4aSJeff Kirsher u32 mapping, len;
2003e689cf4aSJeff Kirsher
2004e689cf4aSJeff Kirsher len = skb->len;
2005e689cf4aSJeff Kirsher mapping = dma_map_single(hp->dma_dev, skb->data, len, DMA_TO_DEVICE);
2006ec1f1276SDavid S. Miller if (unlikely(dma_mapping_error(hp->dma_dev, mapping)))
2007ec1f1276SDavid S. Miller goto out_dma_error;
2008e689cf4aSJeff Kirsher tx_flags |= (TXFLAG_SOP | TXFLAG_EOP);
2009e689cf4aSJeff Kirsher hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2010e689cf4aSJeff Kirsher (tx_flags | (len & TXFLAG_SIZE)),
2011e689cf4aSJeff Kirsher mapping);
2012e689cf4aSJeff Kirsher entry = NEXT_TX(entry);
2013e689cf4aSJeff Kirsher } else {
2014e689cf4aSJeff Kirsher u32 first_len, first_mapping;
2015e689cf4aSJeff Kirsher int frag, first_entry = entry;
2016e689cf4aSJeff Kirsher
2017e689cf4aSJeff Kirsher /* We must give this initial chunk to the device last.
2018e689cf4aSJeff Kirsher * Otherwise we could race with the device.
2019e689cf4aSJeff Kirsher */
2020e689cf4aSJeff Kirsher first_len = skb_headlen(skb);
2021e689cf4aSJeff Kirsher first_mapping = dma_map_single(hp->dma_dev, skb->data, first_len,
2022e689cf4aSJeff Kirsher DMA_TO_DEVICE);
2023ec1f1276SDavid S. Miller if (unlikely(dma_mapping_error(hp->dma_dev, first_mapping)))
2024ec1f1276SDavid S. Miller goto out_dma_error;
2025e689cf4aSJeff Kirsher entry = NEXT_TX(entry);
2026e689cf4aSJeff Kirsher
2027e689cf4aSJeff Kirsher for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
20289e903e08SEric Dumazet const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
2029e689cf4aSJeff Kirsher u32 len, mapping, this_txflags;
2030e689cf4aSJeff Kirsher
20319e903e08SEric Dumazet len = skb_frag_size(this_frag);
20324bc68347SIan Campbell mapping = skb_frag_dma_map(hp->dma_dev, this_frag,
20334bc68347SIan Campbell 0, len, DMA_TO_DEVICE);
2034ec1f1276SDavid S. Miller if (unlikely(dma_mapping_error(hp->dma_dev, mapping))) {
2035ec1f1276SDavid S. Miller unmap_partial_tx_skb(hp, first_mapping, first_len,
2036ec1f1276SDavid S. Miller first_entry, entry);
2037ec1f1276SDavid S. Miller goto out_dma_error;
2038ec1f1276SDavid S. Miller }
2039e689cf4aSJeff Kirsher this_txflags = tx_flags;
2040e689cf4aSJeff Kirsher if (frag == skb_shinfo(skb)->nr_frags - 1)
2041e689cf4aSJeff Kirsher this_txflags |= TXFLAG_EOP;
2042e689cf4aSJeff Kirsher hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
2043e689cf4aSJeff Kirsher (this_txflags | (len & TXFLAG_SIZE)),
2044e689cf4aSJeff Kirsher mapping);
2045e689cf4aSJeff Kirsher entry = NEXT_TX(entry);
2046e689cf4aSJeff Kirsher }
2047e689cf4aSJeff Kirsher hme_write_txd(hp, &hp->happy_block->happy_meal_txd[first_entry],
2048e689cf4aSJeff Kirsher (tx_flags | TXFLAG_SOP | (first_len & TXFLAG_SIZE)),
2049e689cf4aSJeff Kirsher first_mapping);
2050e689cf4aSJeff Kirsher }
2051e689cf4aSJeff Kirsher
2052e689cf4aSJeff Kirsher hp->tx_new = entry;
2053e689cf4aSJeff Kirsher
2054e689cf4aSJeff Kirsher if (TX_BUFFS_AVAIL(hp) <= (MAX_SKB_FRAGS + 1))
2055e689cf4aSJeff Kirsher netif_stop_queue(dev);
2056e689cf4aSJeff Kirsher
2057e689cf4aSJeff Kirsher /* Get it going. */
2058e689cf4aSJeff Kirsher hme_write32(hp, hp->etxregs + ETX_PENDING, ETX_TP_DMAWAKEUP);
2059e689cf4aSJeff Kirsher
2060e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2061e689cf4aSJeff Kirsher
2062e689cf4aSJeff Kirsher tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
2063e689cf4aSJeff Kirsher return NETDEV_TX_OK;
2064ec1f1276SDavid S. Miller
2065ec1f1276SDavid S. Miller out_dma_error:
2066ec1f1276SDavid S. Miller hp->tx_skbs[hp->tx_new] = NULL;
2067ec1f1276SDavid S. Miller spin_unlock_irq(&hp->happy_lock);
2068ec1f1276SDavid S. Miller
2069ec1f1276SDavid S. Miller dev_kfree_skb_any(skb);
2070ec1f1276SDavid S. Miller dev->stats.tx_dropped++;
2071ec1f1276SDavid S. Miller return NETDEV_TX_OK;
2072e689cf4aSJeff Kirsher }
2073e689cf4aSJeff Kirsher
happy_meal_get_stats(struct net_device * dev)2074e689cf4aSJeff Kirsher static struct net_device_stats *happy_meal_get_stats(struct net_device *dev)
2075e689cf4aSJeff Kirsher {
2076e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2077e689cf4aSJeff Kirsher
2078e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
2079e689cf4aSJeff Kirsher happy_meal_get_counters(hp, hp->bigmacregs);
2080e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2081e689cf4aSJeff Kirsher
2082e807bcc7STobias Klauser return &dev->stats;
2083e689cf4aSJeff Kirsher }
2084e689cf4aSJeff Kirsher
happy_meal_set_multicast(struct net_device * dev)2085e689cf4aSJeff Kirsher static void happy_meal_set_multicast(struct net_device *dev)
2086e689cf4aSJeff Kirsher {
2087e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2088e689cf4aSJeff Kirsher void __iomem *bregs = hp->bigmacregs;
2089e689cf4aSJeff Kirsher struct netdev_hw_addr *ha;
2090e689cf4aSJeff Kirsher u32 crc;
2091e689cf4aSJeff Kirsher
2092e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
2093e689cf4aSJeff Kirsher
2094e689cf4aSJeff Kirsher if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
2095e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
2096e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
2097e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
2098e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
2099e689cf4aSJeff Kirsher } else if (dev->flags & IFF_PROMISC) {
2100e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_RXCFG,
2101e689cf4aSJeff Kirsher hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_PMISC);
2102e689cf4aSJeff Kirsher } else {
2103e689cf4aSJeff Kirsher u16 hash_table[4];
2104e689cf4aSJeff Kirsher
2105e689cf4aSJeff Kirsher memset(hash_table, 0, sizeof(hash_table));
2106e689cf4aSJeff Kirsher netdev_for_each_mc_addr(ha, dev) {
2107e689cf4aSJeff Kirsher crc = ether_crc_le(6, ha->addr);
2108e689cf4aSJeff Kirsher crc >>= 26;
2109e689cf4aSJeff Kirsher hash_table[crc >> 4] |= 1 << (crc & 0xf);
2110e689cf4aSJeff Kirsher }
2111e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
2112e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
2113e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
2114e689cf4aSJeff Kirsher hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
2115e689cf4aSJeff Kirsher }
2116e689cf4aSJeff Kirsher
2117e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2118e689cf4aSJeff Kirsher }
2119e689cf4aSJeff Kirsher
2120e689cf4aSJeff Kirsher /* Ethtool support... */
hme_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)2121e016cc64SPhilippe Reynes static int hme_get_link_ksettings(struct net_device *dev,
2122e016cc64SPhilippe Reynes struct ethtool_link_ksettings *cmd)
2123e689cf4aSJeff Kirsher {
2124e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2125e689cf4aSJeff Kirsher u32 speed;
2126e016cc64SPhilippe Reynes u32 supported;
2127e689cf4aSJeff Kirsher
2128e016cc64SPhilippe Reynes supported =
2129e689cf4aSJeff Kirsher (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2130e689cf4aSJeff Kirsher SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2131e689cf4aSJeff Kirsher SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
2132e689cf4aSJeff Kirsher
2133e689cf4aSJeff Kirsher /* XXX hardcoded stuff for now */
2134e016cc64SPhilippe Reynes cmd->base.port = PORT_TP; /* XXX no MII support */
2135e016cc64SPhilippe Reynes cmd->base.phy_address = 0; /* XXX fixed PHYAD */
2136e689cf4aSJeff Kirsher
2137e689cf4aSJeff Kirsher /* Record PHY settings. */
2138e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
2139e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2140e689cf4aSJeff Kirsher hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, MII_LPA);
2141e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2142e689cf4aSJeff Kirsher
2143e689cf4aSJeff Kirsher if (hp->sw_bmcr & BMCR_ANENABLE) {
2144e016cc64SPhilippe Reynes cmd->base.autoneg = AUTONEG_ENABLE;
2145e689cf4aSJeff Kirsher speed = ((hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
2146e689cf4aSJeff Kirsher SPEED_100 : SPEED_10);
2147e689cf4aSJeff Kirsher if (speed == SPEED_100)
2148e016cc64SPhilippe Reynes cmd->base.duplex =
2149e689cf4aSJeff Kirsher (hp->sw_lpa & (LPA_100FULL)) ?
2150e689cf4aSJeff Kirsher DUPLEX_FULL : DUPLEX_HALF;
2151e689cf4aSJeff Kirsher else
2152e016cc64SPhilippe Reynes cmd->base.duplex =
2153e689cf4aSJeff Kirsher (hp->sw_lpa & (LPA_10FULL)) ?
2154e689cf4aSJeff Kirsher DUPLEX_FULL : DUPLEX_HALF;
2155e689cf4aSJeff Kirsher } else {
2156e016cc64SPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE;
2157e689cf4aSJeff Kirsher speed = (hp->sw_bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10;
2158e016cc64SPhilippe Reynes cmd->base.duplex =
2159e689cf4aSJeff Kirsher (hp->sw_bmcr & BMCR_FULLDPLX) ?
2160e689cf4aSJeff Kirsher DUPLEX_FULL : DUPLEX_HALF;
2161e689cf4aSJeff Kirsher }
2162e016cc64SPhilippe Reynes cmd->base.speed = speed;
2163e016cc64SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2164e016cc64SPhilippe Reynes supported);
2165e016cc64SPhilippe Reynes
2166e689cf4aSJeff Kirsher return 0;
2167e689cf4aSJeff Kirsher }
2168e689cf4aSJeff Kirsher
hme_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)2169e016cc64SPhilippe Reynes static int hme_set_link_ksettings(struct net_device *dev,
2170e016cc64SPhilippe Reynes const struct ethtool_link_ksettings *cmd)
2171e689cf4aSJeff Kirsher {
2172e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2173e689cf4aSJeff Kirsher
2174e689cf4aSJeff Kirsher /* Verify the settings we care about. */
2175e016cc64SPhilippe Reynes if (cmd->base.autoneg != AUTONEG_ENABLE &&
2176e016cc64SPhilippe Reynes cmd->base.autoneg != AUTONEG_DISABLE)
2177e689cf4aSJeff Kirsher return -EINVAL;
2178e016cc64SPhilippe Reynes if (cmd->base.autoneg == AUTONEG_DISABLE &&
2179e016cc64SPhilippe Reynes ((cmd->base.speed != SPEED_100 &&
2180e016cc64SPhilippe Reynes cmd->base.speed != SPEED_10) ||
2181e016cc64SPhilippe Reynes (cmd->base.duplex != DUPLEX_HALF &&
2182e016cc64SPhilippe Reynes cmd->base.duplex != DUPLEX_FULL)))
2183e689cf4aSJeff Kirsher return -EINVAL;
2184e689cf4aSJeff Kirsher
2185e689cf4aSJeff Kirsher /* Ok, do it to it. */
2186e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
2187e689cf4aSJeff Kirsher del_timer(&hp->happy_timer);
2188e689cf4aSJeff Kirsher happy_meal_begin_auto_negotiation(hp, hp->tcvregs, cmd);
2189e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2190e689cf4aSJeff Kirsher
2191e689cf4aSJeff Kirsher return 0;
2192e689cf4aSJeff Kirsher }
2193e689cf4aSJeff Kirsher
hme_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)2194e689cf4aSJeff Kirsher static void hme_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2195e689cf4aSJeff Kirsher {
2196e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2197e689cf4aSJeff Kirsher
21986478c6e9SSean Anderson strscpy(info->driver, DRV_NAME, sizeof(info->driver));
2199e689cf4aSJeff Kirsher if (hp->happy_flags & HFLAG_PCI) {
2200e689cf4aSJeff Kirsher struct pci_dev *pdev = hp->happy_dev;
2201f029c781SWolfram Sang strscpy(info->bus_info, pci_name(pdev), sizeof(info->bus_info));
2202e689cf4aSJeff Kirsher }
2203e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2204e689cf4aSJeff Kirsher else {
2205e689cf4aSJeff Kirsher const struct linux_prom_registers *regs;
2206e689cf4aSJeff Kirsher struct platform_device *op = hp->happy_dev;
2207e689cf4aSJeff Kirsher regs = of_get_property(op->dev.of_node, "regs", NULL);
2208e689cf4aSJeff Kirsher if (regs)
220923020ab3SRick Jones snprintf(info->bus_info, sizeof(info->bus_info),
221023020ab3SRick Jones "SBUS:%d",
2211e689cf4aSJeff Kirsher regs->which_io);
2212e689cf4aSJeff Kirsher }
2213e689cf4aSJeff Kirsher #endif
2214e689cf4aSJeff Kirsher }
2215e689cf4aSJeff Kirsher
hme_get_link(struct net_device * dev)2216e689cf4aSJeff Kirsher static u32 hme_get_link(struct net_device *dev)
2217e689cf4aSJeff Kirsher {
2218e689cf4aSJeff Kirsher struct happy_meal *hp = netdev_priv(dev);
2219e689cf4aSJeff Kirsher
2220e689cf4aSJeff Kirsher spin_lock_irq(&hp->happy_lock);
2221e689cf4aSJeff Kirsher hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
2222e689cf4aSJeff Kirsher spin_unlock_irq(&hp->happy_lock);
2223e689cf4aSJeff Kirsher
2224e689cf4aSJeff Kirsher return hp->sw_bmsr & BMSR_LSTATUS;
2225e689cf4aSJeff Kirsher }
2226e689cf4aSJeff Kirsher
2227e689cf4aSJeff Kirsher static const struct ethtool_ops hme_ethtool_ops = {
2228e689cf4aSJeff Kirsher .get_drvinfo = hme_get_drvinfo,
2229e689cf4aSJeff Kirsher .get_link = hme_get_link,
2230e016cc64SPhilippe Reynes .get_link_ksettings = hme_get_link_ksettings,
2231e016cc64SPhilippe Reynes .set_link_ksettings = hme_set_link_ksettings,
2232e689cf4aSJeff Kirsher };
2233e689cf4aSJeff Kirsher
2234e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2235e689cf4aSJeff Kirsher /* Given a happy meal sbus device, find it's quattro parent.
2236e689cf4aSJeff Kirsher * If none exist, allocate and return a new one.
2237e689cf4aSJeff Kirsher *
2238e689cf4aSJeff Kirsher * Return NULL on failure.
2239e689cf4aSJeff Kirsher */
quattro_sbus_find(struct platform_device * child)2240f73d12bdSBill Pemberton static struct quattro *quattro_sbus_find(struct platform_device *child)
2241e689cf4aSJeff Kirsher {
2242e689cf4aSJeff Kirsher struct device *parent = child->dev.parent;
2243e689cf4aSJeff Kirsher struct platform_device *op;
2244e689cf4aSJeff Kirsher struct quattro *qp;
2245e689cf4aSJeff Kirsher
2246e689cf4aSJeff Kirsher op = to_platform_device(parent);
22478513fbd8SJingoo Han qp = platform_get_drvdata(op);
2248e689cf4aSJeff Kirsher if (qp)
2249e689cf4aSJeff Kirsher return qp;
2250e689cf4aSJeff Kirsher
2251d4ddeefaSSean Anderson qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2252d6f1e89bSSean Anderson if (!qp)
2253d6f1e89bSSean Anderson return NULL;
2254e689cf4aSJeff Kirsher
2255e689cf4aSJeff Kirsher qp->quattro_dev = child;
2256e689cf4aSJeff Kirsher qp->next = qfe_sbus_list;
2257e689cf4aSJeff Kirsher qfe_sbus_list = qp;
2258e689cf4aSJeff Kirsher
22598513fbd8SJingoo Han platform_set_drvdata(op, qp);
2260e689cf4aSJeff Kirsher return qp;
2261e689cf4aSJeff Kirsher }
2262e689cf4aSJeff Kirsher #endif /* CONFIG_SBUS */
2263e689cf4aSJeff Kirsher
2264e689cf4aSJeff Kirsher #ifdef CONFIG_PCI
quattro_pci_find(struct pci_dev * pdev)2265f73d12bdSBill Pemberton static struct quattro *quattro_pci_find(struct pci_dev *pdev)
2266e689cf4aSJeff Kirsher {
2267d6f1e89bSSean Anderson int i;
2268e689cf4aSJeff Kirsher struct pci_dev *bdev = pdev->bus->self;
2269e689cf4aSJeff Kirsher struct quattro *qp;
2270e689cf4aSJeff Kirsher
2271d6f1e89bSSean Anderson if (!bdev)
2272d6f1e89bSSean Anderson return ERR_PTR(-ENODEV);
2273d6f1e89bSSean Anderson
2274e689cf4aSJeff Kirsher for (qp = qfe_pci_list; qp != NULL; qp = qp->next) {
2275e689cf4aSJeff Kirsher struct pci_dev *qpdev = qp->quattro_dev;
2276e689cf4aSJeff Kirsher
2277e689cf4aSJeff Kirsher if (qpdev == bdev)
2278e689cf4aSJeff Kirsher return qp;
2279e689cf4aSJeff Kirsher }
2280d6f1e89bSSean Anderson
2281e689cf4aSJeff Kirsher qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
2282d6f1e89bSSean Anderson if (!qp)
2283d6f1e89bSSean Anderson return ERR_PTR(-ENOMEM);
2284e689cf4aSJeff Kirsher
2285e689cf4aSJeff Kirsher for (i = 0; i < 4; i++)
2286e689cf4aSJeff Kirsher qp->happy_meals[i] = NULL;
2287e689cf4aSJeff Kirsher
2288e689cf4aSJeff Kirsher qp->quattro_dev = bdev;
2289e689cf4aSJeff Kirsher qp->next = qfe_pci_list;
2290e689cf4aSJeff Kirsher qfe_pci_list = qp;
2291e689cf4aSJeff Kirsher
2292e689cf4aSJeff Kirsher /* No range tricks necessary on PCI. */
2293e689cf4aSJeff Kirsher qp->nranges = 0;
2294e689cf4aSJeff Kirsher return qp;
2295e689cf4aSJeff Kirsher }
2296e689cf4aSJeff Kirsher #endif /* CONFIG_PCI */
2297e689cf4aSJeff Kirsher
2298e689cf4aSJeff Kirsher static const struct net_device_ops hme_netdev_ops = {
2299e689cf4aSJeff Kirsher .ndo_open = happy_meal_open,
2300e689cf4aSJeff Kirsher .ndo_stop = happy_meal_close,
2301e689cf4aSJeff Kirsher .ndo_start_xmit = happy_meal_start_xmit,
2302e689cf4aSJeff Kirsher .ndo_tx_timeout = happy_meal_tx_timeout,
2303e689cf4aSJeff Kirsher .ndo_get_stats = happy_meal_get_stats,
2304afc4b13dSJiri Pirko .ndo_set_rx_mode = happy_meal_set_multicast,
2305e689cf4aSJeff Kirsher .ndo_set_mac_address = eth_mac_addr,
2306e689cf4aSJeff Kirsher .ndo_validate_addr = eth_validate_addr,
2307e689cf4aSJeff Kirsher };
2308e689cf4aSJeff Kirsher
2309273fb669SSean Anderson #ifdef CONFIG_PCI
is_quattro_p(struct pci_dev * pdev)2310273fb669SSean Anderson static int is_quattro_p(struct pci_dev *pdev)
2311273fb669SSean Anderson {
2312273fb669SSean Anderson struct pci_dev *busdev = pdev->bus->self;
2313273fb669SSean Anderson struct pci_dev *this_pdev;
2314273fb669SSean Anderson int n_hmes;
2315273fb669SSean Anderson
2316273fb669SSean Anderson if (!busdev || busdev->vendor != PCI_VENDOR_ID_DEC ||
2317273fb669SSean Anderson busdev->device != PCI_DEVICE_ID_DEC_21153)
2318273fb669SSean Anderson return 0;
2319273fb669SSean Anderson
2320273fb669SSean Anderson n_hmes = 0;
2321273fb669SSean Anderson list_for_each_entry(this_pdev, &pdev->bus->devices, bus_list) {
2322273fb669SSean Anderson if (this_pdev->vendor == PCI_VENDOR_ID_SUN &&
2323273fb669SSean Anderson this_pdev->device == PCI_DEVICE_ID_SUN_HAPPYMEAL)
2324273fb669SSean Anderson n_hmes++;
2325273fb669SSean Anderson }
2326273fb669SSean Anderson
2327273fb669SSean Anderson if (n_hmes != 4)
2328273fb669SSean Anderson return 0;
2329273fb669SSean Anderson
2330273fb669SSean Anderson return 1;
2331273fb669SSean Anderson }
2332273fb669SSean Anderson
2333273fb669SSean Anderson /* Fetch MAC address from vital product data of PCI ROM. */
find_eth_addr_in_vpd(void __iomem * rom_base,int len,int index,unsigned char * dev_addr)2334273fb669SSean Anderson static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, int index, unsigned char *dev_addr)
2335273fb669SSean Anderson {
2336273fb669SSean Anderson int this_offset;
2337273fb669SSean Anderson
2338273fb669SSean Anderson for (this_offset = 0x20; this_offset < len; this_offset++) {
2339273fb669SSean Anderson void __iomem *p = rom_base + this_offset;
2340273fb669SSean Anderson
2341273fb669SSean Anderson if (readb(p + 0) != 0x90 ||
2342273fb669SSean Anderson readb(p + 1) != 0x00 ||
2343273fb669SSean Anderson readb(p + 2) != 0x09 ||
2344273fb669SSean Anderson readb(p + 3) != 0x4e ||
2345273fb669SSean Anderson readb(p + 4) != 0x41 ||
2346273fb669SSean Anderson readb(p + 5) != 0x06)
2347273fb669SSean Anderson continue;
2348273fb669SSean Anderson
2349273fb669SSean Anderson this_offset += 6;
2350273fb669SSean Anderson p += 6;
2351273fb669SSean Anderson
2352273fb669SSean Anderson if (index == 0) {
2353d1f08819SSean Anderson for (int i = 0; i < 6; i++)
2354273fb669SSean Anderson dev_addr[i] = readb(p + i);
2355273fb669SSean Anderson return 1;
2356273fb669SSean Anderson }
2357273fb669SSean Anderson index--;
2358273fb669SSean Anderson }
2359273fb669SSean Anderson return 0;
2360273fb669SSean Anderson }
2361273fb669SSean Anderson
get_hme_mac_nonsparc(struct pci_dev * pdev,unsigned char * dev_addr)2362273fb669SSean Anderson static void __maybe_unused get_hme_mac_nonsparc(struct pci_dev *pdev,
2363273fb669SSean Anderson unsigned char *dev_addr)
2364273fb669SSean Anderson {
2365d1f08819SSean Anderson void __iomem *p;
2366273fb669SSean Anderson size_t size;
2367273fb669SSean Anderson
2368d1f08819SSean Anderson p = pci_map_rom(pdev, &size);
2369273fb669SSean Anderson if (p) {
2370273fb669SSean Anderson int index = 0;
2371273fb669SSean Anderson int found;
2372273fb669SSean Anderson
2373273fb669SSean Anderson if (is_quattro_p(pdev))
2374273fb669SSean Anderson index = PCI_SLOT(pdev->devfn);
2375273fb669SSean Anderson
2376273fb669SSean Anderson found = readb(p) == 0x55 &&
2377273fb669SSean Anderson readb(p + 1) == 0xaa &&
2378273fb669SSean Anderson find_eth_addr_in_vpd(p, (64 * 1024), index, dev_addr);
2379273fb669SSean Anderson pci_unmap_rom(pdev, p);
2380273fb669SSean Anderson if (found)
2381273fb669SSean Anderson return;
2382273fb669SSean Anderson }
2383273fb669SSean Anderson
2384273fb669SSean Anderson /* Sun MAC prefix then 3 random bytes. */
2385273fb669SSean Anderson dev_addr[0] = 0x08;
2386273fb669SSean Anderson dev_addr[1] = 0x00;
2387273fb669SSean Anderson dev_addr[2] = 0x20;
2388273fb669SSean Anderson get_random_bytes(&dev_addr[3], 3);
2389273fb669SSean Anderson }
2390d1f08819SSean Anderson #endif
2391273fb669SSean Anderson
happy_meal_addr_init(struct happy_meal * hp,struct device_node * dp,int qfe_slot)2392273fb669SSean Anderson static void happy_meal_addr_init(struct happy_meal *hp,
2393273fb669SSean Anderson struct device_node *dp, int qfe_slot)
2394273fb669SSean Anderson {
2395273fb669SSean Anderson int i;
2396273fb669SSean Anderson
2397273fb669SSean Anderson for (i = 0; i < 6; i++) {
2398273fb669SSean Anderson if (macaddr[i] != 0)
2399273fb669SSean Anderson break;
2400273fb669SSean Anderson }
2401273fb669SSean Anderson
2402273fb669SSean Anderson if (i < 6) { /* a mac address was given */
2403273fb669SSean Anderson u8 addr[ETH_ALEN];
2404273fb669SSean Anderson
2405273fb669SSean Anderson for (i = 0; i < 6; i++)
2406273fb669SSean Anderson addr[i] = macaddr[i];
2407273fb669SSean Anderson eth_hw_addr_set(hp->dev, addr);
2408273fb669SSean Anderson macaddr[5]++;
2409273fb669SSean Anderson } else {
2410273fb669SSean Anderson #ifdef CONFIG_SPARC
2411273fb669SSean Anderson const unsigned char *addr;
2412273fb669SSean Anderson int len;
2413273fb669SSean Anderson
2414273fb669SSean Anderson /* If user did not specify a MAC address specifically, use
2415273fb669SSean Anderson * the Quattro local-mac-address property...
2416273fb669SSean Anderson */
2417273fb669SSean Anderson if (qfe_slot != -1) {
2418273fb669SSean Anderson addr = of_get_property(dp, "local-mac-address", &len);
2419273fb669SSean Anderson if (addr && len == 6) {
2420273fb669SSean Anderson eth_hw_addr_set(hp->dev, addr);
2421273fb669SSean Anderson return;
2422273fb669SSean Anderson }
2423273fb669SSean Anderson }
2424273fb669SSean Anderson
2425273fb669SSean Anderson eth_hw_addr_set(hp->dev, idprom->id_ethaddr);
2426273fb669SSean Anderson #else
2427273fb669SSean Anderson u8 addr[ETH_ALEN];
2428273fb669SSean Anderson
2429273fb669SSean Anderson get_hme_mac_nonsparc(hp->happy_dev, addr);
2430273fb669SSean Anderson eth_hw_addr_set(hp->dev, addr);
2431273fb669SSean Anderson #endif
2432273fb669SSean Anderson }
2433273fb669SSean Anderson }
2434273fb669SSean Anderson
happy_meal_common_probe(struct happy_meal * hp,struct device_node * dp)2435ecdcd042SSean Anderson static int happy_meal_common_probe(struct happy_meal *hp,
2436ecdcd042SSean Anderson struct device_node *dp)
2437ecdcd042SSean Anderson {
2438ecdcd042SSean Anderson struct net_device *dev = hp->dev;
2439ecdcd042SSean Anderson int err;
2440ecdcd042SSean Anderson
2441ecdcd042SSean Anderson #ifdef CONFIG_SPARC
2442ecdcd042SSean Anderson hp->hm_revision = of_getintprop_default(dp, "hm-rev", hp->hm_revision);
2443ecdcd042SSean Anderson #endif
2444ecdcd042SSean Anderson
2445ecdcd042SSean Anderson /* Now enable the feature flags we can. */
2446ecdcd042SSean Anderson if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
2447ecdcd042SSean Anderson hp->happy_flags |= HFLAG_20_21;
2448ecdcd042SSean Anderson else if (hp->hm_revision != 0xa0)
2449ecdcd042SSean Anderson hp->happy_flags |= HFLAG_NOT_A0;
2450ecdcd042SSean Anderson
2451ecdcd042SSean Anderson hp->happy_block = dmam_alloc_coherent(hp->dma_dev, PAGE_SIZE,
2452ecdcd042SSean Anderson &hp->hblock_dvma, GFP_KERNEL);
2453ecdcd042SSean Anderson if (!hp->happy_block)
2454ecdcd042SSean Anderson return -ENOMEM;
2455ecdcd042SSean Anderson
2456ecdcd042SSean Anderson /* Force check of the link first time we are brought up. */
2457ecdcd042SSean Anderson hp->linkcheck = 0;
2458ecdcd042SSean Anderson
2459ecdcd042SSean Anderson /* Force timer state to 'asleep' with count of zero. */
2460ecdcd042SSean Anderson hp->timer_state = asleep;
2461ecdcd042SSean Anderson hp->timer_ticks = 0;
2462ecdcd042SSean Anderson
2463ecdcd042SSean Anderson timer_setup(&hp->happy_timer, happy_meal_timer, 0);
2464ecdcd042SSean Anderson
2465ecdcd042SSean Anderson dev->netdev_ops = &hme_netdev_ops;
2466ecdcd042SSean Anderson dev->watchdog_timeo = 5 * HZ;
2467ecdcd042SSean Anderson dev->ethtool_ops = &hme_ethtool_ops;
2468ecdcd042SSean Anderson
2469ecdcd042SSean Anderson /* Happy Meal can do it all... */
2470ecdcd042SSean Anderson dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2471ecdcd042SSean Anderson dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2472ecdcd042SSean Anderson
2473ecdcd042SSean Anderson
2474ecdcd042SSean Anderson /* Grrr, Happy Meal comes up by default not advertising
2475ecdcd042SSean Anderson * full duplex 100baseT capabilities, fix this.
2476ecdcd042SSean Anderson */
2477ecdcd042SSean Anderson spin_lock_irq(&hp->happy_lock);
2478ecdcd042SSean Anderson happy_meal_set_initial_advertisement(hp);
2479ecdcd042SSean Anderson spin_unlock_irq(&hp->happy_lock);
2480ecdcd042SSean Anderson
2481ecdcd042SSean Anderson err = devm_register_netdev(hp->dma_dev, dev);
2482ecdcd042SSean Anderson if (err)
2483ecdcd042SSean Anderson dev_err(hp->dma_dev, "Cannot register net device, aborting.\n");
2484ecdcd042SSean Anderson return err;
2485ecdcd042SSean Anderson }
2486ecdcd042SSean Anderson
2487e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
happy_meal_sbus_probe_one(struct platform_device * op,int is_qfe)2488f73d12bdSBill Pemberton static int happy_meal_sbus_probe_one(struct platform_device *op, int is_qfe)
2489e689cf4aSJeff Kirsher {
2490e689cf4aSJeff Kirsher struct device_node *dp = op->dev.of_node, *sbus_dp;
2491e689cf4aSJeff Kirsher struct quattro *qp = NULL;
2492e689cf4aSJeff Kirsher struct happy_meal *hp;
2493e689cf4aSJeff Kirsher struct net_device *dev;
2494273fb669SSean Anderson int qfe_slot = -1;
2495cc216e4bSSean Anderson int err;
2496e689cf4aSJeff Kirsher
2497e689cf4aSJeff Kirsher sbus_dp = op->dev.parent->of_node;
2498e689cf4aSJeff Kirsher
2499e689cf4aSJeff Kirsher /* We can match PCI devices too, do not accept those here. */
2500bf5849f1SRob Herring if (!of_node_name_eq(sbus_dp, "sbus") && !of_node_name_eq(sbus_dp, "sbi"))
2501cc216e4bSSean Anderson return -ENODEV;
2502e689cf4aSJeff Kirsher
2503e689cf4aSJeff Kirsher if (is_qfe) {
2504e689cf4aSJeff Kirsher qp = quattro_sbus_find(op);
2505e689cf4aSJeff Kirsher if (qp == NULL)
2506cc216e4bSSean Anderson return -ENODEV;
2507e689cf4aSJeff Kirsher for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2508e689cf4aSJeff Kirsher if (qp->happy_meals[qfe_slot] == NULL)
2509e689cf4aSJeff Kirsher break;
2510e689cf4aSJeff Kirsher if (qfe_slot == 4)
2511cc216e4bSSean Anderson return -ENODEV;
2512e689cf4aSJeff Kirsher }
2513e689cf4aSJeff Kirsher
2514cc216e4bSSean Anderson dev = devm_alloc_etherdev(&op->dev, sizeof(struct happy_meal));
2515e689cf4aSJeff Kirsher if (!dev)
2516cc216e4bSSean Anderson return -ENOMEM;
2517e689cf4aSJeff Kirsher SET_NETDEV_DEV(dev, &op->dev);
2518e689cf4aSJeff Kirsher
2519e689cf4aSJeff Kirsher hp = netdev_priv(dev);
2520273fb669SSean Anderson hp->dev = dev;
2521e689cf4aSJeff Kirsher hp->happy_dev = op;
2522e689cf4aSJeff Kirsher hp->dma_dev = &op->dev;
2523273fb669SSean Anderson happy_meal_addr_init(hp, dp, qfe_slot);
2524e689cf4aSJeff Kirsher
2525e689cf4aSJeff Kirsher spin_lock_init(&hp->happy_lock);
2526e689cf4aSJeff Kirsher
2527e689cf4aSJeff Kirsher if (qp != NULL) {
2528e689cf4aSJeff Kirsher hp->qfe_parent = qp;
2529e689cf4aSJeff Kirsher hp->qfe_ent = qfe_slot;
2530e689cf4aSJeff Kirsher qp->happy_meals[qfe_slot] = dev;
2531e689cf4aSJeff Kirsher }
2532e689cf4aSJeff Kirsher
2533cc216e4bSSean Anderson hp->gregs = devm_platform_ioremap_resource(op, 0);
2534cc216e4bSSean Anderson if (IS_ERR(hp->gregs)) {
25358acf878fSSean Anderson dev_err(&op->dev, "Cannot map global registers.\n");
2536cc216e4bSSean Anderson err = PTR_ERR(hp->gregs);
2537cc216e4bSSean Anderson goto err_out_clear_quattro;
2538e689cf4aSJeff Kirsher }
2539e689cf4aSJeff Kirsher
2540cc216e4bSSean Anderson hp->etxregs = devm_platform_ioremap_resource(op, 1);
2541cc216e4bSSean Anderson if (IS_ERR(hp->etxregs)) {
25428acf878fSSean Anderson dev_err(&op->dev, "Cannot map MAC TX registers.\n");
2543cc216e4bSSean Anderson err = PTR_ERR(hp->etxregs);
2544cc216e4bSSean Anderson goto err_out_clear_quattro;
2545e689cf4aSJeff Kirsher }
2546e689cf4aSJeff Kirsher
2547cc216e4bSSean Anderson hp->erxregs = devm_platform_ioremap_resource(op, 2);
2548cc216e4bSSean Anderson if (IS_ERR(hp->erxregs)) {
25498acf878fSSean Anderson dev_err(&op->dev, "Cannot map MAC RX registers.\n");
2550cc216e4bSSean Anderson err = PTR_ERR(hp->erxregs);
2551cc216e4bSSean Anderson goto err_out_clear_quattro;
2552e689cf4aSJeff Kirsher }
2553e689cf4aSJeff Kirsher
2554cc216e4bSSean Anderson hp->bigmacregs = devm_platform_ioremap_resource(op, 3);
2555cc216e4bSSean Anderson if (IS_ERR(hp->bigmacregs)) {
25568acf878fSSean Anderson dev_err(&op->dev, "Cannot map BIGMAC registers.\n");
2557cc216e4bSSean Anderson err = PTR_ERR(hp->bigmacregs);
2558cc216e4bSSean Anderson goto err_out_clear_quattro;
2559e689cf4aSJeff Kirsher }
2560e689cf4aSJeff Kirsher
2561cc216e4bSSean Anderson hp->tcvregs = devm_platform_ioremap_resource(op, 4);
2562cc216e4bSSean Anderson if (IS_ERR(hp->tcvregs)) {
25638acf878fSSean Anderson dev_err(&op->dev, "Cannot map TCVR registers.\n");
2564cc216e4bSSean Anderson err = PTR_ERR(hp->tcvregs);
2565cc216e4bSSean Anderson goto err_out_clear_quattro;
2566e689cf4aSJeff Kirsher }
2567e689cf4aSJeff Kirsher
2568e689cf4aSJeff Kirsher hp->hm_revision = 0xa0;
2569e689cf4aSJeff Kirsher
2570e689cf4aSJeff Kirsher if (qp != NULL)
2571e689cf4aSJeff Kirsher hp->happy_flags |= HFLAG_QUATTRO;
2572e689cf4aSJeff Kirsher
2573ecdcd042SSean Anderson hp->irq = op->archdata.irqs[0];
2574ecdcd042SSean Anderson
2575e689cf4aSJeff Kirsher /* Get the supported DVMA burst sizes from our Happy SBUS. */
2576e689cf4aSJeff Kirsher hp->happy_bursts = of_getintprop_default(sbus_dp,
2577e689cf4aSJeff Kirsher "burst-sizes", 0x00);
2578e689cf4aSJeff Kirsher
2579ecdcd042SSean Anderson #ifdef CONFIG_PCI
2580e689cf4aSJeff Kirsher /* Hook up SBUS register/descriptor accessors. */
2581e689cf4aSJeff Kirsher hp->read_desc32 = sbus_hme_read_desc32;
2582e689cf4aSJeff Kirsher hp->write_txd = sbus_hme_write_txd;
2583e689cf4aSJeff Kirsher hp->write_rxd = sbus_hme_write_rxd;
2584e689cf4aSJeff Kirsher hp->read32 = sbus_hme_read32;
2585e689cf4aSJeff Kirsher hp->write32 = sbus_hme_write32;
2586e689cf4aSJeff Kirsher #endif
2587e689cf4aSJeff Kirsher
2588ecdcd042SSean Anderson err = happy_meal_common_probe(hp, dp);
2589ecdcd042SSean Anderson if (err)
2590cc216e4bSSean Anderson goto err_out_clear_quattro;
2591e689cf4aSJeff Kirsher
2592e04e37a8SJingoo Han platform_set_drvdata(op, hp);
2593e689cf4aSJeff Kirsher
2594e689cf4aSJeff Kirsher if (qfe_slot != -1)
259524cddbc3SSean Anderson netdev_info(dev,
259624cddbc3SSean Anderson "Quattro HME slot %d (SBUS) 10/100baseT Ethernet %pM\n",
259724cddbc3SSean Anderson qfe_slot, dev->dev_addr);
2598e689cf4aSJeff Kirsher else
259924cddbc3SSean Anderson netdev_info(dev, "HAPPY MEAL (SBUS) 10/100baseT Ethernet %pM\n",
260024cddbc3SSean Anderson dev->dev_addr);
2601e689cf4aSJeff Kirsher
2602e689cf4aSJeff Kirsher return 0;
2603e689cf4aSJeff Kirsher
2604cc216e4bSSean Anderson err_out_clear_quattro:
2605e689cf4aSJeff Kirsher if (qp)
2606e689cf4aSJeff Kirsher qp->happy_meals[qfe_slot] = NULL;
2607e689cf4aSJeff Kirsher return err;
2608e689cf4aSJeff Kirsher }
2609e689cf4aSJeff Kirsher #endif
2610e689cf4aSJeff Kirsher
2611e689cf4aSJeff Kirsher #ifdef CONFIG_PCI
happy_meal_pci_probe(struct pci_dev * pdev,const struct pci_device_id * ent)2612f73d12bdSBill Pemberton static int happy_meal_pci_probe(struct pci_dev *pdev,
2613e689cf4aSJeff Kirsher const struct pci_device_id *ent)
2614e689cf4aSJeff Kirsher {
2615273fb669SSean Anderson struct device_node *dp = NULL;
2616e689cf4aSJeff Kirsher struct quattro *qp = NULL;
2617e689cf4aSJeff Kirsher struct happy_meal *hp;
2618e689cf4aSJeff Kirsher struct net_device *dev;
2619e689cf4aSJeff Kirsher void __iomem *hpreg_base;
2620914d9b27SRolf Eike Beer struct resource *hpreg_res;
2621e689cf4aSJeff Kirsher char prom_name[64];
2622273fb669SSean Anderson int qfe_slot = -1;
2623d6115741SSean Anderson int err = -ENODEV;
2624e689cf4aSJeff Kirsher
2625e689cf4aSJeff Kirsher /* Now make sure pci_dev cookie is there. */
2626e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC
2627e689cf4aSJeff Kirsher dp = pci_device_to_OF_node(pdev);
262821c328dcSRob Herring snprintf(prom_name, sizeof(prom_name), "%pOFn", dp);
2629e689cf4aSJeff Kirsher #else
2630e689cf4aSJeff Kirsher if (is_quattro_p(pdev))
2631e689cf4aSJeff Kirsher strcpy(prom_name, "SUNW,qfe");
2632e689cf4aSJeff Kirsher else
2633e689cf4aSJeff Kirsher strcpy(prom_name, "SUNW,hme");
2634e689cf4aSJeff Kirsher #endif
2635e689cf4aSJeff Kirsher
2636914d9b27SRolf Eike Beer err = pcim_enable_device(pdev);
2637acb3f35fSRolf Eike Beer if (err)
2638902fe6e9SSean Anderson return err;
2639e689cf4aSJeff Kirsher pci_set_master(pdev);
2640e689cf4aSJeff Kirsher
2641e689cf4aSJeff Kirsher if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
2642e689cf4aSJeff Kirsher qp = quattro_pci_find(pdev);
2643902fe6e9SSean Anderson if (IS_ERR(qp))
2644902fe6e9SSean Anderson return PTR_ERR(qp);
2645d6f1e89bSSean Anderson
2646e689cf4aSJeff Kirsher for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
2647d6f1e89bSSean Anderson if (!qp->happy_meals[qfe_slot])
2648e689cf4aSJeff Kirsher break;
2649d6f1e89bSSean Anderson
2650e689cf4aSJeff Kirsher if (qfe_slot == 4)
2651902fe6e9SSean Anderson return -ENODEV;
2652e689cf4aSJeff Kirsher }
2653e689cf4aSJeff Kirsher
2654914d9b27SRolf Eike Beer dev = devm_alloc_etherdev(&pdev->dev, sizeof(struct happy_meal));
2655902fe6e9SSean Anderson if (!dev)
2656902fe6e9SSean Anderson return -ENOMEM;
2657e689cf4aSJeff Kirsher SET_NETDEV_DEV(dev, &pdev->dev);
2658e689cf4aSJeff Kirsher
2659e689cf4aSJeff Kirsher hp = netdev_priv(dev);
2660273fb669SSean Anderson hp->dev = dev;
2661e689cf4aSJeff Kirsher hp->happy_dev = pdev;
2662e689cf4aSJeff Kirsher hp->dma_dev = &pdev->dev;
2663e689cf4aSJeff Kirsher
2664e689cf4aSJeff Kirsher spin_lock_init(&hp->happy_lock);
2665e689cf4aSJeff Kirsher
2666e689cf4aSJeff Kirsher if (qp != NULL) {
2667e689cf4aSJeff Kirsher hp->qfe_parent = qp;
2668e689cf4aSJeff Kirsher hp->qfe_ent = qfe_slot;
2669e689cf4aSJeff Kirsher qp->happy_meals[qfe_slot] = dev;
2670e689cf4aSJeff Kirsher }
2671e689cf4aSJeff Kirsher
26725b3dc6ddSSean Anderson err = -EINVAL;
2673e689cf4aSJeff Kirsher if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
26748acf878fSSean Anderson dev_err(&pdev->dev,
26758acf878fSSean Anderson "Cannot find proper PCI device base address.\n");
2676e689cf4aSJeff Kirsher goto err_out_clear_quattro;
2677e689cf4aSJeff Kirsher }
26785b3dc6ddSSean Anderson
2679ee0a735fSSean Anderson hpreg_res = devm_request_mem_region(&pdev->dev,
2680ee0a735fSSean Anderson pci_resource_start(pdev, 0),
2681ee0a735fSSean Anderson pci_resource_len(pdev, 0),
2682ee0a735fSSean Anderson DRV_NAME);
268399df45c9SDan Carpenter if (!hpreg_res) {
268499df45c9SDan Carpenter err = -EBUSY;
26858acf878fSSean Anderson dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting.\n");
2686e689cf4aSJeff Kirsher goto err_out_clear_quattro;
2687e689cf4aSJeff Kirsher }
2688e689cf4aSJeff Kirsher
2689914d9b27SRolf Eike Beer hpreg_base = pcim_iomap(pdev, 0, 0x8000);
26905b3dc6ddSSean Anderson if (!hpreg_base) {
26915b3dc6ddSSean Anderson err = -ENOMEM;
26928acf878fSSean Anderson dev_err(&pdev->dev, "Unable to remap card memory.\n");
2693914d9b27SRolf Eike Beer goto err_out_clear_quattro;
2694e689cf4aSJeff Kirsher }
2695e689cf4aSJeff Kirsher
2696273fb669SSean Anderson happy_meal_addr_init(hp, dp, qfe_slot);
2697e689cf4aSJeff Kirsher
2698e689cf4aSJeff Kirsher /* Layout registers. */
2699e689cf4aSJeff Kirsher hp->gregs = (hpreg_base + 0x0000UL);
2700e689cf4aSJeff Kirsher hp->etxregs = (hpreg_base + 0x2000UL);
2701e689cf4aSJeff Kirsher hp->erxregs = (hpreg_base + 0x4000UL);
2702e689cf4aSJeff Kirsher hp->bigmacregs = (hpreg_base + 0x6000UL);
2703e689cf4aSJeff Kirsher hp->tcvregs = (hpreg_base + 0x7000UL);
2704e689cf4aSJeff Kirsher
2705ecdcd042SSean Anderson if (IS_ENABLED(CONFIG_SPARC))
2706e689cf4aSJeff Kirsher hp->hm_revision = 0xc0 | (pdev->revision & 0x0f);
2707ecdcd042SSean Anderson else
2708e689cf4aSJeff Kirsher hp->hm_revision = 0x20;
2709e689cf4aSJeff Kirsher
2710e689cf4aSJeff Kirsher if (qp != NULL)
2711e689cf4aSJeff Kirsher hp->happy_flags |= HFLAG_QUATTRO;
2712e689cf4aSJeff Kirsher
2713e689cf4aSJeff Kirsher /* And of course, indicate this is PCI. */
2714e689cf4aSJeff Kirsher hp->happy_flags |= HFLAG_PCI;
2715e689cf4aSJeff Kirsher
2716e689cf4aSJeff Kirsher #ifdef CONFIG_SPARC
2717e689cf4aSJeff Kirsher /* Assume PCI happy meals can handle all burst sizes. */
2718e689cf4aSJeff Kirsher hp->happy_bursts = DMA_BURSTBITS;
2719e689cf4aSJeff Kirsher #endif
27207deb1182SFrancois Romieu hp->irq = pdev->irq;
2721e689cf4aSJeff Kirsher
2722ecdcd042SSean Anderson #ifdef CONFIG_SBUS
2723e689cf4aSJeff Kirsher /* Hook up PCI register/descriptor accessors. */
2724e689cf4aSJeff Kirsher hp->read_desc32 = pci_hme_read_desc32;
2725e689cf4aSJeff Kirsher hp->write_txd = pci_hme_write_txd;
2726e689cf4aSJeff Kirsher hp->write_rxd = pci_hme_write_rxd;
2727e689cf4aSJeff Kirsher hp->read32 = pci_hme_read32;
2728e689cf4aSJeff Kirsher hp->write32 = pci_hme_write32;
2729e689cf4aSJeff Kirsher #endif
2730e689cf4aSJeff Kirsher
2731ecdcd042SSean Anderson err = happy_meal_common_probe(hp, dp);
2732ecdcd042SSean Anderson if (err)
2733914d9b27SRolf Eike Beer goto err_out_clear_quattro;
2734e689cf4aSJeff Kirsher
2735521a87cdSJingoo Han pci_set_drvdata(pdev, hp);
2736e689cf4aSJeff Kirsher
2737e689cf4aSJeff Kirsher if (!qfe_slot) {
2738e689cf4aSJeff Kirsher struct pci_dev *qpdev = qp->quattro_dev;
2739e689cf4aSJeff Kirsher
2740e689cf4aSJeff Kirsher prom_name[0] = 0;
2741e689cf4aSJeff Kirsher if (!strncmp(dev->name, "eth", 3)) {
2742e689cf4aSJeff Kirsher int i = simple_strtoul(dev->name + 3, NULL, 10);
2743e689cf4aSJeff Kirsher sprintf(prom_name, "-%d", i + 3);
2744e689cf4aSJeff Kirsher }
27458acf878fSSean Anderson netdev_info(dev,
274624cddbc3SSean Anderson "%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet bridge %04x.%04x\n",
274724cddbc3SSean Anderson prom_name, qpdev->vendor, qpdev->device);
2748e689cf4aSJeff Kirsher }
2749e689cf4aSJeff Kirsher
2750e689cf4aSJeff Kirsher if (qfe_slot != -1)
27518acf878fSSean Anderson netdev_info(dev,
275224cddbc3SSean Anderson "Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet %pM\n",
275324cddbc3SSean Anderson qfe_slot, dev->dev_addr);
2754e689cf4aSJeff Kirsher else
27558acf878fSSean Anderson netdev_info(dev,
275624cddbc3SSean Anderson "HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet %pM\n",
275724cddbc3SSean Anderson dev->dev_addr);
2758e689cf4aSJeff Kirsher
2759e689cf4aSJeff Kirsher return 0;
2760e689cf4aSJeff Kirsher
2761e689cf4aSJeff Kirsher err_out_clear_quattro:
2762e689cf4aSJeff Kirsher if (qp != NULL)
2763e689cf4aSJeff Kirsher qp->happy_meals[qfe_slot] = NULL;
2764e689cf4aSJeff Kirsher return err;
2765e689cf4aSJeff Kirsher }
2766e689cf4aSJeff Kirsher
27679baa3c34SBenoit Taine static const struct pci_device_id happymeal_pci_ids[] = {
2768e689cf4aSJeff Kirsher { PCI_DEVICE(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_HAPPYMEAL) },
2769e689cf4aSJeff Kirsher { } /* Terminating entry */
2770e689cf4aSJeff Kirsher };
2771e689cf4aSJeff Kirsher
2772e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(pci, happymeal_pci_ids);
2773e689cf4aSJeff Kirsher
2774e689cf4aSJeff Kirsher static struct pci_driver hme_pci_driver = {
2775e689cf4aSJeff Kirsher .name = "hme",
2776e689cf4aSJeff Kirsher .id_table = happymeal_pci_ids,
2777e689cf4aSJeff Kirsher .probe = happy_meal_pci_probe,
2778e689cf4aSJeff Kirsher };
2779e689cf4aSJeff Kirsher
happy_meal_pci_init(void)2780e689cf4aSJeff Kirsher static int __init happy_meal_pci_init(void)
2781e689cf4aSJeff Kirsher {
2782e689cf4aSJeff Kirsher return pci_register_driver(&hme_pci_driver);
2783e689cf4aSJeff Kirsher }
2784e689cf4aSJeff Kirsher
happy_meal_pci_exit(void)2785e689cf4aSJeff Kirsher static void happy_meal_pci_exit(void)
2786e689cf4aSJeff Kirsher {
2787e689cf4aSJeff Kirsher pci_unregister_driver(&hme_pci_driver);
2788e689cf4aSJeff Kirsher
2789e689cf4aSJeff Kirsher while (qfe_pci_list) {
2790e689cf4aSJeff Kirsher struct quattro *qfe = qfe_pci_list;
2791e689cf4aSJeff Kirsher struct quattro *next = qfe->next;
2792e689cf4aSJeff Kirsher
2793e689cf4aSJeff Kirsher kfree(qfe);
2794e689cf4aSJeff Kirsher
2795e689cf4aSJeff Kirsher qfe_pci_list = next;
2796e689cf4aSJeff Kirsher }
2797e689cf4aSJeff Kirsher }
2798e689cf4aSJeff Kirsher
2799e689cf4aSJeff Kirsher #endif
2800e689cf4aSJeff Kirsher
2801e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2802e689cf4aSJeff Kirsher static const struct of_device_id hme_sbus_match[];
hme_sbus_probe(struct platform_device * op)2803f73d12bdSBill Pemberton static int hme_sbus_probe(struct platform_device *op)
2804e689cf4aSJeff Kirsher {
2805e689cf4aSJeff Kirsher const struct of_device_id *match;
2806e689cf4aSJeff Kirsher struct device_node *dp = op->dev.of_node;
2807e689cf4aSJeff Kirsher const char *model = of_get_property(dp, "model", NULL);
2808e689cf4aSJeff Kirsher int is_qfe;
2809e689cf4aSJeff Kirsher
2810e689cf4aSJeff Kirsher match = of_match_device(hme_sbus_match, &op->dev);
2811e689cf4aSJeff Kirsher if (!match)
2812e689cf4aSJeff Kirsher return -EINVAL;
2813e689cf4aSJeff Kirsher is_qfe = (match->data != NULL);
2814e689cf4aSJeff Kirsher
2815e689cf4aSJeff Kirsher if (!is_qfe && model && !strcmp(model, "SUNW,sbus-qfe"))
2816e689cf4aSJeff Kirsher is_qfe = 1;
2817e689cf4aSJeff Kirsher
2818e689cf4aSJeff Kirsher return happy_meal_sbus_probe_one(op, is_qfe);
2819e689cf4aSJeff Kirsher }
2820e689cf4aSJeff Kirsher
2821e689cf4aSJeff Kirsher static const struct of_device_id hme_sbus_match[] = {
2822e689cf4aSJeff Kirsher {
2823e689cf4aSJeff Kirsher .name = "SUNW,hme",
2824e689cf4aSJeff Kirsher },
2825e689cf4aSJeff Kirsher {
2826e689cf4aSJeff Kirsher .name = "SUNW,qfe",
2827e689cf4aSJeff Kirsher .data = (void *) 1,
2828e689cf4aSJeff Kirsher },
2829e689cf4aSJeff Kirsher {
2830e689cf4aSJeff Kirsher .name = "qfe",
2831e689cf4aSJeff Kirsher .data = (void *) 1,
2832e689cf4aSJeff Kirsher },
2833e689cf4aSJeff Kirsher {},
2834e689cf4aSJeff Kirsher };
2835e689cf4aSJeff Kirsher
2836e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(of, hme_sbus_match);
2837e689cf4aSJeff Kirsher
2838e689cf4aSJeff Kirsher static struct platform_driver hme_sbus_driver = {
2839e689cf4aSJeff Kirsher .driver = {
2840e689cf4aSJeff Kirsher .name = "hme",
2841e689cf4aSJeff Kirsher .of_match_table = hme_sbus_match,
2842e689cf4aSJeff Kirsher },
2843e689cf4aSJeff Kirsher .probe = hme_sbus_probe,
2844e689cf4aSJeff Kirsher };
2845e689cf4aSJeff Kirsher
happy_meal_sbus_init(void)2846e689cf4aSJeff Kirsher static int __init happy_meal_sbus_init(void)
2847e689cf4aSJeff Kirsher {
284827b9ea8fSSean Anderson return platform_driver_register(&hme_sbus_driver);
2849e689cf4aSJeff Kirsher }
2850e689cf4aSJeff Kirsher
happy_meal_sbus_exit(void)2851e689cf4aSJeff Kirsher static void happy_meal_sbus_exit(void)
2852e689cf4aSJeff Kirsher {
2853e689cf4aSJeff Kirsher platform_driver_unregister(&hme_sbus_driver);
2854e689cf4aSJeff Kirsher
2855e689cf4aSJeff Kirsher while (qfe_sbus_list) {
2856e689cf4aSJeff Kirsher struct quattro *qfe = qfe_sbus_list;
2857e689cf4aSJeff Kirsher struct quattro *next = qfe->next;
2858e689cf4aSJeff Kirsher
2859e689cf4aSJeff Kirsher kfree(qfe);
2860e689cf4aSJeff Kirsher
2861e689cf4aSJeff Kirsher qfe_sbus_list = next;
2862e689cf4aSJeff Kirsher }
2863e689cf4aSJeff Kirsher }
2864e689cf4aSJeff Kirsher #endif
2865e689cf4aSJeff Kirsher
happy_meal_probe(void)2866e689cf4aSJeff Kirsher static int __init happy_meal_probe(void)
2867e689cf4aSJeff Kirsher {
2868e689cf4aSJeff Kirsher int err = 0;
2869e689cf4aSJeff Kirsher
2870e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2871e689cf4aSJeff Kirsher err = happy_meal_sbus_init();
2872e689cf4aSJeff Kirsher #endif
2873e689cf4aSJeff Kirsher #ifdef CONFIG_PCI
2874e689cf4aSJeff Kirsher if (!err) {
2875e689cf4aSJeff Kirsher err = happy_meal_pci_init();
2876e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2877e689cf4aSJeff Kirsher if (err)
2878e689cf4aSJeff Kirsher happy_meal_sbus_exit();
2879e689cf4aSJeff Kirsher #endif
2880e689cf4aSJeff Kirsher }
2881e689cf4aSJeff Kirsher #endif
2882e689cf4aSJeff Kirsher
2883e689cf4aSJeff Kirsher return err;
2884e689cf4aSJeff Kirsher }
2885e689cf4aSJeff Kirsher
2886e689cf4aSJeff Kirsher
happy_meal_exit(void)2887e689cf4aSJeff Kirsher static void __exit happy_meal_exit(void)
2888e689cf4aSJeff Kirsher {
2889e689cf4aSJeff Kirsher #ifdef CONFIG_SBUS
2890e689cf4aSJeff Kirsher happy_meal_sbus_exit();
2891e689cf4aSJeff Kirsher #endif
2892e689cf4aSJeff Kirsher #ifdef CONFIG_PCI
2893e689cf4aSJeff Kirsher happy_meal_pci_exit();
2894e689cf4aSJeff Kirsher #endif
2895e689cf4aSJeff Kirsher }
2896e689cf4aSJeff Kirsher
2897e689cf4aSJeff Kirsher module_init(happy_meal_probe);
2898e689cf4aSJeff Kirsher module_exit(happy_meal_exit);
2899