1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
23396c782SPaul Gortmaker /* drivers/net/ethernet/8390/ax88796.c
3644570b8SJeff Kirsher  *
4644570b8SJeff Kirsher  * Copyright 2005,2007 Simtec Electronics
5644570b8SJeff Kirsher  *	Ben Dooks <ben@simtec.co.uk>
6644570b8SJeff Kirsher  *
7644570b8SJeff Kirsher  * Asix AX88796 10/100 Ethernet controller support
8644570b8SJeff Kirsher  *	Based on ne.c, by Donald Becker, et-al.
9644570b8SJeff Kirsher  */
10644570b8SJeff Kirsher 
11644570b8SJeff Kirsher #include <linux/module.h>
12644570b8SJeff Kirsher #include <linux/kernel.h>
13644570b8SJeff Kirsher #include <linux/errno.h>
14644570b8SJeff Kirsher #include <linux/isapnp.h>
15644570b8SJeff Kirsher #include <linux/interrupt.h>
16644570b8SJeff Kirsher #include <linux/io.h>
17644570b8SJeff Kirsher #include <linux/platform_device.h>
18644570b8SJeff Kirsher #include <linux/delay.h>
19644570b8SJeff Kirsher #include <linux/timer.h>
20644570b8SJeff Kirsher #include <linux/netdevice.h>
21644570b8SJeff Kirsher #include <linux/etherdevice.h>
22644570b8SJeff Kirsher #include <linux/ethtool.h>
23644570b8SJeff Kirsher #include <linux/mdio-bitbang.h>
24644570b8SJeff Kirsher #include <linux/phy.h>
25644570b8SJeff Kirsher #include <linux/eeprom_93cx6.h>
26644570b8SJeff Kirsher #include <linux/slab.h>
27644570b8SJeff Kirsher 
28644570b8SJeff Kirsher #include <net/ax88796.h>
29644570b8SJeff Kirsher 
30644570b8SJeff Kirsher 
31644570b8SJeff Kirsher /* Rename the lib8390.c functions to show that they are in this driver */
32644570b8SJeff Kirsher #define __ei_open ax_ei_open
33644570b8SJeff Kirsher #define __ei_close ax_ei_close
34644570b8SJeff Kirsher #define __ei_poll ax_ei_poll
35644570b8SJeff Kirsher #define __ei_start_xmit ax_ei_start_xmit
36644570b8SJeff Kirsher #define __ei_tx_timeout ax_ei_tx_timeout
37644570b8SJeff Kirsher #define __ei_get_stats ax_ei_get_stats
38644570b8SJeff Kirsher #define __ei_set_multicast_list ax_ei_set_multicast_list
39644570b8SJeff Kirsher #define __ei_interrupt ax_ei_interrupt
40644570b8SJeff Kirsher #define ____alloc_ei_netdev ax__alloc_ei_netdev
41644570b8SJeff Kirsher #define __NS8390_init ax_NS8390_init
42644570b8SJeff Kirsher 
43644570b8SJeff Kirsher /* force unsigned long back to 'void __iomem *' */
44644570b8SJeff Kirsher #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
45644570b8SJeff Kirsher 
46644570b8SJeff Kirsher #define ei_inb(_a) readb(ax_convert_addr(_a))
47644570b8SJeff Kirsher #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
48644570b8SJeff Kirsher 
49644570b8SJeff Kirsher #define ei_inb_p(_a) ei_inb(_a)
50644570b8SJeff Kirsher #define ei_outb_p(_v, _a) ei_outb(_v, _a)
51644570b8SJeff Kirsher 
52644570b8SJeff Kirsher /* define EI_SHIFT() to take into account our register offsets */
53644570b8SJeff Kirsher #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
54644570b8SJeff Kirsher 
55644570b8SJeff Kirsher /* Ensure we have our RCR base value */
56644570b8SJeff Kirsher #define AX88796_PLATFORM
57644570b8SJeff Kirsher 
58644570b8SJeff Kirsher static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
59644570b8SJeff Kirsher 
60644570b8SJeff Kirsher #include "lib8390.c"
61644570b8SJeff Kirsher 
62644570b8SJeff Kirsher #define DRV_NAME "ax88796"
63644570b8SJeff Kirsher #define DRV_VERSION "1.00"
64644570b8SJeff Kirsher 
65644570b8SJeff Kirsher /* from ne.c */
66644570b8SJeff Kirsher #define NE_CMD		EI_SHIFT(0x00)
67644570b8SJeff Kirsher #define NE_RESET	EI_SHIFT(0x1f)
68644570b8SJeff Kirsher #define NE_DATAPORT	EI_SHIFT(0x10)
69644570b8SJeff Kirsher 
70644570b8SJeff Kirsher #define NE1SM_START_PG	0x20	/* First page of TX buffer */
71644570b8SJeff Kirsher #define NE1SM_STOP_PG	0x40	/* Last page +1 of RX ring */
72644570b8SJeff Kirsher #define NESM_START_PG	0x40	/* First page of TX buffer */
73644570b8SJeff Kirsher #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
74644570b8SJeff Kirsher 
75644570b8SJeff Kirsher #define AX_GPOC_PPDSET	BIT(6)
76644570b8SJeff Kirsher 
77644570b8SJeff Kirsher /* device private data */
78644570b8SJeff Kirsher 
79644570b8SJeff Kirsher struct ax_device {
80644570b8SJeff Kirsher 	struct mii_bus *mii_bus;
81644570b8SJeff Kirsher 	struct mdiobb_ctrl bb_ctrl;
82644570b8SJeff Kirsher 	void __iomem *addr_memr;
83644570b8SJeff Kirsher 	u8 reg_memr;
84644570b8SJeff Kirsher 	int link;
85644570b8SJeff Kirsher 	int speed;
86644570b8SJeff Kirsher 	int duplex;
87644570b8SJeff Kirsher 
88644570b8SJeff Kirsher 	void __iomem *map2;
89644570b8SJeff Kirsher 	const struct ax_plat_data *plat;
90644570b8SJeff Kirsher 
91644570b8SJeff Kirsher 	unsigned char running;
92644570b8SJeff Kirsher 	unsigned char resume_open;
93644570b8SJeff Kirsher 	unsigned int irqflags;
94644570b8SJeff Kirsher 
95644570b8SJeff Kirsher 	u32 reg_offsets[0x20];
96644570b8SJeff Kirsher };
97644570b8SJeff Kirsher 
to_ax_dev(struct net_device * dev)98644570b8SJeff Kirsher static inline struct ax_device *to_ax_dev(struct net_device *dev)
99644570b8SJeff Kirsher {
100644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
101644570b8SJeff Kirsher 	return (struct ax_device *)(ei_local + 1);
102644570b8SJeff Kirsher }
103644570b8SJeff Kirsher 
ax_NS8390_reinit(struct net_device * dev)104375df5f8SMichael Schmitz void ax_NS8390_reinit(struct net_device *dev)
105375df5f8SMichael Schmitz {
106375df5f8SMichael Schmitz 	ax_NS8390_init(dev, 1);
107375df5f8SMichael Schmitz }
108375df5f8SMichael Schmitz 
109375df5f8SMichael Schmitz EXPORT_SYMBOL_GPL(ax_NS8390_reinit);
110375df5f8SMichael Schmitz 
111644570b8SJeff Kirsher /*
112644570b8SJeff Kirsher  * ax_initial_check
113644570b8SJeff Kirsher  *
11448fc7f7eSAdam Buchbinder  * do an initial probe for the card to check whether it exists
115644570b8SJeff Kirsher  * and is functional
116644570b8SJeff Kirsher  */
ax_initial_check(struct net_device * dev)117644570b8SJeff Kirsher static int ax_initial_check(struct net_device *dev)
118644570b8SJeff Kirsher {
119644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
120644570b8SJeff Kirsher 	void __iomem *ioaddr = ei_local->mem;
121644570b8SJeff Kirsher 	int reg0;
122644570b8SJeff Kirsher 	int regd;
123644570b8SJeff Kirsher 
124644570b8SJeff Kirsher 	reg0 = ei_inb(ioaddr);
125644570b8SJeff Kirsher 	if (reg0 == 0xFF)
126644570b8SJeff Kirsher 		return -ENODEV;
127644570b8SJeff Kirsher 
128644570b8SJeff Kirsher 	ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
129644570b8SJeff Kirsher 	regd = ei_inb(ioaddr + 0x0d);
130644570b8SJeff Kirsher 	ei_outb(0xff, ioaddr + 0x0d);
131644570b8SJeff Kirsher 	ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
132644570b8SJeff Kirsher 	ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
133644570b8SJeff Kirsher 	if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
134644570b8SJeff Kirsher 		ei_outb(reg0, ioaddr);
135644570b8SJeff Kirsher 		ei_outb(regd, ioaddr + 0x0d);	/* Restore the old values. */
136644570b8SJeff Kirsher 		return -ENODEV;
137644570b8SJeff Kirsher 	}
138644570b8SJeff Kirsher 
139644570b8SJeff Kirsher 	return 0;
140644570b8SJeff Kirsher }
141644570b8SJeff Kirsher 
142644570b8SJeff Kirsher /*
143644570b8SJeff Kirsher  * Hard reset the card. This used to pause for the same period that a
144644570b8SJeff Kirsher  * 8390 reset command required, but that shouldn't be necessary.
145644570b8SJeff Kirsher  */
ax_reset_8390(struct net_device * dev)146644570b8SJeff Kirsher static void ax_reset_8390(struct net_device *dev)
147644570b8SJeff Kirsher {
148644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
149644570b8SJeff Kirsher 	unsigned long reset_start_time = jiffies;
150644570b8SJeff Kirsher 	void __iomem *addr = (void __iomem *)dev->base_addr;
151644570b8SJeff Kirsher 
152c45f812fSMatthew Whitehead 	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
153644570b8SJeff Kirsher 
154644570b8SJeff Kirsher 	ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
155644570b8SJeff Kirsher 
156644570b8SJeff Kirsher 	ei_local->txing = 0;
157644570b8SJeff Kirsher 	ei_local->dmaing = 0;
158644570b8SJeff Kirsher 
159644570b8SJeff Kirsher 	/* This check _should_not_ be necessary, omit eventually. */
160644570b8SJeff Kirsher 	while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
16103b8dcbaSManuel Schölling 		if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
162644570b8SJeff Kirsher 			netdev_warn(dev, "%s: did not complete.\n", __func__);
163644570b8SJeff Kirsher 			break;
164644570b8SJeff Kirsher 		}
165644570b8SJeff Kirsher 	}
166644570b8SJeff Kirsher 
167644570b8SJeff Kirsher 	ei_outb(ENISR_RESET, addr + EN0_ISR);	/* Ack intr. */
168644570b8SJeff Kirsher }
169644570b8SJeff Kirsher 
170cec4c1c5SMichael Karcher /* Wrapper for __ei_interrupt for platforms that have a platform-specific
171cec4c1c5SMichael Karcher  * way to find out whether the interrupt request might be caused by
172cec4c1c5SMichael Karcher  * the ax88796 chip.
173cec4c1c5SMichael Karcher  */
ax_ei_interrupt_filtered(int irq,void * dev_id)174cec4c1c5SMichael Karcher static irqreturn_t ax_ei_interrupt_filtered(int irq, void *dev_id)
175cec4c1c5SMichael Karcher {
176cec4c1c5SMichael Karcher 	struct net_device *dev = dev_id;
177cec4c1c5SMichael Karcher 	struct ax_device *ax = to_ax_dev(dev);
178cec4c1c5SMichael Karcher 	struct platform_device *pdev = to_platform_device(dev->dev.parent);
179cec4c1c5SMichael Karcher 
180cec4c1c5SMichael Karcher 	if (!ax->plat->check_irq(pdev))
181cec4c1c5SMichael Karcher 		return IRQ_NONE;
182cec4c1c5SMichael Karcher 
183cec4c1c5SMichael Karcher 	return ax_ei_interrupt(irq, dev_id);
184cec4c1c5SMichael Karcher }
185644570b8SJeff Kirsher 
ax_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)186644570b8SJeff Kirsher static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
187644570b8SJeff Kirsher 			    int ring_page)
188644570b8SJeff Kirsher {
189644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
190644570b8SJeff Kirsher 	void __iomem *nic_base = ei_local->mem;
191644570b8SJeff Kirsher 
192644570b8SJeff Kirsher 	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
193644570b8SJeff Kirsher 	if (ei_local->dmaing) {
194644570b8SJeff Kirsher 		netdev_err(dev, "DMAing conflict in %s "
195644570b8SJeff Kirsher 			"[DMAstat:%d][irqlock:%d].\n",
196644570b8SJeff Kirsher 			__func__,
197644570b8SJeff Kirsher 			ei_local->dmaing, ei_local->irqlock);
198644570b8SJeff Kirsher 		return;
199644570b8SJeff Kirsher 	}
200644570b8SJeff Kirsher 
201644570b8SJeff Kirsher 	ei_local->dmaing |= 0x01;
202644570b8SJeff Kirsher 	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
203644570b8SJeff Kirsher 	ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
204644570b8SJeff Kirsher 	ei_outb(0, nic_base + EN0_RCNTHI);
205644570b8SJeff Kirsher 	ei_outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
206644570b8SJeff Kirsher 	ei_outb(ring_page, nic_base + EN0_RSARHI);
207644570b8SJeff Kirsher 	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
208644570b8SJeff Kirsher 
209644570b8SJeff Kirsher 	if (ei_local->word16)
210b146ecd6SMatthew Leach 		ioread16_rep(nic_base + NE_DATAPORT, hdr,
211644570b8SJeff Kirsher 			     sizeof(struct e8390_pkt_hdr) >> 1);
212644570b8SJeff Kirsher 	else
213b146ecd6SMatthew Leach 		ioread8_rep(nic_base + NE_DATAPORT, hdr,
214644570b8SJeff Kirsher 			    sizeof(struct e8390_pkt_hdr));
215644570b8SJeff Kirsher 
216644570b8SJeff Kirsher 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
217644570b8SJeff Kirsher 	ei_local->dmaing &= ~0x01;
218644570b8SJeff Kirsher 
219644570b8SJeff Kirsher 	le16_to_cpus(&hdr->count);
220644570b8SJeff Kirsher }
221644570b8SJeff Kirsher 
222644570b8SJeff Kirsher 
223644570b8SJeff Kirsher /*
224644570b8SJeff Kirsher  * Block input and output, similar to the Crynwr packet driver. If
225644570b8SJeff Kirsher  * you are porting to a new ethercard, look at the packet driver
226644570b8SJeff Kirsher  * source for hints. The NEx000 doesn't share the on-board packet
227644570b8SJeff Kirsher  * memory -- you have to put the packet out through the "remote DMA"
228644570b8SJeff Kirsher  * dataport using ei_outb.
229644570b8SJeff Kirsher  */
ax_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)230644570b8SJeff Kirsher static void ax_block_input(struct net_device *dev, int count,
231644570b8SJeff Kirsher 			   struct sk_buff *skb, int ring_offset)
232644570b8SJeff Kirsher {
233644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
234644570b8SJeff Kirsher 	void __iomem *nic_base = ei_local->mem;
235644570b8SJeff Kirsher 	char *buf = skb->data;
236644570b8SJeff Kirsher 
237644570b8SJeff Kirsher 	if (ei_local->dmaing) {
238644570b8SJeff Kirsher 		netdev_err(dev,
239644570b8SJeff Kirsher 			"DMAing conflict in %s "
240644570b8SJeff Kirsher 			"[DMAstat:%d][irqlock:%d].\n",
241644570b8SJeff Kirsher 			__func__,
242644570b8SJeff Kirsher 			ei_local->dmaing, ei_local->irqlock);
243644570b8SJeff Kirsher 		return;
244644570b8SJeff Kirsher 	}
245644570b8SJeff Kirsher 
246644570b8SJeff Kirsher 	ei_local->dmaing |= 0x01;
247644570b8SJeff Kirsher 
248644570b8SJeff Kirsher 	ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
249644570b8SJeff Kirsher 	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
250644570b8SJeff Kirsher 	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
251644570b8SJeff Kirsher 	ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
252644570b8SJeff Kirsher 	ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
253644570b8SJeff Kirsher 	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
254644570b8SJeff Kirsher 
255644570b8SJeff Kirsher 	if (ei_local->word16) {
256b146ecd6SMatthew Leach 		ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
257644570b8SJeff Kirsher 		if (count & 0x01)
258644570b8SJeff Kirsher 			buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
259644570b8SJeff Kirsher 
260644570b8SJeff Kirsher 	} else {
261b146ecd6SMatthew Leach 		ioread8_rep(nic_base + NE_DATAPORT, buf, count);
262644570b8SJeff Kirsher 	}
263644570b8SJeff Kirsher 
264644570b8SJeff Kirsher 	ei_local->dmaing &= ~1;
265644570b8SJeff Kirsher }
266644570b8SJeff Kirsher 
ax_block_output(struct net_device * dev,int count,const unsigned char * buf,const int start_page)267644570b8SJeff Kirsher static void ax_block_output(struct net_device *dev, int count,
268644570b8SJeff Kirsher 			    const unsigned char *buf, const int start_page)
269644570b8SJeff Kirsher {
270644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
271644570b8SJeff Kirsher 	void __iomem *nic_base = ei_local->mem;
272644570b8SJeff Kirsher 	unsigned long dma_start;
273644570b8SJeff Kirsher 
274644570b8SJeff Kirsher 	/*
275644570b8SJeff Kirsher 	 * Round the count up for word writes. Do we need to do this?
276644570b8SJeff Kirsher 	 * What effect will an odd byte count have on the 8390?  I
277644570b8SJeff Kirsher 	 * should check someday.
278644570b8SJeff Kirsher 	 */
279644570b8SJeff Kirsher 	if (ei_local->word16 && (count & 0x01))
280644570b8SJeff Kirsher 		count++;
281644570b8SJeff Kirsher 
282644570b8SJeff Kirsher 	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
283644570b8SJeff Kirsher 	if (ei_local->dmaing) {
284644570b8SJeff Kirsher 		netdev_err(dev, "DMAing conflict in %s."
285644570b8SJeff Kirsher 			"[DMAstat:%d][irqlock:%d]\n",
286644570b8SJeff Kirsher 			__func__,
287644570b8SJeff Kirsher 		       ei_local->dmaing, ei_local->irqlock);
288644570b8SJeff Kirsher 		return;
289644570b8SJeff Kirsher 	}
290644570b8SJeff Kirsher 
291644570b8SJeff Kirsher 	ei_local->dmaing |= 0x01;
292644570b8SJeff Kirsher 	/* We should already be in page 0, but to be safe... */
293644570b8SJeff Kirsher 	ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
294644570b8SJeff Kirsher 
295644570b8SJeff Kirsher 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);
296644570b8SJeff Kirsher 
297644570b8SJeff Kirsher 	/* Now the normal output. */
298644570b8SJeff Kirsher 	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
299644570b8SJeff Kirsher 	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
300644570b8SJeff Kirsher 	ei_outb(0x00, nic_base + EN0_RSARLO);
301644570b8SJeff Kirsher 	ei_outb(start_page, nic_base + EN0_RSARHI);
302644570b8SJeff Kirsher 
303644570b8SJeff Kirsher 	ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
304644570b8SJeff Kirsher 	if (ei_local->word16)
305b146ecd6SMatthew Leach 		iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
306644570b8SJeff Kirsher 	else
307b146ecd6SMatthew Leach 		iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
308644570b8SJeff Kirsher 
309644570b8SJeff Kirsher 	dma_start = jiffies;
310644570b8SJeff Kirsher 
311644570b8SJeff Kirsher 	while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
31203b8dcbaSManuel Schölling 		if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
313644570b8SJeff Kirsher 			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
314644570b8SJeff Kirsher 			ax_reset_8390(dev);
315644570b8SJeff Kirsher 			ax_NS8390_init(dev, 1);
316644570b8SJeff Kirsher 			break;
317644570b8SJeff Kirsher 		}
318644570b8SJeff Kirsher 	}
319644570b8SJeff Kirsher 
320644570b8SJeff Kirsher 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
321644570b8SJeff Kirsher 	ei_local->dmaing &= ~0x01;
322644570b8SJeff Kirsher }
323644570b8SJeff Kirsher 
324644570b8SJeff Kirsher /* definitions for accessing MII/EEPROM interface */
325644570b8SJeff Kirsher 
326644570b8SJeff Kirsher #define AX_MEMR			EI_SHIFT(0x14)
327644570b8SJeff Kirsher #define AX_MEMR_MDC		BIT(0)
328644570b8SJeff Kirsher #define AX_MEMR_MDIR		BIT(1)
329644570b8SJeff Kirsher #define AX_MEMR_MDI		BIT(2)
330644570b8SJeff Kirsher #define AX_MEMR_MDO		BIT(3)
331644570b8SJeff Kirsher #define AX_MEMR_EECS		BIT(4)
332644570b8SJeff Kirsher #define AX_MEMR_EEI		BIT(5)
333644570b8SJeff Kirsher #define AX_MEMR_EEO		BIT(6)
334644570b8SJeff Kirsher #define AX_MEMR_EECLK		BIT(7)
335644570b8SJeff Kirsher 
ax_handle_link_change(struct net_device * dev)336644570b8SJeff Kirsher static void ax_handle_link_change(struct net_device *dev)
337644570b8SJeff Kirsher {
338644570b8SJeff Kirsher 	struct ax_device  *ax = to_ax_dev(dev);
339de0eabf8SPhilippe Reynes 	struct phy_device *phy_dev = dev->phydev;
340644570b8SJeff Kirsher 	int status_change = 0;
341644570b8SJeff Kirsher 
342644570b8SJeff Kirsher 	if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
343644570b8SJeff Kirsher 			     (ax->duplex != phy_dev->duplex))) {
344644570b8SJeff Kirsher 
345644570b8SJeff Kirsher 		ax->speed = phy_dev->speed;
346644570b8SJeff Kirsher 		ax->duplex = phy_dev->duplex;
347644570b8SJeff Kirsher 		status_change = 1;
348644570b8SJeff Kirsher 	}
349644570b8SJeff Kirsher 
350644570b8SJeff Kirsher 	if (phy_dev->link != ax->link) {
351644570b8SJeff Kirsher 		if (!phy_dev->link) {
352644570b8SJeff Kirsher 			ax->speed = 0;
353644570b8SJeff Kirsher 			ax->duplex = -1;
354644570b8SJeff Kirsher 		}
355644570b8SJeff Kirsher 		ax->link = phy_dev->link;
356644570b8SJeff Kirsher 
357644570b8SJeff Kirsher 		status_change = 1;
358644570b8SJeff Kirsher 	}
359644570b8SJeff Kirsher 
360644570b8SJeff Kirsher 	if (status_change)
361644570b8SJeff Kirsher 		phy_print_status(phy_dev);
362644570b8SJeff Kirsher }
363644570b8SJeff Kirsher 
ax_mii_probe(struct net_device * dev)364644570b8SJeff Kirsher static int ax_mii_probe(struct net_device *dev)
365644570b8SJeff Kirsher {
366644570b8SJeff Kirsher 	struct ax_device  *ax = to_ax_dev(dev);
367644570b8SJeff Kirsher 	struct phy_device *phy_dev = NULL;
368644570b8SJeff Kirsher 	int ret;
369644570b8SJeff Kirsher 
370644570b8SJeff Kirsher 	/* find the first phy */
371644570b8SJeff Kirsher 	phy_dev = phy_find_first(ax->mii_bus);
372644570b8SJeff Kirsher 	if (!phy_dev) {
373644570b8SJeff Kirsher 		netdev_err(dev, "no PHY found\n");
374644570b8SJeff Kirsher 		return -ENODEV;
375644570b8SJeff Kirsher 	}
376644570b8SJeff Kirsher 
377f9a8f83bSFlorian Fainelli 	ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
378644570b8SJeff Kirsher 				 PHY_INTERFACE_MODE_MII);
379644570b8SJeff Kirsher 	if (ret) {
380644570b8SJeff Kirsher 		netdev_err(dev, "Could not attach to PHY\n");
381644570b8SJeff Kirsher 		return ret;
382644570b8SJeff Kirsher 	}
383644570b8SJeff Kirsher 
38458056c1eSAndrew Lunn 	phy_set_max_speed(phy_dev, SPEED_100);
385644570b8SJeff Kirsher 
386644570b8SJeff Kirsher 	netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
38784eff6d1SAndrew Lunn 		    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
388644570b8SJeff Kirsher 
389644570b8SJeff Kirsher 	return 0;
390644570b8SJeff Kirsher }
391644570b8SJeff Kirsher 
ax_phy_switch(struct net_device * dev,int on)392644570b8SJeff Kirsher static void ax_phy_switch(struct net_device *dev, int on)
393644570b8SJeff Kirsher {
394644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
395644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
396644570b8SJeff Kirsher 
397644570b8SJeff Kirsher 	u8 reg_gpoc =  ax->plat->gpoc_val;
398644570b8SJeff Kirsher 
399644570b8SJeff Kirsher 	if (!!on)
400644570b8SJeff Kirsher 		reg_gpoc &= ~AX_GPOC_PPDSET;
401644570b8SJeff Kirsher 	else
402644570b8SJeff Kirsher 		reg_gpoc |= AX_GPOC_PPDSET;
403644570b8SJeff Kirsher 
404644570b8SJeff Kirsher 	ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
405644570b8SJeff Kirsher }
406644570b8SJeff Kirsher 
ax_bb_mdc(struct mdiobb_ctrl * ctrl,int level)407fd5f375cSMichael Karcher static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
408fd5f375cSMichael Karcher {
409fd5f375cSMichael Karcher 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
410fd5f375cSMichael Karcher 
411fd5f375cSMichael Karcher 	if (level)
412fd5f375cSMichael Karcher 		ax->reg_memr |= AX_MEMR_MDC;
413fd5f375cSMichael Karcher 	else
414fd5f375cSMichael Karcher 		ax->reg_memr &= ~AX_MEMR_MDC;
415fd5f375cSMichael Karcher 
416fd5f375cSMichael Karcher 	ei_outb(ax->reg_memr, ax->addr_memr);
417fd5f375cSMichael Karcher }
418fd5f375cSMichael Karcher 
ax_bb_dir(struct mdiobb_ctrl * ctrl,int output)419fd5f375cSMichael Karcher static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
420fd5f375cSMichael Karcher {
421fd5f375cSMichael Karcher 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
422fd5f375cSMichael Karcher 
423fd5f375cSMichael Karcher 	if (output)
424fd5f375cSMichael Karcher 		ax->reg_memr &= ~AX_MEMR_MDIR;
425fd5f375cSMichael Karcher 	else
426fd5f375cSMichael Karcher 		ax->reg_memr |= AX_MEMR_MDIR;
427fd5f375cSMichael Karcher 
428fd5f375cSMichael Karcher 	ei_outb(ax->reg_memr, ax->addr_memr);
429fd5f375cSMichael Karcher }
430fd5f375cSMichael Karcher 
ax_bb_set_data(struct mdiobb_ctrl * ctrl,int value)431fd5f375cSMichael Karcher static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
432fd5f375cSMichael Karcher {
433fd5f375cSMichael Karcher 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
434fd5f375cSMichael Karcher 
435fd5f375cSMichael Karcher 	if (value)
436fd5f375cSMichael Karcher 		ax->reg_memr |= AX_MEMR_MDO;
437fd5f375cSMichael Karcher 	else
438fd5f375cSMichael Karcher 		ax->reg_memr &= ~AX_MEMR_MDO;
439fd5f375cSMichael Karcher 
440fd5f375cSMichael Karcher 	ei_outb(ax->reg_memr, ax->addr_memr);
441fd5f375cSMichael Karcher }
442fd5f375cSMichael Karcher 
ax_bb_get_data(struct mdiobb_ctrl * ctrl)443fd5f375cSMichael Karcher static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
444fd5f375cSMichael Karcher {
445fd5f375cSMichael Karcher 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
446fd5f375cSMichael Karcher 	int reg_memr = ei_inb(ax->addr_memr);
447fd5f375cSMichael Karcher 
448fd5f375cSMichael Karcher 	return reg_memr & AX_MEMR_MDI ? 1 : 0;
449fd5f375cSMichael Karcher }
450fd5f375cSMichael Karcher 
451fd5f375cSMichael Karcher static const struct mdiobb_ops bb_ops = {
452fd5f375cSMichael Karcher 	.owner = THIS_MODULE,
453fd5f375cSMichael Karcher 	.set_mdc = ax_bb_mdc,
454fd5f375cSMichael Karcher 	.set_mdio_dir = ax_bb_dir,
455fd5f375cSMichael Karcher 	.set_mdio_data = ax_bb_set_data,
456fd5f375cSMichael Karcher 	.get_mdio_data = ax_bb_get_data,
457fd5f375cSMichael Karcher };
458fd5f375cSMichael Karcher 
ax_mii_init(struct net_device * dev)459fd5f375cSMichael Karcher static int ax_mii_init(struct net_device *dev)
460fd5f375cSMichael Karcher {
461fd5f375cSMichael Karcher 	struct platform_device *pdev = to_platform_device(dev->dev.parent);
462fd5f375cSMichael Karcher 	struct ei_device *ei_local = netdev_priv(dev);
463fd5f375cSMichael Karcher 	struct ax_device *ax = to_ax_dev(dev);
464fd5f375cSMichael Karcher 	int err;
465fd5f375cSMichael Karcher 
466fd5f375cSMichael Karcher 	ax->bb_ctrl.ops = &bb_ops;
467fd5f375cSMichael Karcher 	ax->addr_memr = ei_local->mem + AX_MEMR;
468fd5f375cSMichael Karcher 	ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
469fd5f375cSMichael Karcher 	if (!ax->mii_bus) {
470fd5f375cSMichael Karcher 		err = -ENOMEM;
471fd5f375cSMichael Karcher 		goto out;
472fd5f375cSMichael Karcher 	}
473fd5f375cSMichael Karcher 
474fd5f375cSMichael Karcher 	ax->mii_bus->name = "ax88796_mii_bus";
475fd5f375cSMichael Karcher 	ax->mii_bus->parent = dev->dev.parent;
476fd5f375cSMichael Karcher 	snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
477fd5f375cSMichael Karcher 		 pdev->name, pdev->id);
478fd5f375cSMichael Karcher 
479fd5f375cSMichael Karcher 	err = mdiobus_register(ax->mii_bus);
480fd5f375cSMichael Karcher 	if (err)
481fd5f375cSMichael Karcher 		goto out_free_mdio_bitbang;
482fd5f375cSMichael Karcher 
483fd5f375cSMichael Karcher 	return 0;
484fd5f375cSMichael Karcher 
485fd5f375cSMichael Karcher  out_free_mdio_bitbang:
486fd5f375cSMichael Karcher 	free_mdio_bitbang(ax->mii_bus);
487fd5f375cSMichael Karcher  out:
488fd5f375cSMichael Karcher 	return err;
489fd5f375cSMichael Karcher }
490fd5f375cSMichael Karcher 
ax_open(struct net_device * dev)491644570b8SJeff Kirsher static int ax_open(struct net_device *dev)
492644570b8SJeff Kirsher {
493644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
494644570b8SJeff Kirsher 	int ret;
495644570b8SJeff Kirsher 
496644570b8SJeff Kirsher 	netdev_dbg(dev, "open\n");
497644570b8SJeff Kirsher 
498fd5f375cSMichael Karcher 	ret = ax_mii_init(dev);
499fd5f375cSMichael Karcher 	if (ret)
500fd5f375cSMichael Karcher 		goto failed_mii;
501fd5f375cSMichael Karcher 
502cec4c1c5SMichael Karcher 	if (ax->plat->check_irq)
503cec4c1c5SMichael Karcher 		ret = request_irq(dev->irq, ax_ei_interrupt_filtered,
504cec4c1c5SMichael Karcher 				  ax->irqflags, dev->name, dev);
505cec4c1c5SMichael Karcher 	else
506644570b8SJeff Kirsher 		ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
507644570b8SJeff Kirsher 				  dev->name, dev);
508644570b8SJeff Kirsher 	if (ret)
509644570b8SJeff Kirsher 		goto failed_request_irq;
510644570b8SJeff Kirsher 
511644570b8SJeff Kirsher 	/* turn the phy on (if turned off) */
512644570b8SJeff Kirsher 	ax_phy_switch(dev, 1);
513644570b8SJeff Kirsher 
514644570b8SJeff Kirsher 	ret = ax_mii_probe(dev);
515644570b8SJeff Kirsher 	if (ret)
516644570b8SJeff Kirsher 		goto failed_mii_probe;
517de0eabf8SPhilippe Reynes 	phy_start(dev->phydev);
518644570b8SJeff Kirsher 
519644570b8SJeff Kirsher 	ret = ax_ei_open(dev);
520644570b8SJeff Kirsher 	if (ret)
521644570b8SJeff Kirsher 		goto failed_ax_ei_open;
522644570b8SJeff Kirsher 
523644570b8SJeff Kirsher 	ax->running = 1;
524644570b8SJeff Kirsher 
525644570b8SJeff Kirsher 	return 0;
526644570b8SJeff Kirsher 
527644570b8SJeff Kirsher  failed_ax_ei_open:
528de0eabf8SPhilippe Reynes 	phy_disconnect(dev->phydev);
529644570b8SJeff Kirsher  failed_mii_probe:
530644570b8SJeff Kirsher 	ax_phy_switch(dev, 0);
531644570b8SJeff Kirsher 	free_irq(dev->irq, dev);
532644570b8SJeff Kirsher  failed_request_irq:
533fd5f375cSMichael Karcher 	/* unregister mdiobus */
534fd5f375cSMichael Karcher 	mdiobus_unregister(ax->mii_bus);
535fd5f375cSMichael Karcher 	free_mdio_bitbang(ax->mii_bus);
536fd5f375cSMichael Karcher  failed_mii:
537644570b8SJeff Kirsher 	return ret;
538644570b8SJeff Kirsher }
539644570b8SJeff Kirsher 
ax_close(struct net_device * dev)540644570b8SJeff Kirsher static int ax_close(struct net_device *dev)
541644570b8SJeff Kirsher {
542644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
543644570b8SJeff Kirsher 
544644570b8SJeff Kirsher 	netdev_dbg(dev, "close\n");
545644570b8SJeff Kirsher 
546644570b8SJeff Kirsher 	ax->running = 0;
547644570b8SJeff Kirsher 	wmb();
548644570b8SJeff Kirsher 
549644570b8SJeff Kirsher 	ax_ei_close(dev);
550644570b8SJeff Kirsher 
551644570b8SJeff Kirsher 	/* turn the phy off */
552644570b8SJeff Kirsher 	ax_phy_switch(dev, 0);
553de0eabf8SPhilippe Reynes 	phy_disconnect(dev->phydev);
554644570b8SJeff Kirsher 
555644570b8SJeff Kirsher 	free_irq(dev->irq, dev);
556fd5f375cSMichael Karcher 
557fd5f375cSMichael Karcher 	mdiobus_unregister(ax->mii_bus);
558fd5f375cSMichael Karcher 	free_mdio_bitbang(ax->mii_bus);
559644570b8SJeff Kirsher 	return 0;
560644570b8SJeff Kirsher }
561644570b8SJeff Kirsher 
ax_ioctl(struct net_device * dev,struct ifreq * req,int cmd)562644570b8SJeff Kirsher static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
563644570b8SJeff Kirsher {
564de0eabf8SPhilippe Reynes 	struct phy_device *phy_dev = dev->phydev;
565644570b8SJeff Kirsher 
566644570b8SJeff Kirsher 	if (!netif_running(dev))
567644570b8SJeff Kirsher 		return -EINVAL;
568644570b8SJeff Kirsher 
569644570b8SJeff Kirsher 	if (!phy_dev)
570644570b8SJeff Kirsher 		return -ENODEV;
571644570b8SJeff Kirsher 
572644570b8SJeff Kirsher 	return phy_mii_ioctl(phy_dev, req, cmd);
573644570b8SJeff Kirsher }
574644570b8SJeff Kirsher 
575644570b8SJeff Kirsher /* ethtool ops */
576644570b8SJeff Kirsher 
ax_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)577644570b8SJeff Kirsher static void ax_get_drvinfo(struct net_device *dev,
578644570b8SJeff Kirsher 			   struct ethtool_drvinfo *info)
579644570b8SJeff Kirsher {
580644570b8SJeff Kirsher 	struct platform_device *pdev = to_platform_device(dev->dev.parent);
581644570b8SJeff Kirsher 
582*f029c781SWolfram Sang 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
583*f029c781SWolfram Sang 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
584*f029c781SWolfram Sang 	strscpy(info->bus_info, pdev->name, sizeof(info->bus_info));
585644570b8SJeff Kirsher }
586644570b8SJeff Kirsher 
ax_get_msglevel(struct net_device * dev)587c45f812fSMatthew Whitehead static u32 ax_get_msglevel(struct net_device *dev)
588c45f812fSMatthew Whitehead {
589c45f812fSMatthew Whitehead 	struct ei_device *ei_local = netdev_priv(dev);
590c45f812fSMatthew Whitehead 
591c45f812fSMatthew Whitehead 	return ei_local->msg_enable;
592c45f812fSMatthew Whitehead }
593c45f812fSMatthew Whitehead 
ax_set_msglevel(struct net_device * dev,u32 v)594c45f812fSMatthew Whitehead static void ax_set_msglevel(struct net_device *dev, u32 v)
595c45f812fSMatthew Whitehead {
596c45f812fSMatthew Whitehead 	struct ei_device *ei_local = netdev_priv(dev);
597c45f812fSMatthew Whitehead 
598c45f812fSMatthew Whitehead 	ei_local->msg_enable = v;
599c45f812fSMatthew Whitehead }
600c45f812fSMatthew Whitehead 
601644570b8SJeff Kirsher static const struct ethtool_ops ax_ethtool_ops = {
602644570b8SJeff Kirsher 	.get_drvinfo		= ax_get_drvinfo,
603644570b8SJeff Kirsher 	.get_link		= ethtool_op_get_link,
6042241b67bSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
605c45f812fSMatthew Whitehead 	.get_msglevel		= ax_get_msglevel,
606c45f812fSMatthew Whitehead 	.set_msglevel		= ax_set_msglevel,
607d21cfb37SPhilippe Reynes 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
608d21cfb37SPhilippe Reynes 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
609644570b8SJeff Kirsher };
610644570b8SJeff Kirsher 
611644570b8SJeff Kirsher #ifdef CONFIG_AX88796_93CX6
ax_eeprom_register_read(struct eeprom_93cx6 * eeprom)612644570b8SJeff Kirsher static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
613644570b8SJeff Kirsher {
614644570b8SJeff Kirsher 	struct ei_device *ei_local = eeprom->data;
615644570b8SJeff Kirsher 	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
616644570b8SJeff Kirsher 
617644570b8SJeff Kirsher 	eeprom->reg_data_in = reg & AX_MEMR_EEI;
618644570b8SJeff Kirsher 	eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
619644570b8SJeff Kirsher 	eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
620644570b8SJeff Kirsher 	eeprom->reg_chip_select = reg & AX_MEMR_EECS;
621644570b8SJeff Kirsher }
622644570b8SJeff Kirsher 
ax_eeprom_register_write(struct eeprom_93cx6 * eeprom)623644570b8SJeff Kirsher static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
624644570b8SJeff Kirsher {
625644570b8SJeff Kirsher 	struct ei_device *ei_local = eeprom->data;
626644570b8SJeff Kirsher 	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
627644570b8SJeff Kirsher 
628644570b8SJeff Kirsher 	reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
629644570b8SJeff Kirsher 
630644570b8SJeff Kirsher 	if (eeprom->reg_data_in)
631644570b8SJeff Kirsher 		reg |= AX_MEMR_EEI;
632644570b8SJeff Kirsher 	if (eeprom->reg_data_clock)
633644570b8SJeff Kirsher 		reg |= AX_MEMR_EECLK;
634644570b8SJeff Kirsher 	if (eeprom->reg_chip_select)
635644570b8SJeff Kirsher 		reg |= AX_MEMR_EECS;
636644570b8SJeff Kirsher 
637644570b8SJeff Kirsher 	ei_outb(reg, ei_local->mem + AX_MEMR);
638644570b8SJeff Kirsher 	udelay(10);
639644570b8SJeff Kirsher }
640644570b8SJeff Kirsher #endif
641644570b8SJeff Kirsher 
642644570b8SJeff Kirsher static const struct net_device_ops ax_netdev_ops = {
643644570b8SJeff Kirsher 	.ndo_open		= ax_open,
644644570b8SJeff Kirsher 	.ndo_stop		= ax_close,
645a7605370SArnd Bergmann 	.ndo_eth_ioctl		= ax_ioctl,
646644570b8SJeff Kirsher 
647644570b8SJeff Kirsher 	.ndo_start_xmit		= ax_ei_start_xmit,
648644570b8SJeff Kirsher 	.ndo_tx_timeout		= ax_ei_tx_timeout,
649644570b8SJeff Kirsher 	.ndo_get_stats		= ax_ei_get_stats,
650afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= ax_ei_set_multicast_list,
651644570b8SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
652644570b8SJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
653644570b8SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
654644570b8SJeff Kirsher 	.ndo_poll_controller	= ax_ei_poll,
655644570b8SJeff Kirsher #endif
656644570b8SJeff Kirsher };
657644570b8SJeff Kirsher 
658644570b8SJeff Kirsher /* setup code */
659644570b8SJeff Kirsher 
ax_initial_setup(struct net_device * dev,struct ei_device * ei_local)660644570b8SJeff Kirsher static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
661644570b8SJeff Kirsher {
662644570b8SJeff Kirsher 	void __iomem *ioaddr = ei_local->mem;
663644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
664644570b8SJeff Kirsher 
665644570b8SJeff Kirsher 	/* Select page 0 */
666644570b8SJeff Kirsher 	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
667644570b8SJeff Kirsher 
668644570b8SJeff Kirsher 	/* set to byte access */
669644570b8SJeff Kirsher 	ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
670644570b8SJeff Kirsher 	ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
671644570b8SJeff Kirsher }
672644570b8SJeff Kirsher 
673644570b8SJeff Kirsher /*
674644570b8SJeff Kirsher  * ax_init_dev
675644570b8SJeff Kirsher  *
676644570b8SJeff Kirsher  * initialise the specified device, taking care to note the MAC
677644570b8SJeff Kirsher  * address it may already have (if configured), ensure
678644570b8SJeff Kirsher  * the device is ready to be used by lib8390.c and registerd with
679644570b8SJeff Kirsher  * the network layer.
680644570b8SJeff Kirsher  */
ax_init_dev(struct net_device * dev)681644570b8SJeff Kirsher static int ax_init_dev(struct net_device *dev)
682644570b8SJeff Kirsher {
683644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
684644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
685644570b8SJeff Kirsher 	void __iomem *ioaddr = ei_local->mem;
686644570b8SJeff Kirsher 	unsigned int start_page;
687644570b8SJeff Kirsher 	unsigned int stop_page;
688644570b8SJeff Kirsher 	int ret;
689644570b8SJeff Kirsher 	int i;
690644570b8SJeff Kirsher 
691644570b8SJeff Kirsher 	ret = ax_initial_check(dev);
692644570b8SJeff Kirsher 	if (ret)
693644570b8SJeff Kirsher 		goto err_out;
694644570b8SJeff Kirsher 
695644570b8SJeff Kirsher 	/* setup goes here */
696644570b8SJeff Kirsher 
697644570b8SJeff Kirsher 	ax_initial_setup(dev, ei_local);
698644570b8SJeff Kirsher 
699644570b8SJeff Kirsher 	/* read the mac from the card prom if we need it */
700644570b8SJeff Kirsher 
701644570b8SJeff Kirsher 	if (ax->plat->flags & AXFLG_HAS_EEPROM) {
702644570b8SJeff Kirsher 		unsigned char SA_prom[32];
703644570b8SJeff Kirsher 
7047a0907bdSMichael Karcher 		ei_outb(6, ioaddr + EN0_RCNTLO);
7057a0907bdSMichael Karcher 		ei_outb(0, ioaddr + EN0_RCNTHI);
7067a0907bdSMichael Karcher 		ei_outb(0, ioaddr + EN0_RSARLO);
7077a0907bdSMichael Karcher 		ei_outb(0, ioaddr + EN0_RSARHI);
7087a0907bdSMichael Karcher 		ei_outb(E8390_RREAD + E8390_START, ioaddr + NE_CMD);
709644570b8SJeff Kirsher 		for (i = 0; i < sizeof(SA_prom); i += 2) {
710644570b8SJeff Kirsher 			SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
711644570b8SJeff Kirsher 			SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
712644570b8SJeff Kirsher 		}
7137a0907bdSMichael Karcher 		ei_outb(ENISR_RDC, ioaddr + EN0_ISR);	/* Ack intr. */
714644570b8SJeff Kirsher 
715644570b8SJeff Kirsher 		if (ax->plat->wordlength == 2)
716644570b8SJeff Kirsher 			for (i = 0; i < 16; i++)
717644570b8SJeff Kirsher 				SA_prom[i] = SA_prom[i+i];
718644570b8SJeff Kirsher 
719a96d317fSJakub Kicinski 		eth_hw_addr_set(dev, SA_prom);
720644570b8SJeff Kirsher 	}
721644570b8SJeff Kirsher 
722644570b8SJeff Kirsher #ifdef CONFIG_AX88796_93CX6
723644570b8SJeff Kirsher 	if (ax->plat->flags & AXFLG_HAS_93CX6) {
7241409a932SJoe Perches 		unsigned char mac_addr[ETH_ALEN];
725644570b8SJeff Kirsher 		struct eeprom_93cx6 eeprom;
726644570b8SJeff Kirsher 
727644570b8SJeff Kirsher 		eeprom.data = ei_local;
728644570b8SJeff Kirsher 		eeprom.register_read = ax_eeprom_register_read;
729644570b8SJeff Kirsher 		eeprom.register_write = ax_eeprom_register_write;
730644570b8SJeff Kirsher 		eeprom.width = PCI_EEPROM_WIDTH_93C56;
731644570b8SJeff Kirsher 
732644570b8SJeff Kirsher 		eeprom_93cx6_multiread(&eeprom, 0,
733644570b8SJeff Kirsher 				       (__le16 __force *)mac_addr,
734644570b8SJeff Kirsher 				       sizeof(mac_addr) >> 1);
735644570b8SJeff Kirsher 
736a96d317fSJakub Kicinski 		eth_hw_addr_set(dev, mac_addr);
737644570b8SJeff Kirsher 	}
738644570b8SJeff Kirsher #endif
739644570b8SJeff Kirsher 	if (ax->plat->wordlength == 2) {
740644570b8SJeff Kirsher 		/* We must set the 8390 for word mode. */
741644570b8SJeff Kirsher 		ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
742644570b8SJeff Kirsher 		start_page = NESM_START_PG;
743644570b8SJeff Kirsher 		stop_page = NESM_STOP_PG;
744644570b8SJeff Kirsher 	} else {
745644570b8SJeff Kirsher 		start_page = NE1SM_START_PG;
746644570b8SJeff Kirsher 		stop_page = NE1SM_STOP_PG;
747644570b8SJeff Kirsher 	}
748644570b8SJeff Kirsher 
749644570b8SJeff Kirsher 	/* load the mac-address from the device */
750644570b8SJeff Kirsher 	if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
7518ce218b6SJakub Kicinski 		u8 addr[ETH_ALEN];
7528ce218b6SJakub Kicinski 
753644570b8SJeff Kirsher 		ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
754644570b8SJeff Kirsher 			ei_local->mem + E8390_CMD); /* 0x61 */
755104bf3fbSJoe Perches 		for (i = 0; i < ETH_ALEN; i++)
7568ce218b6SJakub Kicinski 			addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
7578ce218b6SJakub Kicinski 		eth_hw_addr_set(dev, addr);
758644570b8SJeff Kirsher 	}
759644570b8SJeff Kirsher 
760644570b8SJeff Kirsher 	if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
761644570b8SJeff Kirsher 	    ax->plat->mac_addr)
762a96d317fSJakub Kicinski 		eth_hw_addr_set(dev, ax->plat->mac_addr);
763644570b8SJeff Kirsher 
76403d1da3cSUwe Kleine-König 	if (!is_valid_ether_addr(dev->dev_addr)) {
76503d1da3cSUwe Kleine-König 		eth_hw_addr_random(dev);
76603d1da3cSUwe Kleine-König 		dev_info(&dev->dev, "Using random MAC address: %pM\n",
76703d1da3cSUwe Kleine-König 			 dev->dev_addr);
76803d1da3cSUwe Kleine-König 	}
76903d1da3cSUwe Kleine-König 
770644570b8SJeff Kirsher 	ax_reset_8390(dev);
771644570b8SJeff Kirsher 
772644570b8SJeff Kirsher 	ei_local->name = "AX88796";
773644570b8SJeff Kirsher 	ei_local->tx_start_page = start_page;
774644570b8SJeff Kirsher 	ei_local->stop_page = stop_page;
775644570b8SJeff Kirsher 	ei_local->word16 = (ax->plat->wordlength == 2);
776644570b8SJeff Kirsher 	ei_local->rx_start_page = start_page + TX_PAGES;
777644570b8SJeff Kirsher 
778644570b8SJeff Kirsher #ifdef PACKETBUF_MEMSIZE
779644570b8SJeff Kirsher 	/* Allow the packet buffer size to be overridden by know-it-alls. */
780644570b8SJeff Kirsher 	ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
781644570b8SJeff Kirsher #endif
782644570b8SJeff Kirsher 
783644570b8SJeff Kirsher 	ei_local->reset_8390 = &ax_reset_8390;
78427cced20SMichael Karcher 	if (ax->plat->block_input)
78527cced20SMichael Karcher 		ei_local->block_input = ax->plat->block_input;
78627cced20SMichael Karcher 	else
787644570b8SJeff Kirsher 		ei_local->block_input = &ax_block_input;
78827cced20SMichael Karcher 	if (ax->plat->block_output)
78927cced20SMichael Karcher 		ei_local->block_output = ax->plat->block_output;
79027cced20SMichael Karcher 	else
791644570b8SJeff Kirsher 		ei_local->block_output = &ax_block_output;
792644570b8SJeff Kirsher 	ei_local->get_8390_hdr = &ax_get_8390_hdr;
793644570b8SJeff Kirsher 	ei_local->priv = 0;
794644570b8SJeff Kirsher 
795644570b8SJeff Kirsher 	dev->netdev_ops = &ax_netdev_ops;
796644570b8SJeff Kirsher 	dev->ethtool_ops = &ax_ethtool_ops;
797644570b8SJeff Kirsher 
798644570b8SJeff Kirsher 	ax_NS8390_init(dev, 0);
799644570b8SJeff Kirsher 
800644570b8SJeff Kirsher 	ret = register_netdev(dev);
801644570b8SJeff Kirsher 	if (ret)
80282533ad9SUwe Kleine-König 		goto err_out;
803644570b8SJeff Kirsher 
804644570b8SJeff Kirsher 	netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
805644570b8SJeff Kirsher 		    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
806644570b8SJeff Kirsher 		    dev->dev_addr);
807644570b8SJeff Kirsher 
808644570b8SJeff Kirsher 	return 0;
809644570b8SJeff Kirsher 
810644570b8SJeff Kirsher  err_out:
811644570b8SJeff Kirsher 	return ret;
812644570b8SJeff Kirsher }
813644570b8SJeff Kirsher 
ax_remove(struct platform_device * pdev)814644570b8SJeff Kirsher static int ax_remove(struct platform_device *pdev)
815644570b8SJeff Kirsher {
816644570b8SJeff Kirsher 	struct net_device *dev = platform_get_drvdata(pdev);
817644570b8SJeff Kirsher 	struct ei_device *ei_local = netdev_priv(dev);
818644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(dev);
819644570b8SJeff Kirsher 	struct resource *mem;
820644570b8SJeff Kirsher 
821644570b8SJeff Kirsher 	unregister_netdev(dev);
822644570b8SJeff Kirsher 
823644570b8SJeff Kirsher 	iounmap(ei_local->mem);
824644570b8SJeff Kirsher 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
825644570b8SJeff Kirsher 	release_mem_region(mem->start, resource_size(mem));
826644570b8SJeff Kirsher 
827644570b8SJeff Kirsher 	if (ax->map2) {
828644570b8SJeff Kirsher 		iounmap(ax->map2);
829644570b8SJeff Kirsher 		mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
830644570b8SJeff Kirsher 		release_mem_region(mem->start, resource_size(mem));
831644570b8SJeff Kirsher 	}
832644570b8SJeff Kirsher 
833453da988SMichael Schmitz 	platform_set_drvdata(pdev, NULL);
834644570b8SJeff Kirsher 	free_netdev(dev);
835644570b8SJeff Kirsher 
836644570b8SJeff Kirsher 	return 0;
837644570b8SJeff Kirsher }
838644570b8SJeff Kirsher 
839644570b8SJeff Kirsher /*
840644570b8SJeff Kirsher  * ax_probe
841644570b8SJeff Kirsher  *
842644570b8SJeff Kirsher  * This is the entry point when the platform device system uses to
843644570b8SJeff Kirsher  * notify us of a new device to attach to. Allocate memory, find the
844644570b8SJeff Kirsher  * resources and information passed, and map the necessary registers.
845644570b8SJeff Kirsher  */
ax_probe(struct platform_device * pdev)846644570b8SJeff Kirsher static int ax_probe(struct platform_device *pdev)
847644570b8SJeff Kirsher {
848644570b8SJeff Kirsher 	struct net_device *dev;
849644570b8SJeff Kirsher 	struct ei_device *ei_local;
850644570b8SJeff Kirsher 	struct ax_device *ax;
851644570b8SJeff Kirsher 	struct resource *irq, *mem, *mem2;
852b261c20fSArnd Bergmann 	unsigned long mem_size, mem2_size = 0;
853644570b8SJeff Kirsher 	int ret = 0;
854644570b8SJeff Kirsher 
855644570b8SJeff Kirsher 	dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
856644570b8SJeff Kirsher 	if (dev == NULL)
857644570b8SJeff Kirsher 		return -ENOMEM;
858644570b8SJeff Kirsher 
859644570b8SJeff Kirsher 	/* ok, let's setup our device */
860644570b8SJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
861644570b8SJeff Kirsher 	ei_local = netdev_priv(dev);
862644570b8SJeff Kirsher 	ax = to_ax_dev(dev);
863644570b8SJeff Kirsher 
864a3ea2800SJingoo Han 	ax->plat = dev_get_platdata(&pdev->dev);
865644570b8SJeff Kirsher 	platform_set_drvdata(pdev, dev);
866644570b8SJeff Kirsher 
867644570b8SJeff Kirsher 	ei_local->rxcr_base = ax->plat->rcr_val;
868644570b8SJeff Kirsher 
869644570b8SJeff Kirsher 	/* find the platform resources */
870644570b8SJeff Kirsher 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
871644570b8SJeff Kirsher 	if (!irq) {
872644570b8SJeff Kirsher 		dev_err(&pdev->dev, "no IRQ specified\n");
873644570b8SJeff Kirsher 		ret = -ENXIO;
874644570b8SJeff Kirsher 		goto exit_mem;
875644570b8SJeff Kirsher 	}
876644570b8SJeff Kirsher 
877644570b8SJeff Kirsher 	dev->irq = irq->start;
878644570b8SJeff Kirsher 	ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
879644570b8SJeff Kirsher 
880caaf45a6SMichael Karcher 	if (irq->flags &  IORESOURCE_IRQ_SHAREABLE)
881caaf45a6SMichael Karcher 		ax->irqflags |= IRQF_SHARED;
882caaf45a6SMichael Karcher 
883644570b8SJeff Kirsher 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
884644570b8SJeff Kirsher 	if (!mem) {
885644570b8SJeff Kirsher 		dev_err(&pdev->dev, "no MEM specified\n");
886644570b8SJeff Kirsher 		ret = -ENXIO;
887644570b8SJeff Kirsher 		goto exit_mem;
888644570b8SJeff Kirsher 	}
889644570b8SJeff Kirsher 
890644570b8SJeff Kirsher 	mem_size = resource_size(mem);
891644570b8SJeff Kirsher 
892644570b8SJeff Kirsher 	/*
893644570b8SJeff Kirsher 	 * setup the register offsets from either the platform data or
894644570b8SJeff Kirsher 	 * by using the size of the resource provided
895644570b8SJeff Kirsher 	 */
896644570b8SJeff Kirsher 	if (ax->plat->reg_offsets)
897644570b8SJeff Kirsher 		ei_local->reg_offset = ax->plat->reg_offsets;
898644570b8SJeff Kirsher 	else {
899644570b8SJeff Kirsher 		ei_local->reg_offset = ax->reg_offsets;
900644570b8SJeff Kirsher 		for (ret = 0; ret < 0x18; ret++)
901644570b8SJeff Kirsher 			ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
902644570b8SJeff Kirsher 	}
903644570b8SJeff Kirsher 
904644570b8SJeff Kirsher 	if (!request_mem_region(mem->start, mem_size, pdev->name)) {
905644570b8SJeff Kirsher 		dev_err(&pdev->dev, "cannot reserve registers\n");
906644570b8SJeff Kirsher 		ret = -ENXIO;
907644570b8SJeff Kirsher 		goto exit_mem;
908644570b8SJeff Kirsher 	}
909644570b8SJeff Kirsher 
910644570b8SJeff Kirsher 	ei_local->mem = ioremap(mem->start, mem_size);
911644570b8SJeff Kirsher 	dev->base_addr = (unsigned long)ei_local->mem;
912644570b8SJeff Kirsher 
913644570b8SJeff Kirsher 	if (ei_local->mem == NULL) {
914644570b8SJeff Kirsher 		dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
915644570b8SJeff Kirsher 
916644570b8SJeff Kirsher 		ret = -ENXIO;
917644570b8SJeff Kirsher 		goto exit_req;
918644570b8SJeff Kirsher 	}
919644570b8SJeff Kirsher 
920644570b8SJeff Kirsher 	/* look for reset area */
921644570b8SJeff Kirsher 	mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
922644570b8SJeff Kirsher 	if (!mem2) {
923644570b8SJeff Kirsher 		if (!ax->plat->reg_offsets) {
924644570b8SJeff Kirsher 			for (ret = 0; ret < 0x20; ret++)
925644570b8SJeff Kirsher 				ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
926644570b8SJeff Kirsher 		}
927644570b8SJeff Kirsher 	} else {
928644570b8SJeff Kirsher 		mem2_size = resource_size(mem2);
929644570b8SJeff Kirsher 
930644570b8SJeff Kirsher 		if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
931644570b8SJeff Kirsher 			dev_err(&pdev->dev, "cannot reserve registers\n");
932644570b8SJeff Kirsher 			ret = -ENXIO;
933644570b8SJeff Kirsher 			goto exit_mem1;
934644570b8SJeff Kirsher 		}
935644570b8SJeff Kirsher 
936644570b8SJeff Kirsher 		ax->map2 = ioremap(mem2->start, mem2_size);
937644570b8SJeff Kirsher 		if (!ax->map2) {
938644570b8SJeff Kirsher 			dev_err(&pdev->dev, "cannot map reset register\n");
939644570b8SJeff Kirsher 			ret = -ENXIO;
940644570b8SJeff Kirsher 			goto exit_mem2;
941644570b8SJeff Kirsher 		}
942644570b8SJeff Kirsher 
943644570b8SJeff Kirsher 		ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
944644570b8SJeff Kirsher 	}
945644570b8SJeff Kirsher 
946644570b8SJeff Kirsher 	/* got resources, now initialise and register device */
947644570b8SJeff Kirsher 	ret = ax_init_dev(dev);
948644570b8SJeff Kirsher 	if (!ret)
949644570b8SJeff Kirsher 		return 0;
950644570b8SJeff Kirsher 
951644570b8SJeff Kirsher 	if (!ax->map2)
952644570b8SJeff Kirsher 		goto exit_mem1;
953644570b8SJeff Kirsher 
954644570b8SJeff Kirsher 	iounmap(ax->map2);
955644570b8SJeff Kirsher 
956644570b8SJeff Kirsher  exit_mem2:
95709d306d7Sxypron.glpk@gmx.de 	if (mem2)
958644570b8SJeff Kirsher 		release_mem_region(mem2->start, mem2_size);
959644570b8SJeff Kirsher 
960644570b8SJeff Kirsher  exit_mem1:
961644570b8SJeff Kirsher 	iounmap(ei_local->mem);
962644570b8SJeff Kirsher 
963644570b8SJeff Kirsher  exit_req:
964644570b8SJeff Kirsher 	release_mem_region(mem->start, mem_size);
965644570b8SJeff Kirsher 
966644570b8SJeff Kirsher  exit_mem:
967453da988SMichael Schmitz 	platform_set_drvdata(pdev, NULL);
968644570b8SJeff Kirsher 	free_netdev(dev);
969644570b8SJeff Kirsher 
970644570b8SJeff Kirsher 	return ret;
971644570b8SJeff Kirsher }
972644570b8SJeff Kirsher 
973644570b8SJeff Kirsher /* suspend and resume */
974644570b8SJeff Kirsher 
975644570b8SJeff Kirsher #ifdef CONFIG_PM
ax_suspend(struct platform_device * dev,pm_message_t state)976644570b8SJeff Kirsher static int ax_suspend(struct platform_device *dev, pm_message_t state)
977644570b8SJeff Kirsher {
978644570b8SJeff Kirsher 	struct net_device *ndev = platform_get_drvdata(dev);
979644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(ndev);
980644570b8SJeff Kirsher 
981644570b8SJeff Kirsher 	ax->resume_open = ax->running;
982644570b8SJeff Kirsher 
983644570b8SJeff Kirsher 	netif_device_detach(ndev);
984644570b8SJeff Kirsher 	ax_close(ndev);
985644570b8SJeff Kirsher 
986644570b8SJeff Kirsher 	return 0;
987644570b8SJeff Kirsher }
988644570b8SJeff Kirsher 
ax_resume(struct platform_device * pdev)989644570b8SJeff Kirsher static int ax_resume(struct platform_device *pdev)
990644570b8SJeff Kirsher {
991644570b8SJeff Kirsher 	struct net_device *ndev = platform_get_drvdata(pdev);
992644570b8SJeff Kirsher 	struct ax_device *ax = to_ax_dev(ndev);
993644570b8SJeff Kirsher 
994644570b8SJeff Kirsher 	ax_initial_setup(ndev, netdev_priv(ndev));
995644570b8SJeff Kirsher 	ax_NS8390_init(ndev, ax->resume_open);
996644570b8SJeff Kirsher 	netif_device_attach(ndev);
997644570b8SJeff Kirsher 
998644570b8SJeff Kirsher 	if (ax->resume_open)
999644570b8SJeff Kirsher 		ax_open(ndev);
1000644570b8SJeff Kirsher 
1001644570b8SJeff Kirsher 	return 0;
1002644570b8SJeff Kirsher }
1003644570b8SJeff Kirsher 
1004644570b8SJeff Kirsher #else
1005644570b8SJeff Kirsher #define ax_suspend NULL
1006644570b8SJeff Kirsher #define ax_resume NULL
1007644570b8SJeff Kirsher #endif
1008644570b8SJeff Kirsher 
1009644570b8SJeff Kirsher static struct platform_driver axdrv = {
1010644570b8SJeff Kirsher 	.driver	= {
1011644570b8SJeff Kirsher 		.name		= "ax88796",
1012644570b8SJeff Kirsher 	},
1013644570b8SJeff Kirsher 	.probe		= ax_probe,
1014644570b8SJeff Kirsher 	.remove		= ax_remove,
1015644570b8SJeff Kirsher 	.suspend	= ax_suspend,
1016644570b8SJeff Kirsher 	.resume		= ax_resume,
1017644570b8SJeff Kirsher };
1018644570b8SJeff Kirsher 
1019db62f684SAxel Lin module_platform_driver(axdrv);
1020644570b8SJeff Kirsher 
1021644570b8SJeff Kirsher MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1022644570b8SJeff Kirsher MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1023644570b8SJeff Kirsher MODULE_LICENSE("GPL v2");
1024644570b8SJeff Kirsher MODULE_ALIAS("platform:ax88796");
1025