11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2adfc5217SJeff Kirsher /*
3adfc5217SJeff Kirsher * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
4adfc5217SJeff Kirsher * Copyright (c) 2006, 2007 Maciej W. Rozycki
5adfc5217SJeff Kirsher *
6adfc5217SJeff Kirsher * This driver is designed for the Broadcom SiByte SOC built-in
7adfc5217SJeff Kirsher * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
8adfc5217SJeff Kirsher *
9adfc5217SJeff Kirsher * Updated to the driver model and the PHY abstraction layer
10adfc5217SJeff Kirsher * by Maciej W. Rozycki.
11adfc5217SJeff Kirsher */
12adfc5217SJeff Kirsher
13adfc5217SJeff Kirsher #include <linux/bug.h>
14adfc5217SJeff Kirsher #include <linux/module.h>
15adfc5217SJeff Kirsher #include <linux/kernel.h>
16adfc5217SJeff Kirsher #include <linux/string.h>
17adfc5217SJeff Kirsher #include <linux/timer.h>
18adfc5217SJeff Kirsher #include <linux/errno.h>
19adfc5217SJeff Kirsher #include <linux/ioport.h>
20adfc5217SJeff Kirsher #include <linux/slab.h>
21adfc5217SJeff Kirsher #include <linux/interrupt.h>
22adfc5217SJeff Kirsher #include <linux/netdevice.h>
23adfc5217SJeff Kirsher #include <linux/etherdevice.h>
24adfc5217SJeff Kirsher #include <linux/skbuff.h>
25adfc5217SJeff Kirsher #include <linux/bitops.h>
26adfc5217SJeff Kirsher #include <linux/err.h>
27adfc5217SJeff Kirsher #include <linux/ethtool.h>
28adfc5217SJeff Kirsher #include <linux/mii.h>
29adfc5217SJeff Kirsher #include <linux/phy.h>
30adfc5217SJeff Kirsher #include <linux/platform_device.h>
31adfc5217SJeff Kirsher #include <linux/prefetch.h>
32adfc5217SJeff Kirsher
33adfc5217SJeff Kirsher #include <asm/cache.h>
34adfc5217SJeff Kirsher #include <asm/io.h>
35adfc5217SJeff Kirsher #include <asm/processor.h> /* Processor type for cache alignment. */
36adfc5217SJeff Kirsher
37adfc5217SJeff Kirsher /* Operational parameters that usually are not changed. */
38adfc5217SJeff Kirsher
39adfc5217SJeff Kirsher #define CONFIG_SBMAC_COALESCE
40adfc5217SJeff Kirsher
41adfc5217SJeff Kirsher /* Time in jiffies before concluding the transmitter is hung. */
42adfc5217SJeff Kirsher #define TX_TIMEOUT (2*HZ)
43adfc5217SJeff Kirsher
44adfc5217SJeff Kirsher
45adfc5217SJeff Kirsher MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
46adfc5217SJeff Kirsher MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
47adfc5217SJeff Kirsher
48adfc5217SJeff Kirsher /* A few user-configurable values which may be modified when a driver
49adfc5217SJeff Kirsher module is loaded. */
50adfc5217SJeff Kirsher
51adfc5217SJeff Kirsher /* 1 normal messages, 0 quiet .. 7 verbose. */
52adfc5217SJeff Kirsher static int debug = 1;
53d3757ba4SJoe Perches module_param(debug, int, 0444);
54adfc5217SJeff Kirsher MODULE_PARM_DESC(debug, "Debug messages");
55adfc5217SJeff Kirsher
56adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
57adfc5217SJeff Kirsher static int int_pktcnt_tx = 255;
58d3757ba4SJoe Perches module_param(int_pktcnt_tx, int, 0444);
59adfc5217SJeff Kirsher MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
60adfc5217SJeff Kirsher
61adfc5217SJeff Kirsher static int int_timeout_tx = 255;
62d3757ba4SJoe Perches module_param(int_timeout_tx, int, 0444);
63adfc5217SJeff Kirsher MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
64adfc5217SJeff Kirsher
65adfc5217SJeff Kirsher static int int_pktcnt_rx = 64;
66d3757ba4SJoe Perches module_param(int_pktcnt_rx, int, 0444);
67adfc5217SJeff Kirsher MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
68adfc5217SJeff Kirsher
69adfc5217SJeff Kirsher static int int_timeout_rx = 64;
70d3757ba4SJoe Perches module_param(int_timeout_rx, int, 0444);
71adfc5217SJeff Kirsher MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
72adfc5217SJeff Kirsher #endif
73adfc5217SJeff Kirsher
74adfc5217SJeff Kirsher #include <asm/sibyte/board.h>
75adfc5217SJeff Kirsher #include <asm/sibyte/sb1250.h>
76*c34ce279SLukas Bulwahn #if defined(CONFIG_SIBYTE_BCM1x80)
77adfc5217SJeff Kirsher #include <asm/sibyte/bcm1480_regs.h>
78adfc5217SJeff Kirsher #include <asm/sibyte/bcm1480_int.h>
79adfc5217SJeff Kirsher #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
80adfc5217SJeff Kirsher #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
81adfc5217SJeff Kirsher #include <asm/sibyte/sb1250_regs.h>
82adfc5217SJeff Kirsher #include <asm/sibyte/sb1250_int.h>
83adfc5217SJeff Kirsher #else
84adfc5217SJeff Kirsher #error invalid SiByte MAC configuration
85adfc5217SJeff Kirsher #endif
86adfc5217SJeff Kirsher #include <asm/sibyte/sb1250_scd.h>
87adfc5217SJeff Kirsher #include <asm/sibyte/sb1250_mac.h>
88adfc5217SJeff Kirsher #include <asm/sibyte/sb1250_dma.h>
89adfc5217SJeff Kirsher
90*c34ce279SLukas Bulwahn #if defined(CONFIG_SIBYTE_BCM1x80)
91adfc5217SJeff Kirsher #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
92adfc5217SJeff Kirsher #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
93adfc5217SJeff Kirsher #define UNIT_INT(n) (K_INT_MAC_0 + (n))
94adfc5217SJeff Kirsher #else
95adfc5217SJeff Kirsher #error invalid SiByte MAC configuration
96adfc5217SJeff Kirsher #endif
97adfc5217SJeff Kirsher
98adfc5217SJeff Kirsher #ifdef K_INT_PHY
99adfc5217SJeff Kirsher #define SBMAC_PHY_INT K_INT_PHY
100adfc5217SJeff Kirsher #else
101adfc5217SJeff Kirsher #define SBMAC_PHY_INT PHY_POLL
102adfc5217SJeff Kirsher #endif
103adfc5217SJeff Kirsher
104adfc5217SJeff Kirsher /**********************************************************************
105adfc5217SJeff Kirsher * Simple types
106adfc5217SJeff Kirsher ********************************************************************* */
107adfc5217SJeff Kirsher
108adfc5217SJeff Kirsher enum sbmac_speed {
109adfc5217SJeff Kirsher sbmac_speed_none = 0,
110adfc5217SJeff Kirsher sbmac_speed_10 = SPEED_10,
111adfc5217SJeff Kirsher sbmac_speed_100 = SPEED_100,
112adfc5217SJeff Kirsher sbmac_speed_1000 = SPEED_1000,
113adfc5217SJeff Kirsher };
114adfc5217SJeff Kirsher
115adfc5217SJeff Kirsher enum sbmac_duplex {
116adfc5217SJeff Kirsher sbmac_duplex_none = -1,
117adfc5217SJeff Kirsher sbmac_duplex_half = DUPLEX_HALF,
118adfc5217SJeff Kirsher sbmac_duplex_full = DUPLEX_FULL,
119adfc5217SJeff Kirsher };
120adfc5217SJeff Kirsher
121adfc5217SJeff Kirsher enum sbmac_fc {
122adfc5217SJeff Kirsher sbmac_fc_none,
123adfc5217SJeff Kirsher sbmac_fc_disabled,
124adfc5217SJeff Kirsher sbmac_fc_frame,
125adfc5217SJeff Kirsher sbmac_fc_collision,
126adfc5217SJeff Kirsher sbmac_fc_carrier,
127adfc5217SJeff Kirsher };
128adfc5217SJeff Kirsher
129adfc5217SJeff Kirsher enum sbmac_state {
130adfc5217SJeff Kirsher sbmac_state_uninit,
131adfc5217SJeff Kirsher sbmac_state_off,
132adfc5217SJeff Kirsher sbmac_state_on,
133adfc5217SJeff Kirsher sbmac_state_broken,
134adfc5217SJeff Kirsher };
135adfc5217SJeff Kirsher
136adfc5217SJeff Kirsher
137adfc5217SJeff Kirsher /**********************************************************************
138adfc5217SJeff Kirsher * Macros
139adfc5217SJeff Kirsher ********************************************************************* */
140adfc5217SJeff Kirsher
141adfc5217SJeff Kirsher
142adfc5217SJeff Kirsher #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
143adfc5217SJeff Kirsher (d)->sbdma_dscrtable : (d)->f+1)
144adfc5217SJeff Kirsher
145adfc5217SJeff Kirsher
146f8a1988fSzhong jiang #define NUMCACHEBLKS(x) DIV_ROUND_UP(x, SMP_CACHE_BYTES)
147adfc5217SJeff Kirsher
148adfc5217SJeff Kirsher #define SBMAC_MAX_TXDESCR 256
149adfc5217SJeff Kirsher #define SBMAC_MAX_RXDESCR 256
150adfc5217SJeff Kirsher
151adfc5217SJeff Kirsher #define ENET_PACKET_SIZE 1518
152adfc5217SJeff Kirsher /*#define ENET_PACKET_SIZE 9216 */
153adfc5217SJeff Kirsher
154adfc5217SJeff Kirsher /**********************************************************************
155adfc5217SJeff Kirsher * DMA Descriptor structure
156adfc5217SJeff Kirsher ********************************************************************* */
157adfc5217SJeff Kirsher
158adfc5217SJeff Kirsher struct sbdmadscr {
159adfc5217SJeff Kirsher uint64_t dscr_a;
160adfc5217SJeff Kirsher uint64_t dscr_b;
161adfc5217SJeff Kirsher };
162adfc5217SJeff Kirsher
163adfc5217SJeff Kirsher /**********************************************************************
164adfc5217SJeff Kirsher * DMA Controller structure
165adfc5217SJeff Kirsher ********************************************************************* */
166adfc5217SJeff Kirsher
167adfc5217SJeff Kirsher struct sbmacdma {
168adfc5217SJeff Kirsher
169adfc5217SJeff Kirsher /*
170adfc5217SJeff Kirsher * This stuff is used to identify the channel and the registers
171adfc5217SJeff Kirsher * associated with it.
172adfc5217SJeff Kirsher */
173adfc5217SJeff Kirsher struct sbmac_softc *sbdma_eth; /* back pointer to associated
174adfc5217SJeff Kirsher MAC */
175adfc5217SJeff Kirsher int sbdma_channel; /* channel number */
176adfc5217SJeff Kirsher int sbdma_txdir; /* direction (1=transmit) */
177adfc5217SJeff Kirsher int sbdma_maxdescr; /* total # of descriptors
178adfc5217SJeff Kirsher in ring */
179adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
180adfc5217SJeff Kirsher int sbdma_int_pktcnt;
181adfc5217SJeff Kirsher /* # descriptors rx/tx
182adfc5217SJeff Kirsher before interrupt */
183adfc5217SJeff Kirsher int sbdma_int_timeout;
184adfc5217SJeff Kirsher /* # usec rx/tx interrupt */
185adfc5217SJeff Kirsher #endif
186adfc5217SJeff Kirsher void __iomem *sbdma_config0; /* DMA config register 0 */
187adfc5217SJeff Kirsher void __iomem *sbdma_config1; /* DMA config register 1 */
188adfc5217SJeff Kirsher void __iomem *sbdma_dscrbase;
189adfc5217SJeff Kirsher /* descriptor base address */
190adfc5217SJeff Kirsher void __iomem *sbdma_dscrcnt; /* descriptor count register */
191adfc5217SJeff Kirsher void __iomem *sbdma_curdscr; /* current descriptor
192adfc5217SJeff Kirsher address */
193adfc5217SJeff Kirsher void __iomem *sbdma_oodpktlost;
194adfc5217SJeff Kirsher /* pkt drop (rx only) */
195adfc5217SJeff Kirsher
196adfc5217SJeff Kirsher /*
197adfc5217SJeff Kirsher * This stuff is for maintenance of the ring
198adfc5217SJeff Kirsher */
199adfc5217SJeff Kirsher void *sbdma_dscrtable_unaligned;
200adfc5217SJeff Kirsher struct sbdmadscr *sbdma_dscrtable;
201adfc5217SJeff Kirsher /* base of descriptor table */
202adfc5217SJeff Kirsher struct sbdmadscr *sbdma_dscrtable_end;
203adfc5217SJeff Kirsher /* end of descriptor table */
204adfc5217SJeff Kirsher struct sk_buff **sbdma_ctxtable;
205adfc5217SJeff Kirsher /* context table, one
206adfc5217SJeff Kirsher per descr */
207adfc5217SJeff Kirsher dma_addr_t sbdma_dscrtable_phys;
208adfc5217SJeff Kirsher /* and also the phys addr */
209adfc5217SJeff Kirsher struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
210adfc5217SJeff Kirsher struct sbdmadscr *sbdma_remptr; /* next dscr for sw
211adfc5217SJeff Kirsher to remove */
212adfc5217SJeff Kirsher };
213adfc5217SJeff Kirsher
214adfc5217SJeff Kirsher
215adfc5217SJeff Kirsher /**********************************************************************
216adfc5217SJeff Kirsher * Ethernet softc structure
217adfc5217SJeff Kirsher ********************************************************************* */
218adfc5217SJeff Kirsher
219adfc5217SJeff Kirsher struct sbmac_softc {
220adfc5217SJeff Kirsher
221adfc5217SJeff Kirsher /*
222adfc5217SJeff Kirsher * Linux-specific things
223adfc5217SJeff Kirsher */
224adfc5217SJeff Kirsher struct net_device *sbm_dev; /* pointer to linux device */
225adfc5217SJeff Kirsher struct napi_struct napi;
226adfc5217SJeff Kirsher struct phy_device *phy_dev; /* the associated PHY device */
227adfc5217SJeff Kirsher struct mii_bus *mii_bus; /* the MII bus */
228adfc5217SJeff Kirsher spinlock_t sbm_lock; /* spin lock */
229adfc5217SJeff Kirsher int sbm_devflags; /* current device flags */
230adfc5217SJeff Kirsher
231adfc5217SJeff Kirsher /*
232adfc5217SJeff Kirsher * Controller-specific things
233adfc5217SJeff Kirsher */
234adfc5217SJeff Kirsher void __iomem *sbm_base; /* MAC's base address */
235adfc5217SJeff Kirsher enum sbmac_state sbm_state; /* current state */
236adfc5217SJeff Kirsher
237adfc5217SJeff Kirsher void __iomem *sbm_macenable; /* MAC Enable Register */
238adfc5217SJeff Kirsher void __iomem *sbm_maccfg; /* MAC Config Register */
239adfc5217SJeff Kirsher void __iomem *sbm_fifocfg; /* FIFO Config Register */
240adfc5217SJeff Kirsher void __iomem *sbm_framecfg; /* Frame Config Register */
241adfc5217SJeff Kirsher void __iomem *sbm_rxfilter; /* Receive Filter Register */
242adfc5217SJeff Kirsher void __iomem *sbm_isr; /* Interrupt Status Register */
243adfc5217SJeff Kirsher void __iomem *sbm_imr; /* Interrupt Mask Register */
244adfc5217SJeff Kirsher void __iomem *sbm_mdio; /* MDIO Register */
245adfc5217SJeff Kirsher
246adfc5217SJeff Kirsher enum sbmac_speed sbm_speed; /* current speed */
247adfc5217SJeff Kirsher enum sbmac_duplex sbm_duplex; /* current duplex */
248adfc5217SJeff Kirsher enum sbmac_fc sbm_fc; /* cur. flow control setting */
249adfc5217SJeff Kirsher int sbm_pause; /* current pause setting */
250adfc5217SJeff Kirsher int sbm_link; /* current link state */
251adfc5217SJeff Kirsher
252104bf3fbSJoe Perches unsigned char sbm_hwaddr[ETH_ALEN];
253adfc5217SJeff Kirsher
254adfc5217SJeff Kirsher struct sbmacdma sbm_txdma; /* only channel 0 for now */
255adfc5217SJeff Kirsher struct sbmacdma sbm_rxdma;
256adfc5217SJeff Kirsher int rx_hw_checksum;
257adfc5217SJeff Kirsher int sbe_idx;
258adfc5217SJeff Kirsher };
259adfc5217SJeff Kirsher
260adfc5217SJeff Kirsher
261adfc5217SJeff Kirsher /**********************************************************************
262adfc5217SJeff Kirsher * Externs
263adfc5217SJeff Kirsher ********************************************************************* */
264adfc5217SJeff Kirsher
265adfc5217SJeff Kirsher /**********************************************************************
266adfc5217SJeff Kirsher * Prototypes
267adfc5217SJeff Kirsher ********************************************************************* */
268adfc5217SJeff Kirsher
269adfc5217SJeff Kirsher static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
270adfc5217SJeff Kirsher int txrx, int maxdescr);
271adfc5217SJeff Kirsher static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
272adfc5217SJeff Kirsher static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
273adfc5217SJeff Kirsher struct sk_buff *m);
274adfc5217SJeff Kirsher static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
275adfc5217SJeff Kirsher static void sbdma_emptyring(struct sbmacdma *d);
276adfc5217SJeff Kirsher static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
277adfc5217SJeff Kirsher static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
278adfc5217SJeff Kirsher int work_to_do, int poll);
279adfc5217SJeff Kirsher static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
280adfc5217SJeff Kirsher int poll);
281adfc5217SJeff Kirsher static int sbmac_initctx(struct sbmac_softc *s);
282adfc5217SJeff Kirsher static void sbmac_channel_start(struct sbmac_softc *s);
283adfc5217SJeff Kirsher static void sbmac_channel_stop(struct sbmac_softc *s);
284adfc5217SJeff Kirsher static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
285adfc5217SJeff Kirsher enum sbmac_state);
286adfc5217SJeff Kirsher static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
287adfc5217SJeff Kirsher static uint64_t sbmac_addr2reg(unsigned char *ptr);
288adfc5217SJeff Kirsher static irqreturn_t sbmac_intr(int irq, void *dev_instance);
2890c13b8d1SYueHaibing static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
290adfc5217SJeff Kirsher static void sbmac_setmulti(struct sbmac_softc *sc);
291adfc5217SJeff Kirsher static int sbmac_init(struct platform_device *pldev, long long base);
292adfc5217SJeff Kirsher static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
293adfc5217SJeff Kirsher static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
294adfc5217SJeff Kirsher enum sbmac_fc fc);
295adfc5217SJeff Kirsher
296adfc5217SJeff Kirsher static int sbmac_open(struct net_device *dev);
2970290bd29SMichael S. Tsirkin static void sbmac_tx_timeout (struct net_device *dev, unsigned int txqueue);
298adfc5217SJeff Kirsher static void sbmac_set_rx_mode(struct net_device *dev);
299adfc5217SJeff Kirsher static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
300adfc5217SJeff Kirsher static int sbmac_close(struct net_device *dev);
301adfc5217SJeff Kirsher static int sbmac_poll(struct napi_struct *napi, int budget);
302adfc5217SJeff Kirsher
303adfc5217SJeff Kirsher static void sbmac_mii_poll(struct net_device *dev);
304adfc5217SJeff Kirsher static int sbmac_mii_probe(struct net_device *dev);
305adfc5217SJeff Kirsher
306adfc5217SJeff Kirsher static void sbmac_mii_sync(void __iomem *sbm_mdio);
307adfc5217SJeff Kirsher static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
308adfc5217SJeff Kirsher int bitcnt);
309adfc5217SJeff Kirsher static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
310adfc5217SJeff Kirsher static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
311adfc5217SJeff Kirsher u16 val);
312adfc5217SJeff Kirsher
313adfc5217SJeff Kirsher
314adfc5217SJeff Kirsher /**********************************************************************
315adfc5217SJeff Kirsher * Globals
316adfc5217SJeff Kirsher ********************************************************************* */
317adfc5217SJeff Kirsher
318adfc5217SJeff Kirsher static char sbmac_string[] = "sb1250-mac";
319adfc5217SJeff Kirsher
320adfc5217SJeff Kirsher static char sbmac_mdio_string[] = "sb1250-mac-mdio";
321adfc5217SJeff Kirsher
322adfc5217SJeff Kirsher
323adfc5217SJeff Kirsher /**********************************************************************
324adfc5217SJeff Kirsher * MDIO constants
325adfc5217SJeff Kirsher ********************************************************************* */
326adfc5217SJeff Kirsher
327adfc5217SJeff Kirsher #define MII_COMMAND_START 0x01
328adfc5217SJeff Kirsher #define MII_COMMAND_READ 0x02
329adfc5217SJeff Kirsher #define MII_COMMAND_WRITE 0x01
330adfc5217SJeff Kirsher #define MII_COMMAND_ACK 0x02
331adfc5217SJeff Kirsher
332adfc5217SJeff Kirsher #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
333adfc5217SJeff Kirsher
334adfc5217SJeff Kirsher #define ENABLE 1
335adfc5217SJeff Kirsher #define DISABLE 0
336adfc5217SJeff Kirsher
337adfc5217SJeff Kirsher /**********************************************************************
338adfc5217SJeff Kirsher * SBMAC_MII_SYNC(sbm_mdio)
339adfc5217SJeff Kirsher *
340adfc5217SJeff Kirsher * Synchronize with the MII - send a pattern of bits to the MII
341adfc5217SJeff Kirsher * that will guarantee that it is ready to accept a command.
342adfc5217SJeff Kirsher *
343adfc5217SJeff Kirsher * Input parameters:
344adfc5217SJeff Kirsher * sbm_mdio - address of the MAC's MDIO register
345adfc5217SJeff Kirsher *
346adfc5217SJeff Kirsher * Return value:
347adfc5217SJeff Kirsher * nothing
348adfc5217SJeff Kirsher ********************************************************************* */
349adfc5217SJeff Kirsher
sbmac_mii_sync(void __iomem * sbm_mdio)350adfc5217SJeff Kirsher static void sbmac_mii_sync(void __iomem *sbm_mdio)
351adfc5217SJeff Kirsher {
352adfc5217SJeff Kirsher int cnt;
353adfc5217SJeff Kirsher uint64_t bits;
354adfc5217SJeff Kirsher int mac_mdio_genc;
355adfc5217SJeff Kirsher
356adfc5217SJeff Kirsher mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
357adfc5217SJeff Kirsher
358adfc5217SJeff Kirsher bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
359adfc5217SJeff Kirsher
360adfc5217SJeff Kirsher __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
361adfc5217SJeff Kirsher
362adfc5217SJeff Kirsher for (cnt = 0; cnt < 32; cnt++) {
363adfc5217SJeff Kirsher __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
364adfc5217SJeff Kirsher __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
365adfc5217SJeff Kirsher }
366adfc5217SJeff Kirsher }
367adfc5217SJeff Kirsher
368adfc5217SJeff Kirsher /**********************************************************************
369adfc5217SJeff Kirsher * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
370adfc5217SJeff Kirsher *
371adfc5217SJeff Kirsher * Send some bits to the MII. The bits to be sent are right-
372adfc5217SJeff Kirsher * justified in the 'data' parameter.
373adfc5217SJeff Kirsher *
374adfc5217SJeff Kirsher * Input parameters:
375adfc5217SJeff Kirsher * sbm_mdio - address of the MAC's MDIO register
376adfc5217SJeff Kirsher * data - data to send
377adfc5217SJeff Kirsher * bitcnt - number of bits to send
378adfc5217SJeff Kirsher ********************************************************************* */
379adfc5217SJeff Kirsher
sbmac_mii_senddata(void __iomem * sbm_mdio,unsigned int data,int bitcnt)380adfc5217SJeff Kirsher static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
381adfc5217SJeff Kirsher int bitcnt)
382adfc5217SJeff Kirsher {
383adfc5217SJeff Kirsher int i;
384adfc5217SJeff Kirsher uint64_t bits;
385adfc5217SJeff Kirsher unsigned int curmask;
386adfc5217SJeff Kirsher int mac_mdio_genc;
387adfc5217SJeff Kirsher
388adfc5217SJeff Kirsher mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
389adfc5217SJeff Kirsher
390adfc5217SJeff Kirsher bits = M_MAC_MDIO_DIR_OUTPUT;
391adfc5217SJeff Kirsher __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
392adfc5217SJeff Kirsher
393adfc5217SJeff Kirsher curmask = 1 << (bitcnt - 1);
394adfc5217SJeff Kirsher
395adfc5217SJeff Kirsher for (i = 0; i < bitcnt; i++) {
396adfc5217SJeff Kirsher if (data & curmask)
397adfc5217SJeff Kirsher bits |= M_MAC_MDIO_OUT;
398adfc5217SJeff Kirsher else bits &= ~M_MAC_MDIO_OUT;
399adfc5217SJeff Kirsher __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
400adfc5217SJeff Kirsher __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
401adfc5217SJeff Kirsher __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
402adfc5217SJeff Kirsher curmask >>= 1;
403adfc5217SJeff Kirsher }
404adfc5217SJeff Kirsher }
405adfc5217SJeff Kirsher
406adfc5217SJeff Kirsher
407adfc5217SJeff Kirsher
408adfc5217SJeff Kirsher /**********************************************************************
409adfc5217SJeff Kirsher * SBMAC_MII_READ(bus, phyaddr, regidx)
410adfc5217SJeff Kirsher * Read a PHY register.
411adfc5217SJeff Kirsher *
412adfc5217SJeff Kirsher * Input parameters:
413adfc5217SJeff Kirsher * bus - MDIO bus handle
414adfc5217SJeff Kirsher * phyaddr - PHY's address
415adfc5217SJeff Kirsher * regnum - index of register to read
416adfc5217SJeff Kirsher *
417adfc5217SJeff Kirsher * Return value:
418adfc5217SJeff Kirsher * value read, or 0xffff if an error occurred.
419adfc5217SJeff Kirsher ********************************************************************* */
420adfc5217SJeff Kirsher
sbmac_mii_read(struct mii_bus * bus,int phyaddr,int regidx)421adfc5217SJeff Kirsher static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
422adfc5217SJeff Kirsher {
423adfc5217SJeff Kirsher struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
424adfc5217SJeff Kirsher void __iomem *sbm_mdio = sc->sbm_mdio;
425adfc5217SJeff Kirsher int idx;
426adfc5217SJeff Kirsher int error;
427adfc5217SJeff Kirsher int regval;
428adfc5217SJeff Kirsher int mac_mdio_genc;
429adfc5217SJeff Kirsher
430adfc5217SJeff Kirsher /*
431adfc5217SJeff Kirsher * Synchronize ourselves so that the PHY knows the next
432adfc5217SJeff Kirsher * thing coming down is a command
433adfc5217SJeff Kirsher */
434adfc5217SJeff Kirsher sbmac_mii_sync(sbm_mdio);
435adfc5217SJeff Kirsher
436adfc5217SJeff Kirsher /*
437adfc5217SJeff Kirsher * Send the data to the PHY. The sequence is
438adfc5217SJeff Kirsher * a "start" command (2 bits)
439adfc5217SJeff Kirsher * a "read" command (2 bits)
440adfc5217SJeff Kirsher * the PHY addr (5 bits)
441adfc5217SJeff Kirsher * the register index (5 bits)
442adfc5217SJeff Kirsher */
443adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
444adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
445adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
446adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, regidx, 5);
447adfc5217SJeff Kirsher
448adfc5217SJeff Kirsher mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
449adfc5217SJeff Kirsher
450adfc5217SJeff Kirsher /*
451adfc5217SJeff Kirsher * Switch the port around without a clock transition.
452adfc5217SJeff Kirsher */
453adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
454adfc5217SJeff Kirsher
455adfc5217SJeff Kirsher /*
456adfc5217SJeff Kirsher * Send out a clock pulse to signal we want the status
457adfc5217SJeff Kirsher */
458adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
459adfc5217SJeff Kirsher sbm_mdio);
460adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
461adfc5217SJeff Kirsher
462adfc5217SJeff Kirsher /*
463adfc5217SJeff Kirsher * If an error occurred, the PHY will signal '1' back
464adfc5217SJeff Kirsher */
465adfc5217SJeff Kirsher error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
466adfc5217SJeff Kirsher
467adfc5217SJeff Kirsher /*
468adfc5217SJeff Kirsher * Issue an 'idle' clock pulse, but keep the direction
469adfc5217SJeff Kirsher * the same.
470adfc5217SJeff Kirsher */
471adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
472adfc5217SJeff Kirsher sbm_mdio);
473adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
474adfc5217SJeff Kirsher
475adfc5217SJeff Kirsher regval = 0;
476adfc5217SJeff Kirsher
477adfc5217SJeff Kirsher for (idx = 0; idx < 16; idx++) {
478adfc5217SJeff Kirsher regval <<= 1;
479adfc5217SJeff Kirsher
480adfc5217SJeff Kirsher if (error == 0) {
481adfc5217SJeff Kirsher if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
482adfc5217SJeff Kirsher regval |= 1;
483adfc5217SJeff Kirsher }
484adfc5217SJeff Kirsher
485adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
486adfc5217SJeff Kirsher sbm_mdio);
487adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
488adfc5217SJeff Kirsher }
489adfc5217SJeff Kirsher
490adfc5217SJeff Kirsher /* Switch back to output */
491adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
492adfc5217SJeff Kirsher
493adfc5217SJeff Kirsher if (error == 0)
494adfc5217SJeff Kirsher return regval;
495adfc5217SJeff Kirsher return 0xffff;
496adfc5217SJeff Kirsher }
497adfc5217SJeff Kirsher
498adfc5217SJeff Kirsher
499adfc5217SJeff Kirsher /**********************************************************************
500adfc5217SJeff Kirsher * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
501adfc5217SJeff Kirsher *
502adfc5217SJeff Kirsher * Write a value to a PHY register.
503adfc5217SJeff Kirsher *
504adfc5217SJeff Kirsher * Input parameters:
505adfc5217SJeff Kirsher * bus - MDIO bus handle
506adfc5217SJeff Kirsher * phyaddr - PHY to use
507adfc5217SJeff Kirsher * regidx - register within the PHY
508adfc5217SJeff Kirsher * regval - data to write to register
509adfc5217SJeff Kirsher *
510adfc5217SJeff Kirsher * Return value:
511adfc5217SJeff Kirsher * 0 for success
512adfc5217SJeff Kirsher ********************************************************************* */
513adfc5217SJeff Kirsher
sbmac_mii_write(struct mii_bus * bus,int phyaddr,int regidx,u16 regval)514adfc5217SJeff Kirsher static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
515adfc5217SJeff Kirsher u16 regval)
516adfc5217SJeff Kirsher {
517adfc5217SJeff Kirsher struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
518adfc5217SJeff Kirsher void __iomem *sbm_mdio = sc->sbm_mdio;
519adfc5217SJeff Kirsher int mac_mdio_genc;
520adfc5217SJeff Kirsher
521adfc5217SJeff Kirsher sbmac_mii_sync(sbm_mdio);
522adfc5217SJeff Kirsher
523adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
524adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
525adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
526adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, regidx, 5);
527adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
528adfc5217SJeff Kirsher sbmac_mii_senddata(sbm_mdio, regval, 16);
529adfc5217SJeff Kirsher
530adfc5217SJeff Kirsher mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
531adfc5217SJeff Kirsher
532adfc5217SJeff Kirsher __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
533adfc5217SJeff Kirsher
534adfc5217SJeff Kirsher return 0;
535adfc5217SJeff Kirsher }
536adfc5217SJeff Kirsher
537adfc5217SJeff Kirsher
538adfc5217SJeff Kirsher
539adfc5217SJeff Kirsher /**********************************************************************
540adfc5217SJeff Kirsher * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
541adfc5217SJeff Kirsher *
542adfc5217SJeff Kirsher * Initialize a DMA channel context. Since there are potentially
543adfc5217SJeff Kirsher * eight DMA channels per MAC, it's nice to do this in a standard
544adfc5217SJeff Kirsher * way.
545adfc5217SJeff Kirsher *
546adfc5217SJeff Kirsher * Input parameters:
547adfc5217SJeff Kirsher * d - struct sbmacdma (DMA channel context)
548adfc5217SJeff Kirsher * s - struct sbmac_softc (pointer to a MAC)
549adfc5217SJeff Kirsher * chan - channel number (0..1 right now)
550adfc5217SJeff Kirsher * txrx - Identifies DMA_TX or DMA_RX for channel direction
551adfc5217SJeff Kirsher * maxdescr - number of descriptors
552adfc5217SJeff Kirsher *
553adfc5217SJeff Kirsher * Return value:
554adfc5217SJeff Kirsher * nothing
555adfc5217SJeff Kirsher ********************************************************************* */
556adfc5217SJeff Kirsher
sbdma_initctx(struct sbmacdma * d,struct sbmac_softc * s,int chan,int txrx,int maxdescr)557adfc5217SJeff Kirsher static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
558adfc5217SJeff Kirsher int txrx, int maxdescr)
559adfc5217SJeff Kirsher {
560adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
561adfc5217SJeff Kirsher int int_pktcnt, int_timeout;
562adfc5217SJeff Kirsher #endif
563adfc5217SJeff Kirsher
564adfc5217SJeff Kirsher /*
565adfc5217SJeff Kirsher * Save away interesting stuff in the structure
566adfc5217SJeff Kirsher */
567adfc5217SJeff Kirsher
568adfc5217SJeff Kirsher d->sbdma_eth = s;
569adfc5217SJeff Kirsher d->sbdma_channel = chan;
570adfc5217SJeff Kirsher d->sbdma_txdir = txrx;
571adfc5217SJeff Kirsher
572adfc5217SJeff Kirsher #if 0
573adfc5217SJeff Kirsher /* RMON clearing */
574adfc5217SJeff Kirsher s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
575adfc5217SJeff Kirsher #endif
576adfc5217SJeff Kirsher
577adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
578adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
579adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
580adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
581adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
582adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
583adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
584adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
585adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
586adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
587adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
588adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
589adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
590adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
591adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
592adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
593adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
594adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
595adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
596adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
597adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
598adfc5217SJeff Kirsher
599adfc5217SJeff Kirsher /*
600adfc5217SJeff Kirsher * initialize register pointers
601adfc5217SJeff Kirsher */
602adfc5217SJeff Kirsher
603adfc5217SJeff Kirsher d->sbdma_config0 =
604adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
605adfc5217SJeff Kirsher d->sbdma_config1 =
606adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
607adfc5217SJeff Kirsher d->sbdma_dscrbase =
608adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
609adfc5217SJeff Kirsher d->sbdma_dscrcnt =
610adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
611adfc5217SJeff Kirsher d->sbdma_curdscr =
612adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
613adfc5217SJeff Kirsher if (d->sbdma_txdir)
614adfc5217SJeff Kirsher d->sbdma_oodpktlost = NULL;
615adfc5217SJeff Kirsher else
616adfc5217SJeff Kirsher d->sbdma_oodpktlost =
617adfc5217SJeff Kirsher s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
618adfc5217SJeff Kirsher
619adfc5217SJeff Kirsher /*
620adfc5217SJeff Kirsher * Allocate memory for the ring
621adfc5217SJeff Kirsher */
622adfc5217SJeff Kirsher
623adfc5217SJeff Kirsher d->sbdma_maxdescr = maxdescr;
624adfc5217SJeff Kirsher
625adfc5217SJeff Kirsher d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
626adfc5217SJeff Kirsher sizeof(*d->sbdma_dscrtable),
627adfc5217SJeff Kirsher GFP_KERNEL);
628adfc5217SJeff Kirsher
629adfc5217SJeff Kirsher /*
630adfc5217SJeff Kirsher * The descriptor table must be aligned to at least 16 bytes or the
631adfc5217SJeff Kirsher * MAC will corrupt it.
632adfc5217SJeff Kirsher */
633adfc5217SJeff Kirsher d->sbdma_dscrtable = (struct sbdmadscr *)
634adfc5217SJeff Kirsher ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
635adfc5217SJeff Kirsher sizeof(*d->sbdma_dscrtable));
636adfc5217SJeff Kirsher
637adfc5217SJeff Kirsher d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
638adfc5217SJeff Kirsher
639adfc5217SJeff Kirsher d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
640adfc5217SJeff Kirsher
641adfc5217SJeff Kirsher /*
642adfc5217SJeff Kirsher * And context table
643adfc5217SJeff Kirsher */
644adfc5217SJeff Kirsher
645adfc5217SJeff Kirsher d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
646adfc5217SJeff Kirsher sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
647adfc5217SJeff Kirsher
648adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
649adfc5217SJeff Kirsher /*
650adfc5217SJeff Kirsher * Setup Rx/Tx DMA coalescing defaults
651adfc5217SJeff Kirsher */
652adfc5217SJeff Kirsher
653adfc5217SJeff Kirsher int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
654adfc5217SJeff Kirsher if ( int_pktcnt ) {
655adfc5217SJeff Kirsher d->sbdma_int_pktcnt = int_pktcnt;
656adfc5217SJeff Kirsher } else {
657adfc5217SJeff Kirsher d->sbdma_int_pktcnt = 1;
658adfc5217SJeff Kirsher }
659adfc5217SJeff Kirsher
660adfc5217SJeff Kirsher int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
661adfc5217SJeff Kirsher if ( int_timeout ) {
662adfc5217SJeff Kirsher d->sbdma_int_timeout = int_timeout;
663adfc5217SJeff Kirsher } else {
664adfc5217SJeff Kirsher d->sbdma_int_timeout = 0;
665adfc5217SJeff Kirsher }
666adfc5217SJeff Kirsher #endif
667adfc5217SJeff Kirsher
668adfc5217SJeff Kirsher }
669adfc5217SJeff Kirsher
670adfc5217SJeff Kirsher /**********************************************************************
671adfc5217SJeff Kirsher * SBDMA_CHANNEL_START(d)
672adfc5217SJeff Kirsher *
673adfc5217SJeff Kirsher * Initialize the hardware registers for a DMA channel.
674adfc5217SJeff Kirsher *
675adfc5217SJeff Kirsher * Input parameters:
676adfc5217SJeff Kirsher * d - DMA channel to init (context must be previously init'd
677adfc5217SJeff Kirsher * rxtx - DMA_RX or DMA_TX depending on what type of channel
678adfc5217SJeff Kirsher *
679adfc5217SJeff Kirsher * Return value:
680adfc5217SJeff Kirsher * nothing
681adfc5217SJeff Kirsher ********************************************************************* */
682adfc5217SJeff Kirsher
sbdma_channel_start(struct sbmacdma * d,int rxtx)683adfc5217SJeff Kirsher static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
684adfc5217SJeff Kirsher {
685adfc5217SJeff Kirsher /*
686adfc5217SJeff Kirsher * Turn on the DMA channel
687adfc5217SJeff Kirsher */
688adfc5217SJeff Kirsher
689adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
690adfc5217SJeff Kirsher __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
691adfc5217SJeff Kirsher 0, d->sbdma_config1);
692adfc5217SJeff Kirsher __raw_writeq(M_DMA_EOP_INT_EN |
693adfc5217SJeff Kirsher V_DMA_RINGSZ(d->sbdma_maxdescr) |
694adfc5217SJeff Kirsher V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
695adfc5217SJeff Kirsher 0, d->sbdma_config0);
696adfc5217SJeff Kirsher #else
697adfc5217SJeff Kirsher __raw_writeq(0, d->sbdma_config1);
698adfc5217SJeff Kirsher __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
699adfc5217SJeff Kirsher 0, d->sbdma_config0);
700adfc5217SJeff Kirsher #endif
701adfc5217SJeff Kirsher
702adfc5217SJeff Kirsher __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
703adfc5217SJeff Kirsher
704adfc5217SJeff Kirsher /*
705adfc5217SJeff Kirsher * Initialize ring pointers
706adfc5217SJeff Kirsher */
707adfc5217SJeff Kirsher
708adfc5217SJeff Kirsher d->sbdma_addptr = d->sbdma_dscrtable;
709adfc5217SJeff Kirsher d->sbdma_remptr = d->sbdma_dscrtable;
710adfc5217SJeff Kirsher }
711adfc5217SJeff Kirsher
712adfc5217SJeff Kirsher /**********************************************************************
713adfc5217SJeff Kirsher * SBDMA_CHANNEL_STOP(d)
714adfc5217SJeff Kirsher *
715adfc5217SJeff Kirsher * Initialize the hardware registers for a DMA channel.
716adfc5217SJeff Kirsher *
717adfc5217SJeff Kirsher * Input parameters:
718adfc5217SJeff Kirsher * d - DMA channel to init (context must be previously init'd
719adfc5217SJeff Kirsher *
720adfc5217SJeff Kirsher * Return value:
721adfc5217SJeff Kirsher * nothing
722adfc5217SJeff Kirsher ********************************************************************* */
723adfc5217SJeff Kirsher
sbdma_channel_stop(struct sbmacdma * d)724adfc5217SJeff Kirsher static void sbdma_channel_stop(struct sbmacdma *d)
725adfc5217SJeff Kirsher {
726adfc5217SJeff Kirsher /*
727adfc5217SJeff Kirsher * Turn off the DMA channel
728adfc5217SJeff Kirsher */
729adfc5217SJeff Kirsher
730adfc5217SJeff Kirsher __raw_writeq(0, d->sbdma_config1);
731adfc5217SJeff Kirsher
732adfc5217SJeff Kirsher __raw_writeq(0, d->sbdma_dscrbase);
733adfc5217SJeff Kirsher
734adfc5217SJeff Kirsher __raw_writeq(0, d->sbdma_config0);
735adfc5217SJeff Kirsher
736adfc5217SJeff Kirsher /*
737adfc5217SJeff Kirsher * Zero ring pointers
738adfc5217SJeff Kirsher */
739adfc5217SJeff Kirsher
740adfc5217SJeff Kirsher d->sbdma_addptr = NULL;
741adfc5217SJeff Kirsher d->sbdma_remptr = NULL;
742adfc5217SJeff Kirsher }
743adfc5217SJeff Kirsher
sbdma_align_skb(struct sk_buff * skb,unsigned int power2,unsigned int offset)744adfc5217SJeff Kirsher static inline void sbdma_align_skb(struct sk_buff *skb,
745adfc5217SJeff Kirsher unsigned int power2, unsigned int offset)
746adfc5217SJeff Kirsher {
747adfc5217SJeff Kirsher unsigned char *addr = skb->data;
748adfc5217SJeff Kirsher unsigned char *newaddr = PTR_ALIGN(addr, power2);
749adfc5217SJeff Kirsher
750adfc5217SJeff Kirsher skb_reserve(skb, newaddr - addr + offset);
751adfc5217SJeff Kirsher }
752adfc5217SJeff Kirsher
753adfc5217SJeff Kirsher
754adfc5217SJeff Kirsher /**********************************************************************
755adfc5217SJeff Kirsher * SBDMA_ADD_RCVBUFFER(d,sb)
756adfc5217SJeff Kirsher *
757adfc5217SJeff Kirsher * Add a buffer to the specified DMA channel. For receive channels,
758adfc5217SJeff Kirsher * this queues a buffer for inbound packets.
759adfc5217SJeff Kirsher *
760adfc5217SJeff Kirsher * Input parameters:
761adfc5217SJeff Kirsher * sc - softc structure
762adfc5217SJeff Kirsher * d - DMA channel descriptor
763adfc5217SJeff Kirsher * sb - sk_buff to add, or NULL if we should allocate one
764adfc5217SJeff Kirsher *
765adfc5217SJeff Kirsher * Return value:
766adfc5217SJeff Kirsher * 0 if buffer could not be added (ring is full)
767adfc5217SJeff Kirsher * 1 if buffer added successfully
768adfc5217SJeff Kirsher ********************************************************************* */
769adfc5217SJeff Kirsher
770adfc5217SJeff Kirsher
sbdma_add_rcvbuffer(struct sbmac_softc * sc,struct sbmacdma * d,struct sk_buff * sb)771adfc5217SJeff Kirsher static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
772adfc5217SJeff Kirsher struct sk_buff *sb)
773adfc5217SJeff Kirsher {
774adfc5217SJeff Kirsher struct net_device *dev = sc->sbm_dev;
775adfc5217SJeff Kirsher struct sbdmadscr *dsc;
776adfc5217SJeff Kirsher struct sbdmadscr *nextdsc;
777adfc5217SJeff Kirsher struct sk_buff *sb_new = NULL;
778adfc5217SJeff Kirsher int pktsize = ENET_PACKET_SIZE;
779adfc5217SJeff Kirsher
780adfc5217SJeff Kirsher /* get pointer to our current place in the ring */
781adfc5217SJeff Kirsher
782adfc5217SJeff Kirsher dsc = d->sbdma_addptr;
783adfc5217SJeff Kirsher nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
784adfc5217SJeff Kirsher
785adfc5217SJeff Kirsher /*
786adfc5217SJeff Kirsher * figure out if the ring is full - if the next descriptor
787adfc5217SJeff Kirsher * is the same as the one that we're going to remove from
788adfc5217SJeff Kirsher * the ring, the ring is full
789adfc5217SJeff Kirsher */
790adfc5217SJeff Kirsher
791adfc5217SJeff Kirsher if (nextdsc == d->sbdma_remptr) {
792adfc5217SJeff Kirsher return -ENOSPC;
793adfc5217SJeff Kirsher }
794adfc5217SJeff Kirsher
795adfc5217SJeff Kirsher /*
796adfc5217SJeff Kirsher * Allocate a sk_buff if we don't already have one.
797adfc5217SJeff Kirsher * If we do have an sk_buff, reset it so that it's empty.
798adfc5217SJeff Kirsher *
799adfc5217SJeff Kirsher * Note: sk_buffs don't seem to be guaranteed to have any sort
800adfc5217SJeff Kirsher * of alignment when they are allocated. Therefore, allocate enough
801adfc5217SJeff Kirsher * extra space to make sure that:
802adfc5217SJeff Kirsher *
803adfc5217SJeff Kirsher * 1. the data does not start in the middle of a cache line.
804adfc5217SJeff Kirsher * 2. The data does not end in the middle of a cache line
805adfc5217SJeff Kirsher * 3. The buffer can be aligned such that the IP addresses are
806adfc5217SJeff Kirsher * naturally aligned.
807adfc5217SJeff Kirsher *
808adfc5217SJeff Kirsher * Remember, the SOCs MAC writes whole cache lines at a time,
809adfc5217SJeff Kirsher * without reading the old contents first. So, if the sk_buff's
810adfc5217SJeff Kirsher * data portion starts in the middle of a cache line, the SOC
811adfc5217SJeff Kirsher * DMA will trash the beginning (and ending) portions.
812adfc5217SJeff Kirsher */
813adfc5217SJeff Kirsher
814adfc5217SJeff Kirsher if (sb == NULL) {
815adfc5217SJeff Kirsher sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
816adfc5217SJeff Kirsher SMP_CACHE_BYTES * 2 +
817adfc5217SJeff Kirsher NET_IP_ALIGN);
818720a43efSJoe Perches if (sb_new == NULL)
819adfc5217SJeff Kirsher return -ENOBUFS;
820adfc5217SJeff Kirsher
821adfc5217SJeff Kirsher sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
822adfc5217SJeff Kirsher }
823adfc5217SJeff Kirsher else {
824adfc5217SJeff Kirsher sb_new = sb;
825adfc5217SJeff Kirsher /*
826adfc5217SJeff Kirsher * nothing special to reinit buffer, it's already aligned
827adfc5217SJeff Kirsher * and sb->data already points to a good place.
828adfc5217SJeff Kirsher */
829adfc5217SJeff Kirsher }
830adfc5217SJeff Kirsher
831adfc5217SJeff Kirsher /*
832adfc5217SJeff Kirsher * fill in the descriptor
833adfc5217SJeff Kirsher */
834adfc5217SJeff Kirsher
835adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
836adfc5217SJeff Kirsher /*
837adfc5217SJeff Kirsher * Do not interrupt per DMA transfer.
838adfc5217SJeff Kirsher */
839adfc5217SJeff Kirsher dsc->dscr_a = virt_to_phys(sb_new->data) |
840adfc5217SJeff Kirsher V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
841adfc5217SJeff Kirsher #else
842adfc5217SJeff Kirsher dsc->dscr_a = virt_to_phys(sb_new->data) |
843adfc5217SJeff Kirsher V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
844adfc5217SJeff Kirsher M_DMA_DSCRA_INTERRUPT;
845adfc5217SJeff Kirsher #endif
846adfc5217SJeff Kirsher
847adfc5217SJeff Kirsher /* receiving: no options */
848adfc5217SJeff Kirsher dsc->dscr_b = 0;
849adfc5217SJeff Kirsher
850adfc5217SJeff Kirsher /*
851adfc5217SJeff Kirsher * fill in the context
852adfc5217SJeff Kirsher */
853adfc5217SJeff Kirsher
854adfc5217SJeff Kirsher d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
855adfc5217SJeff Kirsher
856adfc5217SJeff Kirsher /*
857adfc5217SJeff Kirsher * point at next packet
858adfc5217SJeff Kirsher */
859adfc5217SJeff Kirsher
860adfc5217SJeff Kirsher d->sbdma_addptr = nextdsc;
861adfc5217SJeff Kirsher
862adfc5217SJeff Kirsher /*
863adfc5217SJeff Kirsher * Give the buffer to the DMA engine.
864adfc5217SJeff Kirsher */
865adfc5217SJeff Kirsher
866adfc5217SJeff Kirsher __raw_writeq(1, d->sbdma_dscrcnt);
867adfc5217SJeff Kirsher
868adfc5217SJeff Kirsher return 0; /* we did it */
869adfc5217SJeff Kirsher }
870adfc5217SJeff Kirsher
871adfc5217SJeff Kirsher /**********************************************************************
872adfc5217SJeff Kirsher * SBDMA_ADD_TXBUFFER(d,sb)
873adfc5217SJeff Kirsher *
874adfc5217SJeff Kirsher * Add a transmit buffer to the specified DMA channel, causing a
875adfc5217SJeff Kirsher * transmit to start.
876adfc5217SJeff Kirsher *
877adfc5217SJeff Kirsher * Input parameters:
878adfc5217SJeff Kirsher * d - DMA channel descriptor
879adfc5217SJeff Kirsher * sb - sk_buff to add
880adfc5217SJeff Kirsher *
881adfc5217SJeff Kirsher * Return value:
882adfc5217SJeff Kirsher * 0 transmit queued successfully
883adfc5217SJeff Kirsher * otherwise error code
884adfc5217SJeff Kirsher ********************************************************************* */
885adfc5217SJeff Kirsher
886adfc5217SJeff Kirsher
sbdma_add_txbuffer(struct sbmacdma * d,struct sk_buff * sb)887adfc5217SJeff Kirsher static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
888adfc5217SJeff Kirsher {
889adfc5217SJeff Kirsher struct sbdmadscr *dsc;
890adfc5217SJeff Kirsher struct sbdmadscr *nextdsc;
891adfc5217SJeff Kirsher uint64_t phys;
892adfc5217SJeff Kirsher uint64_t ncb;
893adfc5217SJeff Kirsher int length;
894adfc5217SJeff Kirsher
895adfc5217SJeff Kirsher /* get pointer to our current place in the ring */
896adfc5217SJeff Kirsher
897adfc5217SJeff Kirsher dsc = d->sbdma_addptr;
898adfc5217SJeff Kirsher nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
899adfc5217SJeff Kirsher
900adfc5217SJeff Kirsher /*
901adfc5217SJeff Kirsher * figure out if the ring is full - if the next descriptor
902adfc5217SJeff Kirsher * is the same as the one that we're going to remove from
903adfc5217SJeff Kirsher * the ring, the ring is full
904adfc5217SJeff Kirsher */
905adfc5217SJeff Kirsher
906adfc5217SJeff Kirsher if (nextdsc == d->sbdma_remptr) {
907adfc5217SJeff Kirsher return -ENOSPC;
908adfc5217SJeff Kirsher }
909adfc5217SJeff Kirsher
910adfc5217SJeff Kirsher /*
911adfc5217SJeff Kirsher * Under Linux, it's not necessary to copy/coalesce buffers
912adfc5217SJeff Kirsher * like it is on NetBSD. We think they're all contiguous,
913adfc5217SJeff Kirsher * but that may not be true for GBE.
914adfc5217SJeff Kirsher */
915adfc5217SJeff Kirsher
916adfc5217SJeff Kirsher length = sb->len;
917adfc5217SJeff Kirsher
918adfc5217SJeff Kirsher /*
919adfc5217SJeff Kirsher * fill in the descriptor. Note that the number of cache
920adfc5217SJeff Kirsher * blocks in the descriptor is the number of blocks
921adfc5217SJeff Kirsher * *spanned*, so we need to add in the offset (if any)
922adfc5217SJeff Kirsher * while doing the calculation.
923adfc5217SJeff Kirsher */
924adfc5217SJeff Kirsher
925adfc5217SJeff Kirsher phys = virt_to_phys(sb->data);
926adfc5217SJeff Kirsher ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
927adfc5217SJeff Kirsher
928adfc5217SJeff Kirsher dsc->dscr_a = phys |
929adfc5217SJeff Kirsher V_DMA_DSCRA_A_SIZE(ncb) |
930adfc5217SJeff Kirsher #ifndef CONFIG_SBMAC_COALESCE
931adfc5217SJeff Kirsher M_DMA_DSCRA_INTERRUPT |
932adfc5217SJeff Kirsher #endif
933adfc5217SJeff Kirsher M_DMA_ETHTX_SOP;
934adfc5217SJeff Kirsher
935adfc5217SJeff Kirsher /* transmitting: set outbound options and length */
936adfc5217SJeff Kirsher
937adfc5217SJeff Kirsher dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
938adfc5217SJeff Kirsher V_DMA_DSCRB_PKT_SIZE(length);
939adfc5217SJeff Kirsher
940adfc5217SJeff Kirsher /*
941adfc5217SJeff Kirsher * fill in the context
942adfc5217SJeff Kirsher */
943adfc5217SJeff Kirsher
944adfc5217SJeff Kirsher d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
945adfc5217SJeff Kirsher
946adfc5217SJeff Kirsher /*
947adfc5217SJeff Kirsher * point at next packet
948adfc5217SJeff Kirsher */
949adfc5217SJeff Kirsher
950adfc5217SJeff Kirsher d->sbdma_addptr = nextdsc;
951adfc5217SJeff Kirsher
952adfc5217SJeff Kirsher /*
953adfc5217SJeff Kirsher * Give the buffer to the DMA engine.
954adfc5217SJeff Kirsher */
955adfc5217SJeff Kirsher
956adfc5217SJeff Kirsher __raw_writeq(1, d->sbdma_dscrcnt);
957adfc5217SJeff Kirsher
958adfc5217SJeff Kirsher return 0; /* we did it */
959adfc5217SJeff Kirsher }
960adfc5217SJeff Kirsher
961adfc5217SJeff Kirsher
962adfc5217SJeff Kirsher
963adfc5217SJeff Kirsher
964adfc5217SJeff Kirsher /**********************************************************************
965adfc5217SJeff Kirsher * SBDMA_EMPTYRING(d)
966adfc5217SJeff Kirsher *
967adfc5217SJeff Kirsher * Free all allocated sk_buffs on the specified DMA channel;
968adfc5217SJeff Kirsher *
969adfc5217SJeff Kirsher * Input parameters:
970adfc5217SJeff Kirsher * d - DMA channel
971adfc5217SJeff Kirsher *
972adfc5217SJeff Kirsher * Return value:
973adfc5217SJeff Kirsher * nothing
974adfc5217SJeff Kirsher ********************************************************************* */
975adfc5217SJeff Kirsher
sbdma_emptyring(struct sbmacdma * d)976adfc5217SJeff Kirsher static void sbdma_emptyring(struct sbmacdma *d)
977adfc5217SJeff Kirsher {
978adfc5217SJeff Kirsher int idx;
979adfc5217SJeff Kirsher struct sk_buff *sb;
980adfc5217SJeff Kirsher
981adfc5217SJeff Kirsher for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
982adfc5217SJeff Kirsher sb = d->sbdma_ctxtable[idx];
983adfc5217SJeff Kirsher if (sb) {
984adfc5217SJeff Kirsher dev_kfree_skb(sb);
985adfc5217SJeff Kirsher d->sbdma_ctxtable[idx] = NULL;
986adfc5217SJeff Kirsher }
987adfc5217SJeff Kirsher }
988adfc5217SJeff Kirsher }
989adfc5217SJeff Kirsher
990adfc5217SJeff Kirsher
991adfc5217SJeff Kirsher /**********************************************************************
992adfc5217SJeff Kirsher * SBDMA_FILLRING(d)
993adfc5217SJeff Kirsher *
994adfc5217SJeff Kirsher * Fill the specified DMA channel (must be receive channel)
995adfc5217SJeff Kirsher * with sk_buffs
996adfc5217SJeff Kirsher *
997adfc5217SJeff Kirsher * Input parameters:
998adfc5217SJeff Kirsher * sc - softc structure
999adfc5217SJeff Kirsher * d - DMA channel
1000adfc5217SJeff Kirsher *
1001adfc5217SJeff Kirsher * Return value:
1002adfc5217SJeff Kirsher * nothing
1003adfc5217SJeff Kirsher ********************************************************************* */
1004adfc5217SJeff Kirsher
sbdma_fillring(struct sbmac_softc * sc,struct sbmacdma * d)1005adfc5217SJeff Kirsher static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
1006adfc5217SJeff Kirsher {
1007adfc5217SJeff Kirsher int idx;
1008adfc5217SJeff Kirsher
1009adfc5217SJeff Kirsher for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
1010adfc5217SJeff Kirsher if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
1011adfc5217SJeff Kirsher break;
1012adfc5217SJeff Kirsher }
1013adfc5217SJeff Kirsher }
1014adfc5217SJeff Kirsher
1015adfc5217SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
sbmac_netpoll(struct net_device * netdev)1016adfc5217SJeff Kirsher static void sbmac_netpoll(struct net_device *netdev)
1017adfc5217SJeff Kirsher {
1018adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(netdev);
1019adfc5217SJeff Kirsher int irq = sc->sbm_dev->irq;
1020adfc5217SJeff Kirsher
1021adfc5217SJeff Kirsher __raw_writeq(0, sc->sbm_imr);
1022adfc5217SJeff Kirsher
1023adfc5217SJeff Kirsher sbmac_intr(irq, netdev);
1024adfc5217SJeff Kirsher
1025adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
1026adfc5217SJeff Kirsher __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1027adfc5217SJeff Kirsher ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
1028adfc5217SJeff Kirsher sc->sbm_imr);
1029adfc5217SJeff Kirsher #else
1030adfc5217SJeff Kirsher __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1031adfc5217SJeff Kirsher (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
1032adfc5217SJeff Kirsher #endif
1033adfc5217SJeff Kirsher }
1034adfc5217SJeff Kirsher #endif
1035adfc5217SJeff Kirsher
1036adfc5217SJeff Kirsher /**********************************************************************
1037adfc5217SJeff Kirsher * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
1038adfc5217SJeff Kirsher *
1039adfc5217SJeff Kirsher * Process "completed" receive buffers on the specified DMA channel.
1040adfc5217SJeff Kirsher *
1041adfc5217SJeff Kirsher * Input parameters:
1042adfc5217SJeff Kirsher * sc - softc structure
1043adfc5217SJeff Kirsher * d - DMA channel context
1044adfc5217SJeff Kirsher * work_to_do - no. of packets to process before enabling interrupt
1045adfc5217SJeff Kirsher * again (for NAPI)
1046adfc5217SJeff Kirsher * poll - 1: using polling (for NAPI)
1047adfc5217SJeff Kirsher *
1048adfc5217SJeff Kirsher * Return value:
1049adfc5217SJeff Kirsher * nothing
1050adfc5217SJeff Kirsher ********************************************************************* */
1051adfc5217SJeff Kirsher
sbdma_rx_process(struct sbmac_softc * sc,struct sbmacdma * d,int work_to_do,int poll)1052adfc5217SJeff Kirsher static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1053adfc5217SJeff Kirsher int work_to_do, int poll)
1054adfc5217SJeff Kirsher {
1055adfc5217SJeff Kirsher struct net_device *dev = sc->sbm_dev;
1056adfc5217SJeff Kirsher int curidx;
1057adfc5217SJeff Kirsher int hwidx;
1058adfc5217SJeff Kirsher struct sbdmadscr *dsc;
1059adfc5217SJeff Kirsher struct sk_buff *sb;
1060adfc5217SJeff Kirsher int len;
1061adfc5217SJeff Kirsher int work_done = 0;
1062adfc5217SJeff Kirsher int dropped = 0;
1063adfc5217SJeff Kirsher
1064adfc5217SJeff Kirsher prefetch(d);
1065adfc5217SJeff Kirsher
1066adfc5217SJeff Kirsher again:
1067adfc5217SJeff Kirsher /* Check if the HW dropped any frames */
1068adfc5217SJeff Kirsher dev->stats.rx_fifo_errors
1069adfc5217SJeff Kirsher += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
1070adfc5217SJeff Kirsher __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
1071adfc5217SJeff Kirsher
1072adfc5217SJeff Kirsher while (work_to_do-- > 0) {
1073adfc5217SJeff Kirsher /*
1074adfc5217SJeff Kirsher * figure out where we are (as an index) and where
1075adfc5217SJeff Kirsher * the hardware is (also as an index)
1076adfc5217SJeff Kirsher *
1077adfc5217SJeff Kirsher * This could be done faster if (for example) the
1078adfc5217SJeff Kirsher * descriptor table was page-aligned and contiguous in
1079adfc5217SJeff Kirsher * both virtual and physical memory -- you could then
1080adfc5217SJeff Kirsher * just compare the low-order bits of the virtual address
1081adfc5217SJeff Kirsher * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1082adfc5217SJeff Kirsher */
1083adfc5217SJeff Kirsher
1084adfc5217SJeff Kirsher dsc = d->sbdma_remptr;
1085adfc5217SJeff Kirsher curidx = dsc - d->sbdma_dscrtable;
1086adfc5217SJeff Kirsher
1087adfc5217SJeff Kirsher prefetch(dsc);
1088adfc5217SJeff Kirsher prefetch(&d->sbdma_ctxtable[curidx]);
1089adfc5217SJeff Kirsher
1090adfc5217SJeff Kirsher hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1091adfc5217SJeff Kirsher d->sbdma_dscrtable_phys) /
1092adfc5217SJeff Kirsher sizeof(*d->sbdma_dscrtable);
1093adfc5217SJeff Kirsher
1094adfc5217SJeff Kirsher /*
1095adfc5217SJeff Kirsher * If they're the same, that means we've processed all
1096adfc5217SJeff Kirsher * of the descriptors up to (but not including) the one that
1097adfc5217SJeff Kirsher * the hardware is working on right now.
1098adfc5217SJeff Kirsher */
1099adfc5217SJeff Kirsher
1100adfc5217SJeff Kirsher if (curidx == hwidx)
1101adfc5217SJeff Kirsher goto done;
1102adfc5217SJeff Kirsher
1103adfc5217SJeff Kirsher /*
1104adfc5217SJeff Kirsher * Otherwise, get the packet's sk_buff ptr back
1105adfc5217SJeff Kirsher */
1106adfc5217SJeff Kirsher
1107adfc5217SJeff Kirsher sb = d->sbdma_ctxtable[curidx];
1108adfc5217SJeff Kirsher d->sbdma_ctxtable[curidx] = NULL;
1109adfc5217SJeff Kirsher
1110adfc5217SJeff Kirsher len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
1111adfc5217SJeff Kirsher
1112adfc5217SJeff Kirsher /*
1113adfc5217SJeff Kirsher * Check packet status. If good, process it.
1114adfc5217SJeff Kirsher * If not, silently drop it and put it back on the
1115adfc5217SJeff Kirsher * receive ring.
1116adfc5217SJeff Kirsher */
1117adfc5217SJeff Kirsher
1118adfc5217SJeff Kirsher if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
1119adfc5217SJeff Kirsher
1120adfc5217SJeff Kirsher /*
1121adfc5217SJeff Kirsher * Add a new buffer to replace the old one. If we fail
1122adfc5217SJeff Kirsher * to allocate a buffer, we're going to drop this
1123adfc5217SJeff Kirsher * packet and put it right back on the receive ring.
1124adfc5217SJeff Kirsher */
1125adfc5217SJeff Kirsher
1126adfc5217SJeff Kirsher if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
1127adfc5217SJeff Kirsher -ENOBUFS)) {
1128adfc5217SJeff Kirsher dev->stats.rx_dropped++;
1129adfc5217SJeff Kirsher /* Re-add old buffer */
1130adfc5217SJeff Kirsher sbdma_add_rcvbuffer(sc, d, sb);
1131adfc5217SJeff Kirsher /* No point in continuing at the moment */
1132adfc5217SJeff Kirsher printk(KERN_ERR "dropped packet (1)\n");
1133adfc5217SJeff Kirsher d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1134adfc5217SJeff Kirsher goto done;
1135adfc5217SJeff Kirsher } else {
1136adfc5217SJeff Kirsher /*
1137adfc5217SJeff Kirsher * Set length into the packet
1138adfc5217SJeff Kirsher */
1139adfc5217SJeff Kirsher skb_put(sb,len);
1140adfc5217SJeff Kirsher
1141adfc5217SJeff Kirsher /*
1142adfc5217SJeff Kirsher * Buffer has been replaced on the
1143adfc5217SJeff Kirsher * receive ring. Pass the buffer to
1144adfc5217SJeff Kirsher * the kernel
1145adfc5217SJeff Kirsher */
1146adfc5217SJeff Kirsher sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
1147adfc5217SJeff Kirsher /* Check hw IPv4/TCP checksum if supported */
1148adfc5217SJeff Kirsher if (sc->rx_hw_checksum == ENABLE) {
1149adfc5217SJeff Kirsher if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
1150adfc5217SJeff Kirsher !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
1151adfc5217SJeff Kirsher sb->ip_summed = CHECKSUM_UNNECESSARY;
1152adfc5217SJeff Kirsher /* don't need to set sb->csum */
1153adfc5217SJeff Kirsher } else {
1154adfc5217SJeff Kirsher skb_checksum_none_assert(sb);
1155adfc5217SJeff Kirsher }
1156adfc5217SJeff Kirsher }
1157adfc5217SJeff Kirsher prefetch(sb->data);
1158adfc5217SJeff Kirsher prefetch((const void *)(((char *)sb->data)+32));
1159adfc5217SJeff Kirsher if (poll)
1160adfc5217SJeff Kirsher dropped = netif_receive_skb(sb);
1161adfc5217SJeff Kirsher else
1162adfc5217SJeff Kirsher dropped = netif_rx(sb);
1163adfc5217SJeff Kirsher
1164adfc5217SJeff Kirsher if (dropped == NET_RX_DROP) {
1165adfc5217SJeff Kirsher dev->stats.rx_dropped++;
1166adfc5217SJeff Kirsher d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1167adfc5217SJeff Kirsher goto done;
1168adfc5217SJeff Kirsher }
1169adfc5217SJeff Kirsher else {
1170adfc5217SJeff Kirsher dev->stats.rx_bytes += len;
1171adfc5217SJeff Kirsher dev->stats.rx_packets++;
1172adfc5217SJeff Kirsher }
1173adfc5217SJeff Kirsher }
1174adfc5217SJeff Kirsher } else {
1175adfc5217SJeff Kirsher /*
1176adfc5217SJeff Kirsher * Packet was mangled somehow. Just drop it and
1177adfc5217SJeff Kirsher * put it back on the receive ring.
1178adfc5217SJeff Kirsher */
1179adfc5217SJeff Kirsher dev->stats.rx_errors++;
1180adfc5217SJeff Kirsher sbdma_add_rcvbuffer(sc, d, sb);
1181adfc5217SJeff Kirsher }
1182adfc5217SJeff Kirsher
1183adfc5217SJeff Kirsher
1184adfc5217SJeff Kirsher /*
1185adfc5217SJeff Kirsher * .. and advance to the next buffer.
1186adfc5217SJeff Kirsher */
1187adfc5217SJeff Kirsher
1188adfc5217SJeff Kirsher d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1189adfc5217SJeff Kirsher work_done++;
1190adfc5217SJeff Kirsher }
1191adfc5217SJeff Kirsher if (!poll) {
1192adfc5217SJeff Kirsher work_to_do = 32;
1193adfc5217SJeff Kirsher goto again; /* collect fifo drop statistics again */
1194adfc5217SJeff Kirsher }
1195adfc5217SJeff Kirsher done:
1196adfc5217SJeff Kirsher return work_done;
1197adfc5217SJeff Kirsher }
1198adfc5217SJeff Kirsher
1199adfc5217SJeff Kirsher /**********************************************************************
1200adfc5217SJeff Kirsher * SBDMA_TX_PROCESS(sc,d)
1201adfc5217SJeff Kirsher *
1202adfc5217SJeff Kirsher * Process "completed" transmit buffers on the specified DMA channel.
1203adfc5217SJeff Kirsher * This is normally called within the interrupt service routine.
1204adfc5217SJeff Kirsher * Note that this isn't really ideal for priority channels, since
1205adfc5217SJeff Kirsher * it processes all of the packets on a given channel before
1206adfc5217SJeff Kirsher * returning.
1207adfc5217SJeff Kirsher *
1208adfc5217SJeff Kirsher * Input parameters:
1209adfc5217SJeff Kirsher * sc - softc structure
1210adfc5217SJeff Kirsher * d - DMA channel context
1211adfc5217SJeff Kirsher * poll - 1: using polling (for NAPI)
1212adfc5217SJeff Kirsher *
1213adfc5217SJeff Kirsher * Return value:
1214adfc5217SJeff Kirsher * nothing
1215adfc5217SJeff Kirsher ********************************************************************* */
1216adfc5217SJeff Kirsher
sbdma_tx_process(struct sbmac_softc * sc,struct sbmacdma * d,int poll)1217adfc5217SJeff Kirsher static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
1218adfc5217SJeff Kirsher int poll)
1219adfc5217SJeff Kirsher {
1220adfc5217SJeff Kirsher struct net_device *dev = sc->sbm_dev;
1221adfc5217SJeff Kirsher int curidx;
1222adfc5217SJeff Kirsher int hwidx;
1223adfc5217SJeff Kirsher struct sbdmadscr *dsc;
1224adfc5217SJeff Kirsher struct sk_buff *sb;
1225adfc5217SJeff Kirsher unsigned long flags;
1226adfc5217SJeff Kirsher int packets_handled = 0;
1227adfc5217SJeff Kirsher
1228adfc5217SJeff Kirsher spin_lock_irqsave(&(sc->sbm_lock), flags);
1229adfc5217SJeff Kirsher
1230adfc5217SJeff Kirsher if (d->sbdma_remptr == d->sbdma_addptr)
1231adfc5217SJeff Kirsher goto end_unlock;
1232adfc5217SJeff Kirsher
1233adfc5217SJeff Kirsher hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
1234adfc5217SJeff Kirsher d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
1235adfc5217SJeff Kirsher
1236adfc5217SJeff Kirsher for (;;) {
1237adfc5217SJeff Kirsher /*
1238adfc5217SJeff Kirsher * figure out where we are (as an index) and where
1239adfc5217SJeff Kirsher * the hardware is (also as an index)
1240adfc5217SJeff Kirsher *
1241adfc5217SJeff Kirsher * This could be done faster if (for example) the
1242adfc5217SJeff Kirsher * descriptor table was page-aligned and contiguous in
1243adfc5217SJeff Kirsher * both virtual and physical memory -- you could then
1244adfc5217SJeff Kirsher * just compare the low-order bits of the virtual address
1245adfc5217SJeff Kirsher * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
1246adfc5217SJeff Kirsher */
1247adfc5217SJeff Kirsher
1248adfc5217SJeff Kirsher curidx = d->sbdma_remptr - d->sbdma_dscrtable;
1249adfc5217SJeff Kirsher
1250adfc5217SJeff Kirsher /*
1251adfc5217SJeff Kirsher * If they're the same, that means we've processed all
1252adfc5217SJeff Kirsher * of the descriptors up to (but not including) the one that
1253adfc5217SJeff Kirsher * the hardware is working on right now.
1254adfc5217SJeff Kirsher */
1255adfc5217SJeff Kirsher
1256adfc5217SJeff Kirsher if (curidx == hwidx)
1257adfc5217SJeff Kirsher break;
1258adfc5217SJeff Kirsher
1259adfc5217SJeff Kirsher /*
1260adfc5217SJeff Kirsher * Otherwise, get the packet's sk_buff ptr back
1261adfc5217SJeff Kirsher */
1262adfc5217SJeff Kirsher
1263adfc5217SJeff Kirsher dsc = &(d->sbdma_dscrtable[curidx]);
1264adfc5217SJeff Kirsher sb = d->sbdma_ctxtable[curidx];
1265adfc5217SJeff Kirsher d->sbdma_ctxtable[curidx] = NULL;
1266adfc5217SJeff Kirsher
1267adfc5217SJeff Kirsher /*
1268adfc5217SJeff Kirsher * Stats
1269adfc5217SJeff Kirsher */
1270adfc5217SJeff Kirsher
1271adfc5217SJeff Kirsher dev->stats.tx_bytes += sb->len;
1272adfc5217SJeff Kirsher dev->stats.tx_packets++;
1273adfc5217SJeff Kirsher
1274adfc5217SJeff Kirsher /*
1275adfc5217SJeff Kirsher * for transmits, we just free buffers.
1276adfc5217SJeff Kirsher */
1277adfc5217SJeff Kirsher
1278d2901b07SYang Wei dev_consume_skb_irq(sb);
1279adfc5217SJeff Kirsher
1280adfc5217SJeff Kirsher /*
1281adfc5217SJeff Kirsher * .. and advance to the next buffer.
1282adfc5217SJeff Kirsher */
1283adfc5217SJeff Kirsher
1284adfc5217SJeff Kirsher d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
1285adfc5217SJeff Kirsher
1286adfc5217SJeff Kirsher packets_handled++;
1287adfc5217SJeff Kirsher
1288adfc5217SJeff Kirsher }
1289adfc5217SJeff Kirsher
1290adfc5217SJeff Kirsher /*
1291adfc5217SJeff Kirsher * Decide if we should wake up the protocol or not.
1292adfc5217SJeff Kirsher * Other drivers seem to do this when we reach a low
1293adfc5217SJeff Kirsher * watermark on the transmit queue.
1294adfc5217SJeff Kirsher */
1295adfc5217SJeff Kirsher
1296adfc5217SJeff Kirsher if (packets_handled)
1297adfc5217SJeff Kirsher netif_wake_queue(d->sbdma_eth->sbm_dev);
1298adfc5217SJeff Kirsher
1299adfc5217SJeff Kirsher end_unlock:
1300adfc5217SJeff Kirsher spin_unlock_irqrestore(&(sc->sbm_lock), flags);
1301adfc5217SJeff Kirsher
1302adfc5217SJeff Kirsher }
1303adfc5217SJeff Kirsher
1304adfc5217SJeff Kirsher
1305adfc5217SJeff Kirsher
1306adfc5217SJeff Kirsher /**********************************************************************
1307adfc5217SJeff Kirsher * SBMAC_INITCTX(s)
1308adfc5217SJeff Kirsher *
1309adfc5217SJeff Kirsher * Initialize an Ethernet context structure - this is called
1310adfc5217SJeff Kirsher * once per MAC on the 1250. Memory is allocated here, so don't
1311adfc5217SJeff Kirsher * call it again from inside the ioctl routines that bring the
1312adfc5217SJeff Kirsher * interface up/down
1313adfc5217SJeff Kirsher *
1314adfc5217SJeff Kirsher * Input parameters:
1315adfc5217SJeff Kirsher * s - sbmac context structure
1316adfc5217SJeff Kirsher *
1317adfc5217SJeff Kirsher * Return value:
1318adfc5217SJeff Kirsher * 0
1319adfc5217SJeff Kirsher ********************************************************************* */
1320adfc5217SJeff Kirsher
sbmac_initctx(struct sbmac_softc * s)1321adfc5217SJeff Kirsher static int sbmac_initctx(struct sbmac_softc *s)
1322adfc5217SJeff Kirsher {
1323adfc5217SJeff Kirsher
1324adfc5217SJeff Kirsher /*
1325adfc5217SJeff Kirsher * figure out the addresses of some ports
1326adfc5217SJeff Kirsher */
1327adfc5217SJeff Kirsher
1328adfc5217SJeff Kirsher s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
1329adfc5217SJeff Kirsher s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
1330adfc5217SJeff Kirsher s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
1331adfc5217SJeff Kirsher s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
1332adfc5217SJeff Kirsher s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
1333adfc5217SJeff Kirsher s->sbm_isr = s->sbm_base + R_MAC_STATUS;
1334adfc5217SJeff Kirsher s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
1335adfc5217SJeff Kirsher s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
1336adfc5217SJeff Kirsher
1337adfc5217SJeff Kirsher /*
1338adfc5217SJeff Kirsher * Initialize the DMA channels. Right now, only one per MAC is used
1339adfc5217SJeff Kirsher * Note: Only do this _once_, as it allocates memory from the kernel!
1340adfc5217SJeff Kirsher */
1341adfc5217SJeff Kirsher
1342adfc5217SJeff Kirsher sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
1343adfc5217SJeff Kirsher sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
1344adfc5217SJeff Kirsher
1345adfc5217SJeff Kirsher /*
1346adfc5217SJeff Kirsher * initial state is OFF
1347adfc5217SJeff Kirsher */
1348adfc5217SJeff Kirsher
1349adfc5217SJeff Kirsher s->sbm_state = sbmac_state_off;
1350adfc5217SJeff Kirsher
1351adfc5217SJeff Kirsher return 0;
1352adfc5217SJeff Kirsher }
1353adfc5217SJeff Kirsher
1354adfc5217SJeff Kirsher
sbdma_uninitctx(struct sbmacdma * d)1355adfc5217SJeff Kirsher static void sbdma_uninitctx(struct sbmacdma *d)
1356adfc5217SJeff Kirsher {
1357adfc5217SJeff Kirsher kfree(d->sbdma_dscrtable_unaligned);
1358adfc5217SJeff Kirsher d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
1359adfc5217SJeff Kirsher
1360adfc5217SJeff Kirsher kfree(d->sbdma_ctxtable);
1361adfc5217SJeff Kirsher d->sbdma_ctxtable = NULL;
1362adfc5217SJeff Kirsher }
1363adfc5217SJeff Kirsher
1364adfc5217SJeff Kirsher
sbmac_uninitctx(struct sbmac_softc * sc)1365adfc5217SJeff Kirsher static void sbmac_uninitctx(struct sbmac_softc *sc)
1366adfc5217SJeff Kirsher {
1367adfc5217SJeff Kirsher sbdma_uninitctx(&(sc->sbm_txdma));
1368adfc5217SJeff Kirsher sbdma_uninitctx(&(sc->sbm_rxdma));
1369adfc5217SJeff Kirsher }
1370adfc5217SJeff Kirsher
1371adfc5217SJeff Kirsher
1372adfc5217SJeff Kirsher /**********************************************************************
1373adfc5217SJeff Kirsher * SBMAC_CHANNEL_START(s)
1374adfc5217SJeff Kirsher *
1375adfc5217SJeff Kirsher * Start packet processing on this MAC.
1376adfc5217SJeff Kirsher *
1377adfc5217SJeff Kirsher * Input parameters:
1378adfc5217SJeff Kirsher * s - sbmac structure
1379adfc5217SJeff Kirsher *
1380adfc5217SJeff Kirsher * Return value:
1381adfc5217SJeff Kirsher * nothing
1382adfc5217SJeff Kirsher ********************************************************************* */
1383adfc5217SJeff Kirsher
sbmac_channel_start(struct sbmac_softc * s)1384adfc5217SJeff Kirsher static void sbmac_channel_start(struct sbmac_softc *s)
1385adfc5217SJeff Kirsher {
1386adfc5217SJeff Kirsher uint64_t reg;
1387adfc5217SJeff Kirsher void __iomem *port;
1388adfc5217SJeff Kirsher uint64_t cfg,fifo,framecfg;
1389adfc5217SJeff Kirsher int idx, th_value;
1390adfc5217SJeff Kirsher
1391adfc5217SJeff Kirsher /*
1392adfc5217SJeff Kirsher * Don't do this if running
1393adfc5217SJeff Kirsher */
1394adfc5217SJeff Kirsher
1395adfc5217SJeff Kirsher if (s->sbm_state == sbmac_state_on)
1396adfc5217SJeff Kirsher return;
1397adfc5217SJeff Kirsher
1398adfc5217SJeff Kirsher /*
1399adfc5217SJeff Kirsher * Bring the controller out of reset, but leave it off.
1400adfc5217SJeff Kirsher */
1401adfc5217SJeff Kirsher
1402adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_macenable);
1403adfc5217SJeff Kirsher
1404adfc5217SJeff Kirsher /*
1405adfc5217SJeff Kirsher * Ignore all received packets
1406adfc5217SJeff Kirsher */
1407adfc5217SJeff Kirsher
1408adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_rxfilter);
1409adfc5217SJeff Kirsher
1410adfc5217SJeff Kirsher /*
1411adfc5217SJeff Kirsher * Calculate values for various control registers.
1412adfc5217SJeff Kirsher */
1413adfc5217SJeff Kirsher
1414adfc5217SJeff Kirsher cfg = M_MAC_RETRY_EN |
1415adfc5217SJeff Kirsher M_MAC_TX_HOLD_SOP_EN |
1416adfc5217SJeff Kirsher V_MAC_TX_PAUSE_CNT_16K |
1417adfc5217SJeff Kirsher M_MAC_AP_STAT_EN |
1418adfc5217SJeff Kirsher M_MAC_FAST_SYNC |
1419adfc5217SJeff Kirsher M_MAC_SS_EN |
1420adfc5217SJeff Kirsher 0;
1421adfc5217SJeff Kirsher
1422adfc5217SJeff Kirsher /*
1423adfc5217SJeff Kirsher * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
1424adfc5217SJeff Kirsher * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
1425adfc5217SJeff Kirsher * Use a larger RD_THRSH for gigabit
1426adfc5217SJeff Kirsher */
1427adfc5217SJeff Kirsher if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
1428adfc5217SJeff Kirsher th_value = 28;
1429adfc5217SJeff Kirsher else
1430adfc5217SJeff Kirsher th_value = 64;
1431adfc5217SJeff Kirsher
1432adfc5217SJeff Kirsher fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
1433adfc5217SJeff Kirsher ((s->sbm_speed == sbmac_speed_1000)
1434adfc5217SJeff Kirsher ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
1435adfc5217SJeff Kirsher V_MAC_TX_RL_THRSH(4) |
1436adfc5217SJeff Kirsher V_MAC_RX_PL_THRSH(4) |
1437adfc5217SJeff Kirsher V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
1438adfc5217SJeff Kirsher V_MAC_RX_RL_THRSH(8) |
1439adfc5217SJeff Kirsher 0;
1440adfc5217SJeff Kirsher
1441adfc5217SJeff Kirsher framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
1442adfc5217SJeff Kirsher V_MAC_MAX_FRAMESZ_DEFAULT |
1443adfc5217SJeff Kirsher V_MAC_BACKOFF_SEL(1);
1444adfc5217SJeff Kirsher
1445adfc5217SJeff Kirsher /*
1446adfc5217SJeff Kirsher * Clear out the hash address map
1447adfc5217SJeff Kirsher */
1448adfc5217SJeff Kirsher
1449adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_HASH_BASE;
1450adfc5217SJeff Kirsher for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
1451adfc5217SJeff Kirsher __raw_writeq(0, port);
1452adfc5217SJeff Kirsher port += sizeof(uint64_t);
1453adfc5217SJeff Kirsher }
1454adfc5217SJeff Kirsher
1455adfc5217SJeff Kirsher /*
1456adfc5217SJeff Kirsher * Clear out the exact-match table
1457adfc5217SJeff Kirsher */
1458adfc5217SJeff Kirsher
1459adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_ADDR_BASE;
1460adfc5217SJeff Kirsher for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
1461adfc5217SJeff Kirsher __raw_writeq(0, port);
1462adfc5217SJeff Kirsher port += sizeof(uint64_t);
1463adfc5217SJeff Kirsher }
1464adfc5217SJeff Kirsher
1465adfc5217SJeff Kirsher /*
1466adfc5217SJeff Kirsher * Clear out the DMA Channel mapping table registers
1467adfc5217SJeff Kirsher */
1468adfc5217SJeff Kirsher
1469adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_CHUP0_BASE;
1470adfc5217SJeff Kirsher for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1471adfc5217SJeff Kirsher __raw_writeq(0, port);
1472adfc5217SJeff Kirsher port += sizeof(uint64_t);
1473adfc5217SJeff Kirsher }
1474adfc5217SJeff Kirsher
1475adfc5217SJeff Kirsher
1476adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_CHLO0_BASE;
1477adfc5217SJeff Kirsher for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
1478adfc5217SJeff Kirsher __raw_writeq(0, port);
1479adfc5217SJeff Kirsher port += sizeof(uint64_t);
1480adfc5217SJeff Kirsher }
1481adfc5217SJeff Kirsher
1482adfc5217SJeff Kirsher /*
1483adfc5217SJeff Kirsher * Program the hardware address. It goes into the hardware-address
1484adfc5217SJeff Kirsher * register as well as the first filter register.
1485adfc5217SJeff Kirsher */
1486adfc5217SJeff Kirsher
1487adfc5217SJeff Kirsher reg = sbmac_addr2reg(s->sbm_hwaddr);
1488adfc5217SJeff Kirsher
1489adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_ADDR_BASE;
1490adfc5217SJeff Kirsher __raw_writeq(reg, port);
1491adfc5217SJeff Kirsher port = s->sbm_base + R_MAC_ETHERNET_ADDR;
1492adfc5217SJeff Kirsher
1493adfc5217SJeff Kirsher __raw_writeq(reg, port);
1494adfc5217SJeff Kirsher
1495adfc5217SJeff Kirsher /*
1496adfc5217SJeff Kirsher * Set the receive filter for no packets, and write values
1497adfc5217SJeff Kirsher * to the various config registers
1498adfc5217SJeff Kirsher */
1499adfc5217SJeff Kirsher
1500adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_rxfilter);
1501adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_imr);
1502adfc5217SJeff Kirsher __raw_writeq(framecfg, s->sbm_framecfg);
1503adfc5217SJeff Kirsher __raw_writeq(fifo, s->sbm_fifocfg);
1504adfc5217SJeff Kirsher __raw_writeq(cfg, s->sbm_maccfg);
1505adfc5217SJeff Kirsher
1506adfc5217SJeff Kirsher /*
1507adfc5217SJeff Kirsher * Initialize DMA channels (rings should be ok now)
1508adfc5217SJeff Kirsher */
1509adfc5217SJeff Kirsher
1510adfc5217SJeff Kirsher sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
1511adfc5217SJeff Kirsher sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
1512adfc5217SJeff Kirsher
1513adfc5217SJeff Kirsher /*
1514adfc5217SJeff Kirsher * Configure the speed, duplex, and flow control
1515adfc5217SJeff Kirsher */
1516adfc5217SJeff Kirsher
1517adfc5217SJeff Kirsher sbmac_set_speed(s,s->sbm_speed);
1518adfc5217SJeff Kirsher sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
1519adfc5217SJeff Kirsher
1520adfc5217SJeff Kirsher /*
1521adfc5217SJeff Kirsher * Fill the receive ring
1522adfc5217SJeff Kirsher */
1523adfc5217SJeff Kirsher
1524adfc5217SJeff Kirsher sbdma_fillring(s, &(s->sbm_rxdma));
1525adfc5217SJeff Kirsher
1526adfc5217SJeff Kirsher /*
1527adfc5217SJeff Kirsher * Turn on the rest of the bits in the enable register
1528adfc5217SJeff Kirsher */
1529adfc5217SJeff Kirsher
1530*c34ce279SLukas Bulwahn #if defined(CONFIG_SIBYTE_BCM1x80)
1531adfc5217SJeff Kirsher __raw_writeq(M_MAC_RXDMA_EN0 |
1532adfc5217SJeff Kirsher M_MAC_TXDMA_EN0, s->sbm_macenable);
1533adfc5217SJeff Kirsher #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
1534adfc5217SJeff Kirsher __raw_writeq(M_MAC_RXDMA_EN0 |
1535adfc5217SJeff Kirsher M_MAC_TXDMA_EN0 |
1536adfc5217SJeff Kirsher M_MAC_RX_ENABLE |
1537adfc5217SJeff Kirsher M_MAC_TX_ENABLE, s->sbm_macenable);
1538adfc5217SJeff Kirsher #else
1539adfc5217SJeff Kirsher #error invalid SiByte MAC configuration
1540adfc5217SJeff Kirsher #endif
1541adfc5217SJeff Kirsher
1542adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
1543adfc5217SJeff Kirsher __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
1544adfc5217SJeff Kirsher ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
1545adfc5217SJeff Kirsher #else
1546adfc5217SJeff Kirsher __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
1547adfc5217SJeff Kirsher (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
1548adfc5217SJeff Kirsher #endif
1549adfc5217SJeff Kirsher
1550adfc5217SJeff Kirsher /*
1551adfc5217SJeff Kirsher * Enable receiving unicasts and broadcasts
1552adfc5217SJeff Kirsher */
1553adfc5217SJeff Kirsher
1554adfc5217SJeff Kirsher __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
1555adfc5217SJeff Kirsher
1556adfc5217SJeff Kirsher /*
1557adfc5217SJeff Kirsher * we're running now.
1558adfc5217SJeff Kirsher */
1559adfc5217SJeff Kirsher
1560adfc5217SJeff Kirsher s->sbm_state = sbmac_state_on;
1561adfc5217SJeff Kirsher
1562adfc5217SJeff Kirsher /*
1563adfc5217SJeff Kirsher * Program multicast addresses
1564adfc5217SJeff Kirsher */
1565adfc5217SJeff Kirsher
1566adfc5217SJeff Kirsher sbmac_setmulti(s);
1567adfc5217SJeff Kirsher
1568adfc5217SJeff Kirsher /*
1569adfc5217SJeff Kirsher * If channel was in promiscuous mode before, turn that on
1570adfc5217SJeff Kirsher */
1571adfc5217SJeff Kirsher
1572adfc5217SJeff Kirsher if (s->sbm_devflags & IFF_PROMISC) {
1573adfc5217SJeff Kirsher sbmac_promiscuous_mode(s,1);
1574adfc5217SJeff Kirsher }
1575adfc5217SJeff Kirsher
1576adfc5217SJeff Kirsher }
1577adfc5217SJeff Kirsher
1578adfc5217SJeff Kirsher
1579adfc5217SJeff Kirsher /**********************************************************************
1580adfc5217SJeff Kirsher * SBMAC_CHANNEL_STOP(s)
1581adfc5217SJeff Kirsher *
1582adfc5217SJeff Kirsher * Stop packet processing on this MAC.
1583adfc5217SJeff Kirsher *
1584adfc5217SJeff Kirsher * Input parameters:
1585adfc5217SJeff Kirsher * s - sbmac structure
1586adfc5217SJeff Kirsher *
1587adfc5217SJeff Kirsher * Return value:
1588adfc5217SJeff Kirsher * nothing
1589adfc5217SJeff Kirsher ********************************************************************* */
1590adfc5217SJeff Kirsher
sbmac_channel_stop(struct sbmac_softc * s)1591adfc5217SJeff Kirsher static void sbmac_channel_stop(struct sbmac_softc *s)
1592adfc5217SJeff Kirsher {
1593adfc5217SJeff Kirsher /* don't do this if already stopped */
1594adfc5217SJeff Kirsher
1595adfc5217SJeff Kirsher if (s->sbm_state == sbmac_state_off)
1596adfc5217SJeff Kirsher return;
1597adfc5217SJeff Kirsher
1598adfc5217SJeff Kirsher /* don't accept any packets, disable all interrupts */
1599adfc5217SJeff Kirsher
1600adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_rxfilter);
1601adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_imr);
1602adfc5217SJeff Kirsher
1603adfc5217SJeff Kirsher /* Turn off ticker */
1604adfc5217SJeff Kirsher
1605adfc5217SJeff Kirsher /* XXX */
1606adfc5217SJeff Kirsher
1607adfc5217SJeff Kirsher /* turn off receiver and transmitter */
1608adfc5217SJeff Kirsher
1609adfc5217SJeff Kirsher __raw_writeq(0, s->sbm_macenable);
1610adfc5217SJeff Kirsher
1611adfc5217SJeff Kirsher /* We're stopped now. */
1612adfc5217SJeff Kirsher
1613adfc5217SJeff Kirsher s->sbm_state = sbmac_state_off;
1614adfc5217SJeff Kirsher
1615adfc5217SJeff Kirsher /*
1616adfc5217SJeff Kirsher * Stop DMA channels (rings should be ok now)
1617adfc5217SJeff Kirsher */
1618adfc5217SJeff Kirsher
1619adfc5217SJeff Kirsher sbdma_channel_stop(&(s->sbm_rxdma));
1620adfc5217SJeff Kirsher sbdma_channel_stop(&(s->sbm_txdma));
1621adfc5217SJeff Kirsher
1622adfc5217SJeff Kirsher /* Empty the receive and transmit rings */
1623adfc5217SJeff Kirsher
1624adfc5217SJeff Kirsher sbdma_emptyring(&(s->sbm_rxdma));
1625adfc5217SJeff Kirsher sbdma_emptyring(&(s->sbm_txdma));
1626adfc5217SJeff Kirsher
1627adfc5217SJeff Kirsher }
1628adfc5217SJeff Kirsher
1629adfc5217SJeff Kirsher /**********************************************************************
1630adfc5217SJeff Kirsher * SBMAC_SET_CHANNEL_STATE(state)
1631adfc5217SJeff Kirsher *
1632adfc5217SJeff Kirsher * Set the channel's state ON or OFF
1633adfc5217SJeff Kirsher *
1634adfc5217SJeff Kirsher * Input parameters:
1635adfc5217SJeff Kirsher * state - new state
1636adfc5217SJeff Kirsher *
1637adfc5217SJeff Kirsher * Return value:
1638adfc5217SJeff Kirsher * old state
1639adfc5217SJeff Kirsher ********************************************************************* */
sbmac_set_channel_state(struct sbmac_softc * sc,enum sbmac_state state)1640adfc5217SJeff Kirsher static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
1641adfc5217SJeff Kirsher enum sbmac_state state)
1642adfc5217SJeff Kirsher {
1643adfc5217SJeff Kirsher enum sbmac_state oldstate = sc->sbm_state;
1644adfc5217SJeff Kirsher
1645adfc5217SJeff Kirsher /*
1646adfc5217SJeff Kirsher * If same as previous state, return
1647adfc5217SJeff Kirsher */
1648adfc5217SJeff Kirsher
1649adfc5217SJeff Kirsher if (state == oldstate) {
1650adfc5217SJeff Kirsher return oldstate;
1651adfc5217SJeff Kirsher }
1652adfc5217SJeff Kirsher
1653adfc5217SJeff Kirsher /*
1654adfc5217SJeff Kirsher * If new state is ON, turn channel on
1655adfc5217SJeff Kirsher */
1656adfc5217SJeff Kirsher
1657adfc5217SJeff Kirsher if (state == sbmac_state_on) {
1658adfc5217SJeff Kirsher sbmac_channel_start(sc);
1659adfc5217SJeff Kirsher }
1660adfc5217SJeff Kirsher else {
1661adfc5217SJeff Kirsher sbmac_channel_stop(sc);
1662adfc5217SJeff Kirsher }
1663adfc5217SJeff Kirsher
1664adfc5217SJeff Kirsher /*
1665adfc5217SJeff Kirsher * Return previous state
1666adfc5217SJeff Kirsher */
1667adfc5217SJeff Kirsher
1668adfc5217SJeff Kirsher return oldstate;
1669adfc5217SJeff Kirsher }
1670adfc5217SJeff Kirsher
1671adfc5217SJeff Kirsher
1672adfc5217SJeff Kirsher /**********************************************************************
1673adfc5217SJeff Kirsher * SBMAC_PROMISCUOUS_MODE(sc,onoff)
1674adfc5217SJeff Kirsher *
1675adfc5217SJeff Kirsher * Turn on or off promiscuous mode
1676adfc5217SJeff Kirsher *
1677adfc5217SJeff Kirsher * Input parameters:
1678adfc5217SJeff Kirsher * sc - softc
1679adfc5217SJeff Kirsher * onoff - 1 to turn on, 0 to turn off
1680adfc5217SJeff Kirsher *
1681adfc5217SJeff Kirsher * Return value:
1682adfc5217SJeff Kirsher * nothing
1683adfc5217SJeff Kirsher ********************************************************************* */
1684adfc5217SJeff Kirsher
sbmac_promiscuous_mode(struct sbmac_softc * sc,int onoff)1685adfc5217SJeff Kirsher static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
1686adfc5217SJeff Kirsher {
1687adfc5217SJeff Kirsher uint64_t reg;
1688adfc5217SJeff Kirsher
1689adfc5217SJeff Kirsher if (sc->sbm_state != sbmac_state_on)
1690adfc5217SJeff Kirsher return;
1691adfc5217SJeff Kirsher
1692adfc5217SJeff Kirsher if (onoff) {
1693adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
1694adfc5217SJeff Kirsher reg |= M_MAC_ALLPKT_EN;
1695adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
1696adfc5217SJeff Kirsher }
1697adfc5217SJeff Kirsher else {
1698adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
1699adfc5217SJeff Kirsher reg &= ~M_MAC_ALLPKT_EN;
1700adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
1701adfc5217SJeff Kirsher }
1702adfc5217SJeff Kirsher }
1703adfc5217SJeff Kirsher
1704adfc5217SJeff Kirsher /**********************************************************************
1705adfc5217SJeff Kirsher * SBMAC_SETIPHDR_OFFSET(sc,onoff)
1706adfc5217SJeff Kirsher *
1707adfc5217SJeff Kirsher * Set the iphdr offset as 15 assuming ethernet encapsulation
1708adfc5217SJeff Kirsher *
1709adfc5217SJeff Kirsher * Input parameters:
1710adfc5217SJeff Kirsher * sc - softc
1711adfc5217SJeff Kirsher *
1712adfc5217SJeff Kirsher * Return value:
1713adfc5217SJeff Kirsher * nothing
1714adfc5217SJeff Kirsher ********************************************************************* */
1715adfc5217SJeff Kirsher
sbmac_set_iphdr_offset(struct sbmac_softc * sc)1716adfc5217SJeff Kirsher static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
1717adfc5217SJeff Kirsher {
1718adfc5217SJeff Kirsher uint64_t reg;
1719adfc5217SJeff Kirsher
1720adfc5217SJeff Kirsher /* Hard code the off set to 15 for now */
1721adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
1722adfc5217SJeff Kirsher reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
1723adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
1724adfc5217SJeff Kirsher
1725adfc5217SJeff Kirsher /* BCM1250 pass1 didn't have hardware checksum. Everything
1726adfc5217SJeff Kirsher later does. */
1727adfc5217SJeff Kirsher if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
1728adfc5217SJeff Kirsher sc->rx_hw_checksum = DISABLE;
1729adfc5217SJeff Kirsher } else {
1730adfc5217SJeff Kirsher sc->rx_hw_checksum = ENABLE;
1731adfc5217SJeff Kirsher }
1732adfc5217SJeff Kirsher }
1733adfc5217SJeff Kirsher
1734adfc5217SJeff Kirsher
1735adfc5217SJeff Kirsher /**********************************************************************
1736adfc5217SJeff Kirsher * SBMAC_ADDR2REG(ptr)
1737adfc5217SJeff Kirsher *
1738adfc5217SJeff Kirsher * Convert six bytes into the 64-bit register value that
1739adfc5217SJeff Kirsher * we typically write into the SBMAC's address/mcast registers
1740adfc5217SJeff Kirsher *
1741adfc5217SJeff Kirsher * Input parameters:
1742adfc5217SJeff Kirsher * ptr - pointer to 6 bytes
1743adfc5217SJeff Kirsher *
1744adfc5217SJeff Kirsher * Return value:
1745adfc5217SJeff Kirsher * register value
1746adfc5217SJeff Kirsher ********************************************************************* */
1747adfc5217SJeff Kirsher
sbmac_addr2reg(unsigned char * ptr)1748adfc5217SJeff Kirsher static uint64_t sbmac_addr2reg(unsigned char *ptr)
1749adfc5217SJeff Kirsher {
1750adfc5217SJeff Kirsher uint64_t reg = 0;
1751adfc5217SJeff Kirsher
1752adfc5217SJeff Kirsher ptr += 6;
1753adfc5217SJeff Kirsher
1754adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1755adfc5217SJeff Kirsher reg <<= 8;
1756adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1757adfc5217SJeff Kirsher reg <<= 8;
1758adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1759adfc5217SJeff Kirsher reg <<= 8;
1760adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1761adfc5217SJeff Kirsher reg <<= 8;
1762adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1763adfc5217SJeff Kirsher reg <<= 8;
1764adfc5217SJeff Kirsher reg |= (uint64_t) *(--ptr);
1765adfc5217SJeff Kirsher
1766adfc5217SJeff Kirsher return reg;
1767adfc5217SJeff Kirsher }
1768adfc5217SJeff Kirsher
1769adfc5217SJeff Kirsher
1770adfc5217SJeff Kirsher /**********************************************************************
1771adfc5217SJeff Kirsher * SBMAC_SET_SPEED(s,speed)
1772adfc5217SJeff Kirsher *
1773adfc5217SJeff Kirsher * Configure LAN speed for the specified MAC.
1774adfc5217SJeff Kirsher * Warning: must be called when MAC is off!
1775adfc5217SJeff Kirsher *
1776adfc5217SJeff Kirsher * Input parameters:
1777adfc5217SJeff Kirsher * s - sbmac structure
1778adfc5217SJeff Kirsher * speed - speed to set MAC to (see enum sbmac_speed)
1779adfc5217SJeff Kirsher *
1780adfc5217SJeff Kirsher * Return value:
1781adfc5217SJeff Kirsher * 1 if successful
1782adfc5217SJeff Kirsher * 0 indicates invalid parameters
1783adfc5217SJeff Kirsher ********************************************************************* */
1784adfc5217SJeff Kirsher
sbmac_set_speed(struct sbmac_softc * s,enum sbmac_speed speed)1785adfc5217SJeff Kirsher static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
1786adfc5217SJeff Kirsher {
1787adfc5217SJeff Kirsher uint64_t cfg;
1788adfc5217SJeff Kirsher uint64_t framecfg;
1789adfc5217SJeff Kirsher
1790adfc5217SJeff Kirsher /*
1791adfc5217SJeff Kirsher * Save new current values
1792adfc5217SJeff Kirsher */
1793adfc5217SJeff Kirsher
1794adfc5217SJeff Kirsher s->sbm_speed = speed;
1795adfc5217SJeff Kirsher
1796adfc5217SJeff Kirsher if (s->sbm_state == sbmac_state_on)
1797adfc5217SJeff Kirsher return 0; /* save for next restart */
1798adfc5217SJeff Kirsher
1799adfc5217SJeff Kirsher /*
1800adfc5217SJeff Kirsher * Read current register values
1801adfc5217SJeff Kirsher */
1802adfc5217SJeff Kirsher
1803adfc5217SJeff Kirsher cfg = __raw_readq(s->sbm_maccfg);
1804adfc5217SJeff Kirsher framecfg = __raw_readq(s->sbm_framecfg);
1805adfc5217SJeff Kirsher
1806adfc5217SJeff Kirsher /*
1807adfc5217SJeff Kirsher * Mask out the stuff we want to change
1808adfc5217SJeff Kirsher */
1809adfc5217SJeff Kirsher
1810adfc5217SJeff Kirsher cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
1811adfc5217SJeff Kirsher framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
1812adfc5217SJeff Kirsher M_MAC_SLOT_SIZE);
1813adfc5217SJeff Kirsher
1814adfc5217SJeff Kirsher /*
1815adfc5217SJeff Kirsher * Now add in the new bits
1816adfc5217SJeff Kirsher */
1817adfc5217SJeff Kirsher
1818adfc5217SJeff Kirsher switch (speed) {
1819adfc5217SJeff Kirsher case sbmac_speed_10:
1820adfc5217SJeff Kirsher framecfg |= V_MAC_IFG_RX_10 |
1821adfc5217SJeff Kirsher V_MAC_IFG_TX_10 |
1822adfc5217SJeff Kirsher K_MAC_IFG_THRSH_10 |
1823adfc5217SJeff Kirsher V_MAC_SLOT_SIZE_10;
1824adfc5217SJeff Kirsher cfg |= V_MAC_SPEED_SEL_10MBPS;
1825adfc5217SJeff Kirsher break;
1826adfc5217SJeff Kirsher
1827adfc5217SJeff Kirsher case sbmac_speed_100:
1828adfc5217SJeff Kirsher framecfg |= V_MAC_IFG_RX_100 |
1829adfc5217SJeff Kirsher V_MAC_IFG_TX_100 |
1830adfc5217SJeff Kirsher V_MAC_IFG_THRSH_100 |
1831adfc5217SJeff Kirsher V_MAC_SLOT_SIZE_100;
1832adfc5217SJeff Kirsher cfg |= V_MAC_SPEED_SEL_100MBPS ;
1833adfc5217SJeff Kirsher break;
1834adfc5217SJeff Kirsher
1835adfc5217SJeff Kirsher case sbmac_speed_1000:
1836adfc5217SJeff Kirsher framecfg |= V_MAC_IFG_RX_1000 |
1837adfc5217SJeff Kirsher V_MAC_IFG_TX_1000 |
1838adfc5217SJeff Kirsher V_MAC_IFG_THRSH_1000 |
1839adfc5217SJeff Kirsher V_MAC_SLOT_SIZE_1000;
1840adfc5217SJeff Kirsher cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
1841adfc5217SJeff Kirsher break;
1842adfc5217SJeff Kirsher
1843adfc5217SJeff Kirsher default:
1844adfc5217SJeff Kirsher return 0;
1845adfc5217SJeff Kirsher }
1846adfc5217SJeff Kirsher
1847adfc5217SJeff Kirsher /*
1848adfc5217SJeff Kirsher * Send the bits back to the hardware
1849adfc5217SJeff Kirsher */
1850adfc5217SJeff Kirsher
1851adfc5217SJeff Kirsher __raw_writeq(framecfg, s->sbm_framecfg);
1852adfc5217SJeff Kirsher __raw_writeq(cfg, s->sbm_maccfg);
1853adfc5217SJeff Kirsher
1854adfc5217SJeff Kirsher return 1;
1855adfc5217SJeff Kirsher }
1856adfc5217SJeff Kirsher
1857adfc5217SJeff Kirsher /**********************************************************************
1858adfc5217SJeff Kirsher * SBMAC_SET_DUPLEX(s,duplex,fc)
1859adfc5217SJeff Kirsher *
1860adfc5217SJeff Kirsher * Set Ethernet duplex and flow control options for this MAC
1861adfc5217SJeff Kirsher * Warning: must be called when MAC is off!
1862adfc5217SJeff Kirsher *
1863adfc5217SJeff Kirsher * Input parameters:
1864adfc5217SJeff Kirsher * s - sbmac structure
1865adfc5217SJeff Kirsher * duplex - duplex setting (see enum sbmac_duplex)
1866adfc5217SJeff Kirsher * fc - flow control setting (see enum sbmac_fc)
1867adfc5217SJeff Kirsher *
1868adfc5217SJeff Kirsher * Return value:
1869adfc5217SJeff Kirsher * 1 if ok
1870adfc5217SJeff Kirsher * 0 if an invalid parameter combination was specified
1871adfc5217SJeff Kirsher ********************************************************************* */
1872adfc5217SJeff Kirsher
sbmac_set_duplex(struct sbmac_softc * s,enum sbmac_duplex duplex,enum sbmac_fc fc)1873adfc5217SJeff Kirsher static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
1874adfc5217SJeff Kirsher enum sbmac_fc fc)
1875adfc5217SJeff Kirsher {
1876adfc5217SJeff Kirsher uint64_t cfg;
1877adfc5217SJeff Kirsher
1878adfc5217SJeff Kirsher /*
1879adfc5217SJeff Kirsher * Save new current values
1880adfc5217SJeff Kirsher */
1881adfc5217SJeff Kirsher
1882adfc5217SJeff Kirsher s->sbm_duplex = duplex;
1883adfc5217SJeff Kirsher s->sbm_fc = fc;
1884adfc5217SJeff Kirsher
1885adfc5217SJeff Kirsher if (s->sbm_state == sbmac_state_on)
1886adfc5217SJeff Kirsher return 0; /* save for next restart */
1887adfc5217SJeff Kirsher
1888adfc5217SJeff Kirsher /*
1889adfc5217SJeff Kirsher * Read current register values
1890adfc5217SJeff Kirsher */
1891adfc5217SJeff Kirsher
1892adfc5217SJeff Kirsher cfg = __raw_readq(s->sbm_maccfg);
1893adfc5217SJeff Kirsher
1894adfc5217SJeff Kirsher /*
1895adfc5217SJeff Kirsher * Mask off the stuff we're about to change
1896adfc5217SJeff Kirsher */
1897adfc5217SJeff Kirsher
1898adfc5217SJeff Kirsher cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
1899adfc5217SJeff Kirsher
1900adfc5217SJeff Kirsher
1901adfc5217SJeff Kirsher switch (duplex) {
1902adfc5217SJeff Kirsher case sbmac_duplex_half:
1903adfc5217SJeff Kirsher switch (fc) {
1904adfc5217SJeff Kirsher case sbmac_fc_disabled:
1905adfc5217SJeff Kirsher cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
1906adfc5217SJeff Kirsher break;
1907adfc5217SJeff Kirsher
1908adfc5217SJeff Kirsher case sbmac_fc_collision:
1909adfc5217SJeff Kirsher cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
1910adfc5217SJeff Kirsher break;
1911adfc5217SJeff Kirsher
1912adfc5217SJeff Kirsher case sbmac_fc_carrier:
1913adfc5217SJeff Kirsher cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
1914adfc5217SJeff Kirsher break;
1915adfc5217SJeff Kirsher
1916adfc5217SJeff Kirsher case sbmac_fc_frame: /* not valid in half duplex */
1917adfc5217SJeff Kirsher default: /* invalid selection */
1918adfc5217SJeff Kirsher return 0;
1919adfc5217SJeff Kirsher }
1920adfc5217SJeff Kirsher break;
1921adfc5217SJeff Kirsher
1922adfc5217SJeff Kirsher case sbmac_duplex_full:
1923adfc5217SJeff Kirsher switch (fc) {
1924adfc5217SJeff Kirsher case sbmac_fc_disabled:
1925adfc5217SJeff Kirsher cfg |= V_MAC_FC_CMD_DISABLED;
1926adfc5217SJeff Kirsher break;
1927adfc5217SJeff Kirsher
1928adfc5217SJeff Kirsher case sbmac_fc_frame:
1929adfc5217SJeff Kirsher cfg |= V_MAC_FC_CMD_ENABLED;
1930adfc5217SJeff Kirsher break;
1931adfc5217SJeff Kirsher
1932adfc5217SJeff Kirsher case sbmac_fc_collision: /* not valid in full duplex */
1933adfc5217SJeff Kirsher case sbmac_fc_carrier: /* not valid in full duplex */
1934adfc5217SJeff Kirsher default:
1935adfc5217SJeff Kirsher return 0;
1936adfc5217SJeff Kirsher }
1937adfc5217SJeff Kirsher break;
1938adfc5217SJeff Kirsher default:
1939adfc5217SJeff Kirsher return 0;
1940adfc5217SJeff Kirsher }
1941adfc5217SJeff Kirsher
1942adfc5217SJeff Kirsher /*
1943adfc5217SJeff Kirsher * Send the bits back to the hardware
1944adfc5217SJeff Kirsher */
1945adfc5217SJeff Kirsher
1946adfc5217SJeff Kirsher __raw_writeq(cfg, s->sbm_maccfg);
1947adfc5217SJeff Kirsher
1948adfc5217SJeff Kirsher return 1;
1949adfc5217SJeff Kirsher }
1950adfc5217SJeff Kirsher
1951adfc5217SJeff Kirsher
1952adfc5217SJeff Kirsher
1953adfc5217SJeff Kirsher
1954adfc5217SJeff Kirsher /**********************************************************************
1955adfc5217SJeff Kirsher * SBMAC_INTR()
1956adfc5217SJeff Kirsher *
1957adfc5217SJeff Kirsher * Interrupt handler for MAC interrupts
1958adfc5217SJeff Kirsher *
1959adfc5217SJeff Kirsher * Input parameters:
1960adfc5217SJeff Kirsher * MAC structure
1961adfc5217SJeff Kirsher *
1962adfc5217SJeff Kirsher * Return value:
1963adfc5217SJeff Kirsher * nothing
1964adfc5217SJeff Kirsher ********************************************************************* */
sbmac_intr(int irq,void * dev_instance)1965adfc5217SJeff Kirsher static irqreturn_t sbmac_intr(int irq,void *dev_instance)
1966adfc5217SJeff Kirsher {
1967adfc5217SJeff Kirsher struct net_device *dev = (struct net_device *) dev_instance;
1968adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
1969adfc5217SJeff Kirsher uint64_t isr;
1970adfc5217SJeff Kirsher int handled = 0;
1971adfc5217SJeff Kirsher
1972adfc5217SJeff Kirsher /*
1973adfc5217SJeff Kirsher * Read the ISR (this clears the bits in the real
1974adfc5217SJeff Kirsher * register, except for counter addr)
1975adfc5217SJeff Kirsher */
1976adfc5217SJeff Kirsher
1977adfc5217SJeff Kirsher isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
1978adfc5217SJeff Kirsher
1979adfc5217SJeff Kirsher if (isr == 0)
1980adfc5217SJeff Kirsher return IRQ_RETVAL(0);
1981adfc5217SJeff Kirsher handled = 1;
1982adfc5217SJeff Kirsher
1983adfc5217SJeff Kirsher /*
1984adfc5217SJeff Kirsher * Transmits on channel 0
1985adfc5217SJeff Kirsher */
1986adfc5217SJeff Kirsher
1987adfc5217SJeff Kirsher if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
1988adfc5217SJeff Kirsher sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
1989adfc5217SJeff Kirsher
1990adfc5217SJeff Kirsher if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
1991adfc5217SJeff Kirsher if (napi_schedule_prep(&sc->napi)) {
1992adfc5217SJeff Kirsher __raw_writeq(0, sc->sbm_imr);
1993adfc5217SJeff Kirsher __napi_schedule(&sc->napi);
1994adfc5217SJeff Kirsher /* Depend on the exit from poll to reenable intr */
1995adfc5217SJeff Kirsher }
1996adfc5217SJeff Kirsher else {
1997adfc5217SJeff Kirsher /* may leave some packets behind */
1998adfc5217SJeff Kirsher sbdma_rx_process(sc,&(sc->sbm_rxdma),
1999adfc5217SJeff Kirsher SBMAC_MAX_RXDESCR * 2, 0);
2000adfc5217SJeff Kirsher }
2001adfc5217SJeff Kirsher }
2002adfc5217SJeff Kirsher return IRQ_RETVAL(handled);
2003adfc5217SJeff Kirsher }
2004adfc5217SJeff Kirsher
2005adfc5217SJeff Kirsher /**********************************************************************
2006adfc5217SJeff Kirsher * SBMAC_START_TX(skb,dev)
2007adfc5217SJeff Kirsher *
2008adfc5217SJeff Kirsher * Start output on the specified interface. Basically, we
2009adfc5217SJeff Kirsher * queue as many buffers as we can until the ring fills up, or
2010adfc5217SJeff Kirsher * we run off the end of the queue, whichever comes first.
2011adfc5217SJeff Kirsher *
2012adfc5217SJeff Kirsher * Input parameters:
2013adfc5217SJeff Kirsher *
2014adfc5217SJeff Kirsher *
2015adfc5217SJeff Kirsher * Return value:
2016adfc5217SJeff Kirsher * nothing
2017adfc5217SJeff Kirsher ********************************************************************* */
sbmac_start_tx(struct sk_buff * skb,struct net_device * dev)20180c13b8d1SYueHaibing static netdev_tx_t sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
2019adfc5217SJeff Kirsher {
2020adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2021adfc5217SJeff Kirsher unsigned long flags;
2022adfc5217SJeff Kirsher
2023adfc5217SJeff Kirsher /* lock eth irq */
2024adfc5217SJeff Kirsher spin_lock_irqsave(&sc->sbm_lock, flags);
2025adfc5217SJeff Kirsher
2026adfc5217SJeff Kirsher /*
2027adfc5217SJeff Kirsher * Put the buffer on the transmit ring. If we
2028adfc5217SJeff Kirsher * don't have room, stop the queue.
2029adfc5217SJeff Kirsher */
2030adfc5217SJeff Kirsher
2031adfc5217SJeff Kirsher if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
2032adfc5217SJeff Kirsher /* XXX save skb that we could not send */
2033adfc5217SJeff Kirsher netif_stop_queue(dev);
2034adfc5217SJeff Kirsher spin_unlock_irqrestore(&sc->sbm_lock, flags);
2035adfc5217SJeff Kirsher
2036adfc5217SJeff Kirsher return NETDEV_TX_BUSY;
2037adfc5217SJeff Kirsher }
2038adfc5217SJeff Kirsher
2039adfc5217SJeff Kirsher spin_unlock_irqrestore(&sc->sbm_lock, flags);
2040adfc5217SJeff Kirsher
2041adfc5217SJeff Kirsher return NETDEV_TX_OK;
2042adfc5217SJeff Kirsher }
2043adfc5217SJeff Kirsher
2044adfc5217SJeff Kirsher /**********************************************************************
2045adfc5217SJeff Kirsher * SBMAC_SETMULTI(sc)
2046adfc5217SJeff Kirsher *
2047adfc5217SJeff Kirsher * Reprogram the multicast table into the hardware, given
2048adfc5217SJeff Kirsher * the list of multicasts associated with the interface
2049adfc5217SJeff Kirsher * structure.
2050adfc5217SJeff Kirsher *
2051adfc5217SJeff Kirsher * Input parameters:
2052adfc5217SJeff Kirsher * sc - softc
2053adfc5217SJeff Kirsher *
2054adfc5217SJeff Kirsher * Return value:
2055adfc5217SJeff Kirsher * nothing
2056adfc5217SJeff Kirsher ********************************************************************* */
2057adfc5217SJeff Kirsher
sbmac_setmulti(struct sbmac_softc * sc)2058adfc5217SJeff Kirsher static void sbmac_setmulti(struct sbmac_softc *sc)
2059adfc5217SJeff Kirsher {
2060adfc5217SJeff Kirsher uint64_t reg;
2061adfc5217SJeff Kirsher void __iomem *port;
2062adfc5217SJeff Kirsher int idx;
2063adfc5217SJeff Kirsher struct netdev_hw_addr *ha;
2064adfc5217SJeff Kirsher struct net_device *dev = sc->sbm_dev;
2065adfc5217SJeff Kirsher
2066adfc5217SJeff Kirsher /*
2067adfc5217SJeff Kirsher * Clear out entire multicast table. We do this by nuking
2068adfc5217SJeff Kirsher * the entire hash table and all the direct matches except
2069adfc5217SJeff Kirsher * the first one, which is used for our station address
2070adfc5217SJeff Kirsher */
2071adfc5217SJeff Kirsher
2072adfc5217SJeff Kirsher for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
2073adfc5217SJeff Kirsher port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
2074adfc5217SJeff Kirsher __raw_writeq(0, port);
2075adfc5217SJeff Kirsher }
2076adfc5217SJeff Kirsher
2077adfc5217SJeff Kirsher for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
2078adfc5217SJeff Kirsher port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
2079adfc5217SJeff Kirsher __raw_writeq(0, port);
2080adfc5217SJeff Kirsher }
2081adfc5217SJeff Kirsher
2082adfc5217SJeff Kirsher /*
2083adfc5217SJeff Kirsher * Clear the filter to say we don't want any multicasts.
2084adfc5217SJeff Kirsher */
2085adfc5217SJeff Kirsher
2086adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
2087adfc5217SJeff Kirsher reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2088adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
2089adfc5217SJeff Kirsher
2090adfc5217SJeff Kirsher if (dev->flags & IFF_ALLMULTI) {
2091adfc5217SJeff Kirsher /*
2092adfc5217SJeff Kirsher * Enable ALL multicasts. Do this by inverting the
2093adfc5217SJeff Kirsher * multicast enable bit.
2094adfc5217SJeff Kirsher */
2095adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
2096adfc5217SJeff Kirsher reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
2097adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
2098adfc5217SJeff Kirsher return;
2099adfc5217SJeff Kirsher }
2100adfc5217SJeff Kirsher
2101adfc5217SJeff Kirsher
2102adfc5217SJeff Kirsher /*
2103adfc5217SJeff Kirsher * Progam new multicast entries. For now, only use the
2104adfc5217SJeff Kirsher * perfect filter. In the future we'll need to use the
2105adfc5217SJeff Kirsher * hash filter if the perfect filter overflows
2106adfc5217SJeff Kirsher */
2107adfc5217SJeff Kirsher
2108adfc5217SJeff Kirsher /* XXX only using perfect filter for now, need to use hash
2109adfc5217SJeff Kirsher * XXX if the table overflows */
2110adfc5217SJeff Kirsher
2111adfc5217SJeff Kirsher idx = 1; /* skip station address */
2112adfc5217SJeff Kirsher netdev_for_each_mc_addr(ha, dev) {
2113adfc5217SJeff Kirsher if (idx == MAC_ADDR_COUNT)
2114adfc5217SJeff Kirsher break;
2115adfc5217SJeff Kirsher reg = sbmac_addr2reg(ha->addr);
2116adfc5217SJeff Kirsher port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
2117adfc5217SJeff Kirsher __raw_writeq(reg, port);
2118adfc5217SJeff Kirsher idx++;
2119adfc5217SJeff Kirsher }
2120adfc5217SJeff Kirsher
2121adfc5217SJeff Kirsher /*
2122adfc5217SJeff Kirsher * Enable the "accept multicast bits" if we programmed at least one
2123adfc5217SJeff Kirsher * multicast.
2124adfc5217SJeff Kirsher */
2125adfc5217SJeff Kirsher
2126adfc5217SJeff Kirsher if (idx > 1) {
2127adfc5217SJeff Kirsher reg = __raw_readq(sc->sbm_rxfilter);
2128adfc5217SJeff Kirsher reg |= M_MAC_MCAST_EN;
2129adfc5217SJeff Kirsher __raw_writeq(reg, sc->sbm_rxfilter);
2130adfc5217SJeff Kirsher }
2131adfc5217SJeff Kirsher }
2132adfc5217SJeff Kirsher
2133adfc5217SJeff Kirsher static const struct net_device_ops sbmac_netdev_ops = {
2134adfc5217SJeff Kirsher .ndo_open = sbmac_open,
2135adfc5217SJeff Kirsher .ndo_stop = sbmac_close,
2136adfc5217SJeff Kirsher .ndo_start_xmit = sbmac_start_tx,
2137afc4b13dSJiri Pirko .ndo_set_rx_mode = sbmac_set_rx_mode,
2138adfc5217SJeff Kirsher .ndo_tx_timeout = sbmac_tx_timeout,
2139a7605370SArnd Bergmann .ndo_eth_ioctl = sbmac_mii_ioctl,
2140adfc5217SJeff Kirsher .ndo_validate_addr = eth_validate_addr,
2141adfc5217SJeff Kirsher .ndo_set_mac_address = eth_mac_addr,
2142adfc5217SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2143adfc5217SJeff Kirsher .ndo_poll_controller = sbmac_netpoll,
2144adfc5217SJeff Kirsher #endif
2145adfc5217SJeff Kirsher };
2146adfc5217SJeff Kirsher
2147adfc5217SJeff Kirsher /**********************************************************************
2148adfc5217SJeff Kirsher * SBMAC_INIT(dev)
2149adfc5217SJeff Kirsher *
2150adfc5217SJeff Kirsher * Attach routine - init hardware and hook ourselves into linux
2151adfc5217SJeff Kirsher *
2152adfc5217SJeff Kirsher * Input parameters:
2153adfc5217SJeff Kirsher * dev - net_device structure
2154adfc5217SJeff Kirsher *
2155adfc5217SJeff Kirsher * Return value:
2156adfc5217SJeff Kirsher * status
2157adfc5217SJeff Kirsher ********************************************************************* */
2158adfc5217SJeff Kirsher
sbmac_init(struct platform_device * pldev,long long base)2159adfc5217SJeff Kirsher static int sbmac_init(struct platform_device *pldev, long long base)
2160adfc5217SJeff Kirsher {
21618513fbd8SJingoo Han struct net_device *dev = platform_get_drvdata(pldev);
2162adfc5217SJeff Kirsher int idx = pldev->id;
2163adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2164adfc5217SJeff Kirsher unsigned char *eaddr;
2165adfc5217SJeff Kirsher uint64_t ea_reg;
2166adfc5217SJeff Kirsher int i;
2167adfc5217SJeff Kirsher int err;
2168adfc5217SJeff Kirsher
2169adfc5217SJeff Kirsher sc->sbm_dev = dev;
2170adfc5217SJeff Kirsher sc->sbe_idx = idx;
2171adfc5217SJeff Kirsher
2172adfc5217SJeff Kirsher eaddr = sc->sbm_hwaddr;
2173adfc5217SJeff Kirsher
2174adfc5217SJeff Kirsher /*
2175adfc5217SJeff Kirsher * Read the ethernet address. The firmware left this programmed
2176adfc5217SJeff Kirsher * for us in the ethernet address register for each mac.
2177adfc5217SJeff Kirsher */
2178adfc5217SJeff Kirsher
2179adfc5217SJeff Kirsher ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
2180adfc5217SJeff Kirsher __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
2181adfc5217SJeff Kirsher for (i = 0; i < 6; i++) {
2182adfc5217SJeff Kirsher eaddr[i] = (uint8_t) (ea_reg & 0xFF);
2183adfc5217SJeff Kirsher ea_reg >>= 8;
2184adfc5217SJeff Kirsher }
2185adfc5217SJeff Kirsher
21867f6ec2b2SJakub Kicinski eth_hw_addr_set(dev, eaddr);
2187adfc5217SJeff Kirsher
2188adfc5217SJeff Kirsher /*
2189adfc5217SJeff Kirsher * Initialize context (get pointers to registers and stuff), then
2190adfc5217SJeff Kirsher * allocate the memory for the descriptor tables.
2191adfc5217SJeff Kirsher */
2192adfc5217SJeff Kirsher
2193adfc5217SJeff Kirsher sbmac_initctx(sc);
2194adfc5217SJeff Kirsher
2195adfc5217SJeff Kirsher /*
2196adfc5217SJeff Kirsher * Set up Linux device callins
2197adfc5217SJeff Kirsher */
2198adfc5217SJeff Kirsher
2199adfc5217SJeff Kirsher spin_lock_init(&(sc->sbm_lock));
2200adfc5217SJeff Kirsher
2201adfc5217SJeff Kirsher dev->netdev_ops = &sbmac_netdev_ops;
2202adfc5217SJeff Kirsher dev->watchdog_timeo = TX_TIMEOUT;
2203110447f8SStefan Richter dev->min_mtu = 0;
2204d894be57SJarod Wilson dev->max_mtu = ENET_PACKET_SIZE;
2205adfc5217SJeff Kirsher
2206b707b89fSJakub Kicinski netif_napi_add_weight(dev, &sc->napi, sbmac_poll, 16);
2207adfc5217SJeff Kirsher
2208adfc5217SJeff Kirsher dev->irq = UNIT_INT(idx);
2209adfc5217SJeff Kirsher
2210adfc5217SJeff Kirsher /* This is needed for PASS2 for Rx H/W checksum feature */
2211adfc5217SJeff Kirsher sbmac_set_iphdr_offset(sc);
2212adfc5217SJeff Kirsher
2213adfc5217SJeff Kirsher sc->mii_bus = mdiobus_alloc();
2214adfc5217SJeff Kirsher if (sc->mii_bus == NULL) {
2215adfc5217SJeff Kirsher err = -ENOMEM;
2216adfc5217SJeff Kirsher goto uninit_ctx;
2217adfc5217SJeff Kirsher }
2218adfc5217SJeff Kirsher
2219adfc5217SJeff Kirsher sc->mii_bus->name = sbmac_mdio_string;
2220446bbc4bSFlorian Fainelli snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2221446bbc4bSFlorian Fainelli pldev->name, idx);
2222adfc5217SJeff Kirsher sc->mii_bus->priv = sc;
2223adfc5217SJeff Kirsher sc->mii_bus->read = sbmac_mii_read;
2224adfc5217SJeff Kirsher sc->mii_bus->write = sbmac_mii_write;
2225adfc5217SJeff Kirsher
2226adfc5217SJeff Kirsher sc->mii_bus->parent = &pldev->dev;
2227adfc5217SJeff Kirsher /*
2228adfc5217SJeff Kirsher * Probe PHY address
2229adfc5217SJeff Kirsher */
2230adfc5217SJeff Kirsher err = mdiobus_register(sc->mii_bus);
2231adfc5217SJeff Kirsher if (err) {
2232adfc5217SJeff Kirsher printk(KERN_ERR "%s: unable to register MDIO bus\n",
2233adfc5217SJeff Kirsher dev->name);
2234adfc5217SJeff Kirsher goto free_mdio;
2235adfc5217SJeff Kirsher }
22368513fbd8SJingoo Han platform_set_drvdata(pldev, sc->mii_bus);
2237adfc5217SJeff Kirsher
2238adfc5217SJeff Kirsher err = register_netdev(dev);
2239adfc5217SJeff Kirsher if (err) {
2240adfc5217SJeff Kirsher printk(KERN_ERR "%s.%d: unable to register netdev\n",
2241adfc5217SJeff Kirsher sbmac_string, idx);
2242adfc5217SJeff Kirsher goto unreg_mdio;
2243adfc5217SJeff Kirsher }
2244adfc5217SJeff Kirsher
2245adfc5217SJeff Kirsher pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
2246adfc5217SJeff Kirsher
2247adfc5217SJeff Kirsher if (sc->rx_hw_checksum == ENABLE)
2248adfc5217SJeff Kirsher pr_info("%s: enabling TCP rcv checksum\n", dev->name);
2249adfc5217SJeff Kirsher
2250adfc5217SJeff Kirsher /*
2251adfc5217SJeff Kirsher * Display Ethernet address (this is called during the config
2252adfc5217SJeff Kirsher * process so we need to finish off the config message that
2253adfc5217SJeff Kirsher * was being displayed)
2254adfc5217SJeff Kirsher */
2255adfc5217SJeff Kirsher pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
2256adfc5217SJeff Kirsher dev->name, base, eaddr);
2257adfc5217SJeff Kirsher
2258adfc5217SJeff Kirsher return 0;
2259adfc5217SJeff Kirsher unreg_mdio:
2260adfc5217SJeff Kirsher mdiobus_unregister(sc->mii_bus);
2261adfc5217SJeff Kirsher free_mdio:
2262adfc5217SJeff Kirsher mdiobus_free(sc->mii_bus);
2263adfc5217SJeff Kirsher uninit_ctx:
2264adfc5217SJeff Kirsher sbmac_uninitctx(sc);
2265adfc5217SJeff Kirsher return err;
2266adfc5217SJeff Kirsher }
2267adfc5217SJeff Kirsher
2268adfc5217SJeff Kirsher
sbmac_open(struct net_device * dev)2269adfc5217SJeff Kirsher static int sbmac_open(struct net_device *dev)
2270adfc5217SJeff Kirsher {
2271adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2272adfc5217SJeff Kirsher int err;
2273adfc5217SJeff Kirsher
2274adfc5217SJeff Kirsher if (debug > 1)
2275adfc5217SJeff Kirsher pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
2276adfc5217SJeff Kirsher
2277adfc5217SJeff Kirsher /*
2278adfc5217SJeff Kirsher * map/route interrupt (clear status first, in case something
2279adfc5217SJeff Kirsher * weird is pending; we haven't initialized the mac registers
2280adfc5217SJeff Kirsher * yet)
2281adfc5217SJeff Kirsher */
2282adfc5217SJeff Kirsher
2283adfc5217SJeff Kirsher __raw_readq(sc->sbm_isr);
2284adfc5217SJeff Kirsher err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
2285adfc5217SJeff Kirsher if (err) {
2286adfc5217SJeff Kirsher printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
2287adfc5217SJeff Kirsher dev->irq);
2288adfc5217SJeff Kirsher goto out_err;
2289adfc5217SJeff Kirsher }
2290adfc5217SJeff Kirsher
2291adfc5217SJeff Kirsher sc->sbm_speed = sbmac_speed_none;
2292adfc5217SJeff Kirsher sc->sbm_duplex = sbmac_duplex_none;
2293adfc5217SJeff Kirsher sc->sbm_fc = sbmac_fc_none;
2294adfc5217SJeff Kirsher sc->sbm_pause = -1;
2295adfc5217SJeff Kirsher sc->sbm_link = 0;
2296adfc5217SJeff Kirsher
2297adfc5217SJeff Kirsher /*
2298adfc5217SJeff Kirsher * Attach to the PHY
2299adfc5217SJeff Kirsher */
2300adfc5217SJeff Kirsher err = sbmac_mii_probe(dev);
2301adfc5217SJeff Kirsher if (err)
2302adfc5217SJeff Kirsher goto out_unregister;
2303adfc5217SJeff Kirsher
2304adfc5217SJeff Kirsher /*
2305adfc5217SJeff Kirsher * Turn on the channel
2306adfc5217SJeff Kirsher */
2307adfc5217SJeff Kirsher
2308adfc5217SJeff Kirsher sbmac_set_channel_state(sc,sbmac_state_on);
2309adfc5217SJeff Kirsher
2310adfc5217SJeff Kirsher netif_start_queue(dev);
2311adfc5217SJeff Kirsher
2312adfc5217SJeff Kirsher sbmac_set_rx_mode(dev);
2313adfc5217SJeff Kirsher
2314adfc5217SJeff Kirsher phy_start(sc->phy_dev);
2315adfc5217SJeff Kirsher
2316adfc5217SJeff Kirsher napi_enable(&sc->napi);
2317adfc5217SJeff Kirsher
2318adfc5217SJeff Kirsher return 0;
2319adfc5217SJeff Kirsher
2320adfc5217SJeff Kirsher out_unregister:
2321adfc5217SJeff Kirsher free_irq(dev->irq, dev);
2322adfc5217SJeff Kirsher out_err:
2323adfc5217SJeff Kirsher return err;
2324adfc5217SJeff Kirsher }
2325adfc5217SJeff Kirsher
sbmac_mii_probe(struct net_device * dev)2326adfc5217SJeff Kirsher static int sbmac_mii_probe(struct net_device *dev)
2327adfc5217SJeff Kirsher {
2328adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2329adfc5217SJeff Kirsher struct phy_device *phy_dev;
2330adfc5217SJeff Kirsher
2331ee64f08eSGuenter Roeck phy_dev = phy_find_first(sc->mii_bus);
2332adfc5217SJeff Kirsher if (!phy_dev) {
2333adfc5217SJeff Kirsher printk(KERN_ERR "%s: no PHY found\n", dev->name);
2334adfc5217SJeff Kirsher return -ENXIO;
2335adfc5217SJeff Kirsher }
2336adfc5217SJeff Kirsher
2337e5a03bfdSAndrew Lunn phy_dev = phy_connect(dev, dev_name(&phy_dev->mdio.dev),
2338e5a03bfdSAndrew Lunn &sbmac_mii_poll, PHY_INTERFACE_MODE_GMII);
2339adfc5217SJeff Kirsher if (IS_ERR(phy_dev)) {
2340adfc5217SJeff Kirsher printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
2341adfc5217SJeff Kirsher return PTR_ERR(phy_dev);
2342adfc5217SJeff Kirsher }
2343adfc5217SJeff Kirsher
2344adfc5217SJeff Kirsher /* Remove any features not supported by the controller */
234558056c1eSAndrew Lunn phy_set_max_speed(phy_dev, SPEED_1000);
2346af8d9bb2SAndrew Lunn phy_support_asym_pause(phy_dev);
2347adfc5217SJeff Kirsher
2348ee64f08eSGuenter Roeck phy_attached_info(phy_dev);
23492220943aSAndrew Lunn
2350adfc5217SJeff Kirsher sc->phy_dev = phy_dev;
2351adfc5217SJeff Kirsher
2352adfc5217SJeff Kirsher return 0;
2353adfc5217SJeff Kirsher }
2354adfc5217SJeff Kirsher
2355adfc5217SJeff Kirsher
sbmac_mii_poll(struct net_device * dev)2356adfc5217SJeff Kirsher static void sbmac_mii_poll(struct net_device *dev)
2357adfc5217SJeff Kirsher {
2358adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2359adfc5217SJeff Kirsher struct phy_device *phy_dev = sc->phy_dev;
2360adfc5217SJeff Kirsher unsigned long flags;
2361adfc5217SJeff Kirsher enum sbmac_fc fc;
2362adfc5217SJeff Kirsher int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
2363adfc5217SJeff Kirsher
2364adfc5217SJeff Kirsher link_chg = (sc->sbm_link != phy_dev->link);
2365adfc5217SJeff Kirsher speed_chg = (sc->sbm_speed != phy_dev->speed);
2366adfc5217SJeff Kirsher duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
2367adfc5217SJeff Kirsher pause_chg = (sc->sbm_pause != phy_dev->pause);
2368adfc5217SJeff Kirsher
2369adfc5217SJeff Kirsher if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
2370adfc5217SJeff Kirsher return; /* Hmmm... */
2371adfc5217SJeff Kirsher
2372adfc5217SJeff Kirsher if (!phy_dev->link) {
2373adfc5217SJeff Kirsher if (link_chg) {
2374adfc5217SJeff Kirsher sc->sbm_link = phy_dev->link;
2375adfc5217SJeff Kirsher sc->sbm_speed = sbmac_speed_none;
2376adfc5217SJeff Kirsher sc->sbm_duplex = sbmac_duplex_none;
2377adfc5217SJeff Kirsher sc->sbm_fc = sbmac_fc_disabled;
2378adfc5217SJeff Kirsher sc->sbm_pause = -1;
2379adfc5217SJeff Kirsher pr_info("%s: link unavailable\n", dev->name);
2380adfc5217SJeff Kirsher }
2381adfc5217SJeff Kirsher return;
2382adfc5217SJeff Kirsher }
2383adfc5217SJeff Kirsher
2384adfc5217SJeff Kirsher if (phy_dev->duplex == DUPLEX_FULL) {
2385adfc5217SJeff Kirsher if (phy_dev->pause)
2386adfc5217SJeff Kirsher fc = sbmac_fc_frame;
2387adfc5217SJeff Kirsher else
2388adfc5217SJeff Kirsher fc = sbmac_fc_disabled;
2389adfc5217SJeff Kirsher } else
2390adfc5217SJeff Kirsher fc = sbmac_fc_collision;
2391adfc5217SJeff Kirsher fc_chg = (sc->sbm_fc != fc);
2392adfc5217SJeff Kirsher
2393adfc5217SJeff Kirsher pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
2394adfc5217SJeff Kirsher phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
2395adfc5217SJeff Kirsher
2396adfc5217SJeff Kirsher spin_lock_irqsave(&sc->sbm_lock, flags);
2397adfc5217SJeff Kirsher
2398adfc5217SJeff Kirsher sc->sbm_speed = phy_dev->speed;
2399adfc5217SJeff Kirsher sc->sbm_duplex = phy_dev->duplex;
2400adfc5217SJeff Kirsher sc->sbm_fc = fc;
2401adfc5217SJeff Kirsher sc->sbm_pause = phy_dev->pause;
2402adfc5217SJeff Kirsher sc->sbm_link = phy_dev->link;
2403adfc5217SJeff Kirsher
2404adfc5217SJeff Kirsher if ((speed_chg || duplex_chg || fc_chg) &&
2405adfc5217SJeff Kirsher sc->sbm_state != sbmac_state_off) {
2406adfc5217SJeff Kirsher /*
2407adfc5217SJeff Kirsher * something changed, restart the channel
2408adfc5217SJeff Kirsher */
2409adfc5217SJeff Kirsher if (debug > 1)
2410adfc5217SJeff Kirsher pr_debug("%s: restarting channel "
2411adfc5217SJeff Kirsher "because PHY state changed\n", dev->name);
2412adfc5217SJeff Kirsher sbmac_channel_stop(sc);
2413adfc5217SJeff Kirsher sbmac_channel_start(sc);
2414adfc5217SJeff Kirsher }
2415adfc5217SJeff Kirsher
2416adfc5217SJeff Kirsher spin_unlock_irqrestore(&sc->sbm_lock, flags);
2417adfc5217SJeff Kirsher }
2418adfc5217SJeff Kirsher
2419adfc5217SJeff Kirsher
sbmac_tx_timeout(struct net_device * dev,unsigned int txqueue)24200290bd29SMichael S. Tsirkin static void sbmac_tx_timeout (struct net_device *dev, unsigned int txqueue)
2421adfc5217SJeff Kirsher {
2422adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2423adfc5217SJeff Kirsher unsigned long flags;
2424adfc5217SJeff Kirsher
2425adfc5217SJeff Kirsher spin_lock_irqsave(&sc->sbm_lock, flags);
2426adfc5217SJeff Kirsher
2427adfc5217SJeff Kirsher
2428860e9538SFlorian Westphal netif_trans_update(dev); /* prevent tx timeout */
2429adfc5217SJeff Kirsher dev->stats.tx_errors++;
2430adfc5217SJeff Kirsher
2431adfc5217SJeff Kirsher spin_unlock_irqrestore(&sc->sbm_lock, flags);
2432adfc5217SJeff Kirsher
2433adfc5217SJeff Kirsher printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
2434adfc5217SJeff Kirsher }
2435adfc5217SJeff Kirsher
2436adfc5217SJeff Kirsher
2437adfc5217SJeff Kirsher
2438adfc5217SJeff Kirsher
sbmac_set_rx_mode(struct net_device * dev)2439adfc5217SJeff Kirsher static void sbmac_set_rx_mode(struct net_device *dev)
2440adfc5217SJeff Kirsher {
2441adfc5217SJeff Kirsher unsigned long flags;
2442adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2443adfc5217SJeff Kirsher
2444adfc5217SJeff Kirsher spin_lock_irqsave(&sc->sbm_lock, flags);
2445adfc5217SJeff Kirsher if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
2446adfc5217SJeff Kirsher /*
2447adfc5217SJeff Kirsher * Promiscuous changed.
2448adfc5217SJeff Kirsher */
2449adfc5217SJeff Kirsher
2450adfc5217SJeff Kirsher if (dev->flags & IFF_PROMISC) {
2451adfc5217SJeff Kirsher sbmac_promiscuous_mode(sc,1);
2452adfc5217SJeff Kirsher }
2453adfc5217SJeff Kirsher else {
2454adfc5217SJeff Kirsher sbmac_promiscuous_mode(sc,0);
2455adfc5217SJeff Kirsher }
2456adfc5217SJeff Kirsher }
2457adfc5217SJeff Kirsher spin_unlock_irqrestore(&sc->sbm_lock, flags);
2458adfc5217SJeff Kirsher
2459adfc5217SJeff Kirsher /*
2460adfc5217SJeff Kirsher * Program the multicasts. Do this every time.
2461adfc5217SJeff Kirsher */
2462adfc5217SJeff Kirsher
2463adfc5217SJeff Kirsher sbmac_setmulti(sc);
2464adfc5217SJeff Kirsher
2465adfc5217SJeff Kirsher }
2466adfc5217SJeff Kirsher
sbmac_mii_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)2467adfc5217SJeff Kirsher static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2468adfc5217SJeff Kirsher {
2469adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2470adfc5217SJeff Kirsher
2471adfc5217SJeff Kirsher if (!netif_running(dev) || !sc->phy_dev)
2472adfc5217SJeff Kirsher return -EINVAL;
2473adfc5217SJeff Kirsher
2474adfc5217SJeff Kirsher return phy_mii_ioctl(sc->phy_dev, rq, cmd);
2475adfc5217SJeff Kirsher }
2476adfc5217SJeff Kirsher
sbmac_close(struct net_device * dev)2477adfc5217SJeff Kirsher static int sbmac_close(struct net_device *dev)
2478adfc5217SJeff Kirsher {
2479adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2480adfc5217SJeff Kirsher
2481adfc5217SJeff Kirsher napi_disable(&sc->napi);
2482adfc5217SJeff Kirsher
2483adfc5217SJeff Kirsher phy_stop(sc->phy_dev);
2484adfc5217SJeff Kirsher
2485adfc5217SJeff Kirsher sbmac_set_channel_state(sc, sbmac_state_off);
2486adfc5217SJeff Kirsher
2487adfc5217SJeff Kirsher netif_stop_queue(dev);
2488adfc5217SJeff Kirsher
2489adfc5217SJeff Kirsher if (debug > 1)
2490adfc5217SJeff Kirsher pr_debug("%s: Shutting down ethercard\n", dev->name);
2491adfc5217SJeff Kirsher
2492adfc5217SJeff Kirsher phy_disconnect(sc->phy_dev);
2493adfc5217SJeff Kirsher sc->phy_dev = NULL;
2494adfc5217SJeff Kirsher free_irq(dev->irq, dev);
2495adfc5217SJeff Kirsher
2496adfc5217SJeff Kirsher sbdma_emptyring(&(sc->sbm_txdma));
2497adfc5217SJeff Kirsher sbdma_emptyring(&(sc->sbm_rxdma));
2498adfc5217SJeff Kirsher
2499adfc5217SJeff Kirsher return 0;
2500adfc5217SJeff Kirsher }
2501adfc5217SJeff Kirsher
sbmac_poll(struct napi_struct * napi,int budget)2502adfc5217SJeff Kirsher static int sbmac_poll(struct napi_struct *napi, int budget)
2503adfc5217SJeff Kirsher {
2504adfc5217SJeff Kirsher struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
2505adfc5217SJeff Kirsher int work_done;
2506adfc5217SJeff Kirsher
2507adfc5217SJeff Kirsher work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
2508adfc5217SJeff Kirsher sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
2509adfc5217SJeff Kirsher
2510adfc5217SJeff Kirsher if (work_done < budget) {
25116ad20165SEric Dumazet napi_complete_done(napi, work_done);
2512adfc5217SJeff Kirsher
2513adfc5217SJeff Kirsher #ifdef CONFIG_SBMAC_COALESCE
2514adfc5217SJeff Kirsher __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
2515adfc5217SJeff Kirsher ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
2516adfc5217SJeff Kirsher sc->sbm_imr);
2517adfc5217SJeff Kirsher #else
2518adfc5217SJeff Kirsher __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
2519adfc5217SJeff Kirsher (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
2520adfc5217SJeff Kirsher #endif
2521adfc5217SJeff Kirsher }
2522adfc5217SJeff Kirsher
2523adfc5217SJeff Kirsher return work_done;
2524adfc5217SJeff Kirsher }
2525adfc5217SJeff Kirsher
2526adfc5217SJeff Kirsher
sbmac_probe(struct platform_device * pldev)2527047fc566SBill Pemberton static int sbmac_probe(struct platform_device *pldev)
2528adfc5217SJeff Kirsher {
2529adfc5217SJeff Kirsher struct net_device *dev;
2530adfc5217SJeff Kirsher struct sbmac_softc *sc;
2531adfc5217SJeff Kirsher void __iomem *sbm_base;
2532adfc5217SJeff Kirsher struct resource *res;
2533adfc5217SJeff Kirsher u64 sbmac_orig_hwaddr;
2534adfc5217SJeff Kirsher int err;
2535adfc5217SJeff Kirsher
2536adfc5217SJeff Kirsher res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
253760d78e9fSYang Yingliang if (!res) {
253860d78e9fSYang Yingliang printk(KERN_ERR "%s: failed to get resource\n",
253960d78e9fSYang Yingliang dev_name(&pldev->dev));
254060d78e9fSYang Yingliang err = -EINVAL;
254160d78e9fSYang Yingliang goto out_out;
254260d78e9fSYang Yingliang }
25434bdc0d67SChristoph Hellwig sbm_base = ioremap(res->start, resource_size(res));
2544adfc5217SJeff Kirsher if (!sbm_base) {
2545adfc5217SJeff Kirsher printk(KERN_ERR "%s: unable to map device registers\n",
2546adfc5217SJeff Kirsher dev_name(&pldev->dev));
2547adfc5217SJeff Kirsher err = -ENOMEM;
2548adfc5217SJeff Kirsher goto out_out;
2549adfc5217SJeff Kirsher }
2550adfc5217SJeff Kirsher
2551adfc5217SJeff Kirsher /*
2552adfc5217SJeff Kirsher * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
2553adfc5217SJeff Kirsher * value for us by the firmware if we're going to use this MAC.
2554adfc5217SJeff Kirsher * If we find a zero, skip this MAC.
2555adfc5217SJeff Kirsher */
2556adfc5217SJeff Kirsher sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
2557adfc5217SJeff Kirsher pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
2558adfc5217SJeff Kirsher sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
2559adfc5217SJeff Kirsher if (sbmac_orig_hwaddr == 0) {
2560adfc5217SJeff Kirsher err = 0;
2561adfc5217SJeff Kirsher goto out_unmap;
2562adfc5217SJeff Kirsher }
2563adfc5217SJeff Kirsher
2564adfc5217SJeff Kirsher /*
2565adfc5217SJeff Kirsher * Okay, cool. Initialize this MAC.
2566adfc5217SJeff Kirsher */
2567adfc5217SJeff Kirsher dev = alloc_etherdev(sizeof(struct sbmac_softc));
2568adfc5217SJeff Kirsher if (!dev) {
2569adfc5217SJeff Kirsher err = -ENOMEM;
2570adfc5217SJeff Kirsher goto out_unmap;
2571adfc5217SJeff Kirsher }
2572adfc5217SJeff Kirsher
25738513fbd8SJingoo Han platform_set_drvdata(pldev, dev);
2574adfc5217SJeff Kirsher SET_NETDEV_DEV(dev, &pldev->dev);
2575adfc5217SJeff Kirsher
2576adfc5217SJeff Kirsher sc = netdev_priv(dev);
2577adfc5217SJeff Kirsher sc->sbm_base = sbm_base;
2578adfc5217SJeff Kirsher
2579adfc5217SJeff Kirsher err = sbmac_init(pldev, res->start);
2580adfc5217SJeff Kirsher if (err)
2581adfc5217SJeff Kirsher goto out_kfree;
2582adfc5217SJeff Kirsher
2583adfc5217SJeff Kirsher return 0;
2584adfc5217SJeff Kirsher
2585adfc5217SJeff Kirsher out_kfree:
2586adfc5217SJeff Kirsher free_netdev(dev);
2587adfc5217SJeff Kirsher __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
2588adfc5217SJeff Kirsher
2589adfc5217SJeff Kirsher out_unmap:
2590adfc5217SJeff Kirsher iounmap(sbm_base);
2591adfc5217SJeff Kirsher
2592adfc5217SJeff Kirsher out_out:
2593adfc5217SJeff Kirsher return err;
2594adfc5217SJeff Kirsher }
2595adfc5217SJeff Kirsher
sbmac_remove(struct platform_device * pldev)2596be12502eSDmitry Torokhov static int sbmac_remove(struct platform_device *pldev)
2597adfc5217SJeff Kirsher {
25988513fbd8SJingoo Han struct net_device *dev = platform_get_drvdata(pldev);
2599adfc5217SJeff Kirsher struct sbmac_softc *sc = netdev_priv(dev);
2600adfc5217SJeff Kirsher
2601adfc5217SJeff Kirsher unregister_netdev(dev);
2602adfc5217SJeff Kirsher sbmac_uninitctx(sc);
2603adfc5217SJeff Kirsher mdiobus_unregister(sc->mii_bus);
2604adfc5217SJeff Kirsher mdiobus_free(sc->mii_bus);
2605adfc5217SJeff Kirsher iounmap(sc->sbm_base);
2606adfc5217SJeff Kirsher free_netdev(dev);
2607adfc5217SJeff Kirsher
2608adfc5217SJeff Kirsher return 0;
2609adfc5217SJeff Kirsher }
2610adfc5217SJeff Kirsher
2611adfc5217SJeff Kirsher static struct platform_driver sbmac_driver = {
2612adfc5217SJeff Kirsher .probe = sbmac_probe,
2613be12502eSDmitry Torokhov .remove = sbmac_remove,
2614adfc5217SJeff Kirsher .driver = {
2615adfc5217SJeff Kirsher .name = sbmac_string,
2616adfc5217SJeff Kirsher },
2617adfc5217SJeff Kirsher };
2618adfc5217SJeff Kirsher
2619db62f684SAxel Lin module_platform_driver(sbmac_driver);
2620a98e1788SRalf Baechle MODULE_LICENSE("GPL");
2621