1 /* drivers/net/ethernet/8390/ax88796.c
2  *
3  * Copyright 2005,2007 Simtec Electronics
4  *	Ben Dooks <ben@simtec.co.uk>
5  *
6  * Asix AX88796 10/100 Ethernet controller support
7  *	Based on ne.c, by Donald Becker, et-al.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/isapnp.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/platform_device.h>
21 #include <linux/delay.h>
22 #include <linux/timer.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/mdio-bitbang.h>
27 #include <linux/phy.h>
28 #include <linux/eeprom_93cx6.h>
29 #include <linux/slab.h>
30 
31 #include <net/ax88796.h>
32 
33 
34 /* Rename the lib8390.c functions to show that they are in this driver */
35 #define __ei_open ax_ei_open
36 #define __ei_close ax_ei_close
37 #define __ei_poll ax_ei_poll
38 #define __ei_start_xmit ax_ei_start_xmit
39 #define __ei_tx_timeout ax_ei_tx_timeout
40 #define __ei_get_stats ax_ei_get_stats
41 #define __ei_set_multicast_list ax_ei_set_multicast_list
42 #define __ei_interrupt ax_ei_interrupt
43 #define ____alloc_ei_netdev ax__alloc_ei_netdev
44 #define __NS8390_init ax_NS8390_init
45 
46 /* force unsigned long back to 'void __iomem *' */
47 #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
48 
49 #define ei_inb(_a) readb(ax_convert_addr(_a))
50 #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
51 
52 #define ei_inb_p(_a) ei_inb(_a)
53 #define ei_outb_p(_v, _a) ei_outb(_v, _a)
54 
55 /* define EI_SHIFT() to take into account our register offsets */
56 #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
57 
58 /* Ensure we have our RCR base value */
59 #define AX88796_PLATFORM
60 
61 static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
62 
63 #include "lib8390.c"
64 
65 #define DRV_NAME "ax88796"
66 #define DRV_VERSION "1.00"
67 
68 /* from ne.c */
69 #define NE_CMD		EI_SHIFT(0x00)
70 #define NE_RESET	EI_SHIFT(0x1f)
71 #define NE_DATAPORT	EI_SHIFT(0x10)
72 
73 #define NE1SM_START_PG	0x20	/* First page of TX buffer */
74 #define NE1SM_STOP_PG	0x40	/* Last page +1 of RX ring */
75 #define NESM_START_PG	0x40	/* First page of TX buffer */
76 #define NESM_STOP_PG	0x80	/* Last page +1 of RX ring */
77 
78 #define AX_GPOC_PPDSET	BIT(6)
79 
80 /* device private data */
81 
82 struct ax_device {
83 	struct mii_bus *mii_bus;
84 	struct mdiobb_ctrl bb_ctrl;
85 	void __iomem *addr_memr;
86 	u8 reg_memr;
87 	int link;
88 	int speed;
89 	int duplex;
90 
91 	void __iomem *map2;
92 	const struct ax_plat_data *plat;
93 
94 	unsigned char running;
95 	unsigned char resume_open;
96 	unsigned int irqflags;
97 
98 	u32 reg_offsets[0x20];
99 };
100 
101 static inline struct ax_device *to_ax_dev(struct net_device *dev)
102 {
103 	struct ei_device *ei_local = netdev_priv(dev);
104 	return (struct ax_device *)(ei_local + 1);
105 }
106 
107 /*
108  * ax_initial_check
109  *
110  * do an initial probe for the card to check whether it exists
111  * and is functional
112  */
113 static int ax_initial_check(struct net_device *dev)
114 {
115 	struct ei_device *ei_local = netdev_priv(dev);
116 	void __iomem *ioaddr = ei_local->mem;
117 	int reg0;
118 	int regd;
119 
120 	reg0 = ei_inb(ioaddr);
121 	if (reg0 == 0xFF)
122 		return -ENODEV;
123 
124 	ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
125 	regd = ei_inb(ioaddr + 0x0d);
126 	ei_outb(0xff, ioaddr + 0x0d);
127 	ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
128 	ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
129 	if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
130 		ei_outb(reg0, ioaddr);
131 		ei_outb(regd, ioaddr + 0x0d);	/* Restore the old values. */
132 		return -ENODEV;
133 	}
134 
135 	return 0;
136 }
137 
138 /*
139  * Hard reset the card. This used to pause for the same period that a
140  * 8390 reset command required, but that shouldn't be necessary.
141  */
142 static void ax_reset_8390(struct net_device *dev)
143 {
144 	struct ei_device *ei_local = netdev_priv(dev);
145 	unsigned long reset_start_time = jiffies;
146 	void __iomem *addr = (void __iomem *)dev->base_addr;
147 
148 	netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
149 
150 	ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
151 
152 	ei_local->txing = 0;
153 	ei_local->dmaing = 0;
154 
155 	/* This check _should_not_ be necessary, omit eventually. */
156 	while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
157 		if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
158 			netdev_warn(dev, "%s: did not complete.\n", __func__);
159 			break;
160 		}
161 	}
162 
163 	ei_outb(ENISR_RESET, addr + EN0_ISR);	/* Ack intr. */
164 }
165 
166 
167 static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
168 			    int ring_page)
169 {
170 	struct ei_device *ei_local = netdev_priv(dev);
171 	void __iomem *nic_base = ei_local->mem;
172 
173 	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
174 	if (ei_local->dmaing) {
175 		netdev_err(dev, "DMAing conflict in %s "
176 			"[DMAstat:%d][irqlock:%d].\n",
177 			__func__,
178 			ei_local->dmaing, ei_local->irqlock);
179 		return;
180 	}
181 
182 	ei_local->dmaing |= 0x01;
183 	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
184 	ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
185 	ei_outb(0, nic_base + EN0_RCNTHI);
186 	ei_outb(0, nic_base + EN0_RSARLO);		/* On page boundary */
187 	ei_outb(ring_page, nic_base + EN0_RSARHI);
188 	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
189 
190 	if (ei_local->word16)
191 		ioread16_rep(nic_base + NE_DATAPORT, hdr,
192 			     sizeof(struct e8390_pkt_hdr) >> 1);
193 	else
194 		ioread8_rep(nic_base + NE_DATAPORT, hdr,
195 			    sizeof(struct e8390_pkt_hdr));
196 
197 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
198 	ei_local->dmaing &= ~0x01;
199 
200 	le16_to_cpus(&hdr->count);
201 }
202 
203 
204 /*
205  * Block input and output, similar to the Crynwr packet driver. If
206  * you are porting to a new ethercard, look at the packet driver
207  * source for hints. The NEx000 doesn't share the on-board packet
208  * memory -- you have to put the packet out through the "remote DMA"
209  * dataport using ei_outb.
210  */
211 static void ax_block_input(struct net_device *dev, int count,
212 			   struct sk_buff *skb, int ring_offset)
213 {
214 	struct ei_device *ei_local = netdev_priv(dev);
215 	void __iomem *nic_base = ei_local->mem;
216 	char *buf = skb->data;
217 
218 	if (ei_local->dmaing) {
219 		netdev_err(dev,
220 			"DMAing conflict in %s "
221 			"[DMAstat:%d][irqlock:%d].\n",
222 			__func__,
223 			ei_local->dmaing, ei_local->irqlock);
224 		return;
225 	}
226 
227 	ei_local->dmaing |= 0x01;
228 
229 	ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
230 	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
231 	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
232 	ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
233 	ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
234 	ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
235 
236 	if (ei_local->word16) {
237 		ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
238 		if (count & 0x01)
239 			buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
240 
241 	} else {
242 		ioread8_rep(nic_base + NE_DATAPORT, buf, count);
243 	}
244 
245 	ei_local->dmaing &= ~1;
246 }
247 
248 static void ax_block_output(struct net_device *dev, int count,
249 			    const unsigned char *buf, const int start_page)
250 {
251 	struct ei_device *ei_local = netdev_priv(dev);
252 	void __iomem *nic_base = ei_local->mem;
253 	unsigned long dma_start;
254 
255 	/*
256 	 * Round the count up for word writes. Do we need to do this?
257 	 * What effect will an odd byte count have on the 8390?  I
258 	 * should check someday.
259 	 */
260 	if (ei_local->word16 && (count & 0x01))
261 		count++;
262 
263 	/* This *shouldn't* happen. If it does, it's the last thing you'll see */
264 	if (ei_local->dmaing) {
265 		netdev_err(dev, "DMAing conflict in %s."
266 			"[DMAstat:%d][irqlock:%d]\n",
267 			__func__,
268 		       ei_local->dmaing, ei_local->irqlock);
269 		return;
270 	}
271 
272 	ei_local->dmaing |= 0x01;
273 	/* We should already be in page 0, but to be safe... */
274 	ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
275 
276 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);
277 
278 	/* Now the normal output. */
279 	ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
280 	ei_outb(count >> 8, nic_base + EN0_RCNTHI);
281 	ei_outb(0x00, nic_base + EN0_RSARLO);
282 	ei_outb(start_page, nic_base + EN0_RSARHI);
283 
284 	ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
285 	if (ei_local->word16)
286 		iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
287 	else
288 		iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
289 
290 	dma_start = jiffies;
291 
292 	while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
293 		if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
294 			netdev_warn(dev, "timeout waiting for Tx RDC.\n");
295 			ax_reset_8390(dev);
296 			ax_NS8390_init(dev, 1);
297 			break;
298 		}
299 	}
300 
301 	ei_outb(ENISR_RDC, nic_base + EN0_ISR);	/* Ack intr. */
302 	ei_local->dmaing &= ~0x01;
303 }
304 
305 /* definitions for accessing MII/EEPROM interface */
306 
307 #define AX_MEMR			EI_SHIFT(0x14)
308 #define AX_MEMR_MDC		BIT(0)
309 #define AX_MEMR_MDIR		BIT(1)
310 #define AX_MEMR_MDI		BIT(2)
311 #define AX_MEMR_MDO		BIT(3)
312 #define AX_MEMR_EECS		BIT(4)
313 #define AX_MEMR_EEI		BIT(5)
314 #define AX_MEMR_EEO		BIT(6)
315 #define AX_MEMR_EECLK		BIT(7)
316 
317 static void ax_handle_link_change(struct net_device *dev)
318 {
319 	struct ax_device  *ax = to_ax_dev(dev);
320 	struct phy_device *phy_dev = dev->phydev;
321 	int status_change = 0;
322 
323 	if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
324 			     (ax->duplex != phy_dev->duplex))) {
325 
326 		ax->speed = phy_dev->speed;
327 		ax->duplex = phy_dev->duplex;
328 		status_change = 1;
329 	}
330 
331 	if (phy_dev->link != ax->link) {
332 		if (!phy_dev->link) {
333 			ax->speed = 0;
334 			ax->duplex = -1;
335 		}
336 		ax->link = phy_dev->link;
337 
338 		status_change = 1;
339 	}
340 
341 	if (status_change)
342 		phy_print_status(phy_dev);
343 }
344 
345 static int ax_mii_probe(struct net_device *dev)
346 {
347 	struct ax_device  *ax = to_ax_dev(dev);
348 	struct phy_device *phy_dev = NULL;
349 	int ret;
350 
351 	/* find the first phy */
352 	phy_dev = phy_find_first(ax->mii_bus);
353 	if (!phy_dev) {
354 		netdev_err(dev, "no PHY found\n");
355 		return -ENODEV;
356 	}
357 
358 	ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
359 				 PHY_INTERFACE_MODE_MII);
360 	if (ret) {
361 		netdev_err(dev, "Could not attach to PHY\n");
362 		return ret;
363 	}
364 
365 	/* mask with MAC supported features */
366 	phy_dev->supported &= PHY_BASIC_FEATURES;
367 	phy_dev->advertising = phy_dev->supported;
368 
369 	netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
370 		    phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
371 
372 	return 0;
373 }
374 
375 static void ax_phy_switch(struct net_device *dev, int on)
376 {
377 	struct ei_device *ei_local = netdev_priv(dev);
378 	struct ax_device *ax = to_ax_dev(dev);
379 
380 	u8 reg_gpoc =  ax->plat->gpoc_val;
381 
382 	if (!!on)
383 		reg_gpoc &= ~AX_GPOC_PPDSET;
384 	else
385 		reg_gpoc |= AX_GPOC_PPDSET;
386 
387 	ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
388 }
389 
390 static int ax_open(struct net_device *dev)
391 {
392 	struct ax_device *ax = to_ax_dev(dev);
393 	int ret;
394 
395 	netdev_dbg(dev, "open\n");
396 
397 	ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
398 			  dev->name, dev);
399 	if (ret)
400 		goto failed_request_irq;
401 
402 	/* turn the phy on (if turned off) */
403 	ax_phy_switch(dev, 1);
404 
405 	ret = ax_mii_probe(dev);
406 	if (ret)
407 		goto failed_mii_probe;
408 	phy_start(dev->phydev);
409 
410 	ret = ax_ei_open(dev);
411 	if (ret)
412 		goto failed_ax_ei_open;
413 
414 	ax->running = 1;
415 
416 	return 0;
417 
418  failed_ax_ei_open:
419 	phy_disconnect(dev->phydev);
420  failed_mii_probe:
421 	ax_phy_switch(dev, 0);
422 	free_irq(dev->irq, dev);
423  failed_request_irq:
424 	return ret;
425 }
426 
427 static int ax_close(struct net_device *dev)
428 {
429 	struct ax_device *ax = to_ax_dev(dev);
430 
431 	netdev_dbg(dev, "close\n");
432 
433 	ax->running = 0;
434 	wmb();
435 
436 	ax_ei_close(dev);
437 
438 	/* turn the phy off */
439 	ax_phy_switch(dev, 0);
440 	phy_disconnect(dev->phydev);
441 
442 	free_irq(dev->irq, dev);
443 	return 0;
444 }
445 
446 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
447 {
448 	struct phy_device *phy_dev = dev->phydev;
449 
450 	if (!netif_running(dev))
451 		return -EINVAL;
452 
453 	if (!phy_dev)
454 		return -ENODEV;
455 
456 	return phy_mii_ioctl(phy_dev, req, cmd);
457 }
458 
459 /* ethtool ops */
460 
461 static void ax_get_drvinfo(struct net_device *dev,
462 			   struct ethtool_drvinfo *info)
463 {
464 	struct platform_device *pdev = to_platform_device(dev->dev.parent);
465 
466 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
467 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
468 	strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
469 }
470 
471 static u32 ax_get_msglevel(struct net_device *dev)
472 {
473 	struct ei_device *ei_local = netdev_priv(dev);
474 
475 	return ei_local->msg_enable;
476 }
477 
478 static void ax_set_msglevel(struct net_device *dev, u32 v)
479 {
480 	struct ei_device *ei_local = netdev_priv(dev);
481 
482 	ei_local->msg_enable = v;
483 }
484 
485 static const struct ethtool_ops ax_ethtool_ops = {
486 	.get_drvinfo		= ax_get_drvinfo,
487 	.get_link		= ethtool_op_get_link,
488 	.get_ts_info		= ethtool_op_get_ts_info,
489 	.get_msglevel		= ax_get_msglevel,
490 	.set_msglevel		= ax_set_msglevel,
491 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
492 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
493 };
494 
495 #ifdef CONFIG_AX88796_93CX6
496 static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
497 {
498 	struct ei_device *ei_local = eeprom->data;
499 	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
500 
501 	eeprom->reg_data_in = reg & AX_MEMR_EEI;
502 	eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
503 	eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
504 	eeprom->reg_chip_select = reg & AX_MEMR_EECS;
505 }
506 
507 static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
508 {
509 	struct ei_device *ei_local = eeprom->data;
510 	u8 reg = ei_inb(ei_local->mem + AX_MEMR);
511 
512 	reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
513 
514 	if (eeprom->reg_data_in)
515 		reg |= AX_MEMR_EEI;
516 	if (eeprom->reg_data_clock)
517 		reg |= AX_MEMR_EECLK;
518 	if (eeprom->reg_chip_select)
519 		reg |= AX_MEMR_EECS;
520 
521 	ei_outb(reg, ei_local->mem + AX_MEMR);
522 	udelay(10);
523 }
524 #endif
525 
526 static const struct net_device_ops ax_netdev_ops = {
527 	.ndo_open		= ax_open,
528 	.ndo_stop		= ax_close,
529 	.ndo_do_ioctl		= ax_ioctl,
530 
531 	.ndo_start_xmit		= ax_ei_start_xmit,
532 	.ndo_tx_timeout		= ax_ei_tx_timeout,
533 	.ndo_get_stats		= ax_ei_get_stats,
534 	.ndo_set_rx_mode	= ax_ei_set_multicast_list,
535 	.ndo_validate_addr	= eth_validate_addr,
536 	.ndo_set_mac_address	= eth_mac_addr,
537 #ifdef CONFIG_NET_POLL_CONTROLLER
538 	.ndo_poll_controller	= ax_ei_poll,
539 #endif
540 };
541 
542 static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
543 {
544 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
545 
546 	if (level)
547 		ax->reg_memr |= AX_MEMR_MDC;
548 	else
549 		ax->reg_memr &= ~AX_MEMR_MDC;
550 
551 	ei_outb(ax->reg_memr, ax->addr_memr);
552 }
553 
554 static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
555 {
556 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
557 
558 	if (output)
559 		ax->reg_memr &= ~AX_MEMR_MDIR;
560 	else
561 		ax->reg_memr |= AX_MEMR_MDIR;
562 
563 	ei_outb(ax->reg_memr, ax->addr_memr);
564 }
565 
566 static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
567 {
568 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
569 
570 	if (value)
571 		ax->reg_memr |= AX_MEMR_MDO;
572 	else
573 		ax->reg_memr &= ~AX_MEMR_MDO;
574 
575 	ei_outb(ax->reg_memr, ax->addr_memr);
576 }
577 
578 static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
579 {
580 	struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
581 	int reg_memr = ei_inb(ax->addr_memr);
582 
583 	return reg_memr & AX_MEMR_MDI ? 1 : 0;
584 }
585 
586 static const struct mdiobb_ops bb_ops = {
587 	.owner = THIS_MODULE,
588 	.set_mdc = ax_bb_mdc,
589 	.set_mdio_dir = ax_bb_dir,
590 	.set_mdio_data = ax_bb_set_data,
591 	.get_mdio_data = ax_bb_get_data,
592 };
593 
594 /* setup code */
595 
596 static int ax_mii_init(struct net_device *dev)
597 {
598 	struct platform_device *pdev = to_platform_device(dev->dev.parent);
599 	struct ei_device *ei_local = netdev_priv(dev);
600 	struct ax_device *ax = to_ax_dev(dev);
601 	int err;
602 
603 	ax->bb_ctrl.ops = &bb_ops;
604 	ax->addr_memr = ei_local->mem + AX_MEMR;
605 	ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
606 	if (!ax->mii_bus) {
607 		err = -ENOMEM;
608 		goto out;
609 	}
610 
611 	ax->mii_bus->name = "ax88796_mii_bus";
612 	ax->mii_bus->parent = dev->dev.parent;
613 	snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
614 		pdev->name, pdev->id);
615 
616 	err = mdiobus_register(ax->mii_bus);
617 	if (err)
618 		goto out_free_mdio_bitbang;
619 
620 	return 0;
621 
622  out_free_mdio_bitbang:
623 	free_mdio_bitbang(ax->mii_bus);
624  out:
625 	return err;
626 }
627 
628 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
629 {
630 	void __iomem *ioaddr = ei_local->mem;
631 	struct ax_device *ax = to_ax_dev(dev);
632 
633 	/* Select page 0 */
634 	ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
635 
636 	/* set to byte access */
637 	ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
638 	ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
639 }
640 
641 /*
642  * ax_init_dev
643  *
644  * initialise the specified device, taking care to note the MAC
645  * address it may already have (if configured), ensure
646  * the device is ready to be used by lib8390.c and registerd with
647  * the network layer.
648  */
649 static int ax_init_dev(struct net_device *dev)
650 {
651 	struct ei_device *ei_local = netdev_priv(dev);
652 	struct ax_device *ax = to_ax_dev(dev);
653 	void __iomem *ioaddr = ei_local->mem;
654 	unsigned int start_page;
655 	unsigned int stop_page;
656 	int ret;
657 	int i;
658 
659 	ret = ax_initial_check(dev);
660 	if (ret)
661 		goto err_out;
662 
663 	/* setup goes here */
664 
665 	ax_initial_setup(dev, ei_local);
666 
667 	/* read the mac from the card prom if we need it */
668 
669 	if (ax->plat->flags & AXFLG_HAS_EEPROM) {
670 		unsigned char SA_prom[32];
671 
672 		for (i = 0; i < sizeof(SA_prom); i += 2) {
673 			SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
674 			SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
675 		}
676 
677 		if (ax->plat->wordlength == 2)
678 			for (i = 0; i < 16; i++)
679 				SA_prom[i] = SA_prom[i+i];
680 
681 		memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
682 	}
683 
684 #ifdef CONFIG_AX88796_93CX6
685 	if (ax->plat->flags & AXFLG_HAS_93CX6) {
686 		unsigned char mac_addr[ETH_ALEN];
687 		struct eeprom_93cx6 eeprom;
688 
689 		eeprom.data = ei_local;
690 		eeprom.register_read = ax_eeprom_register_read;
691 		eeprom.register_write = ax_eeprom_register_write;
692 		eeprom.width = PCI_EEPROM_WIDTH_93C56;
693 
694 		eeprom_93cx6_multiread(&eeprom, 0,
695 				       (__le16 __force *)mac_addr,
696 				       sizeof(mac_addr) >> 1);
697 
698 		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
699 	}
700 #endif
701 	if (ax->plat->wordlength == 2) {
702 		/* We must set the 8390 for word mode. */
703 		ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
704 		start_page = NESM_START_PG;
705 		stop_page = NESM_STOP_PG;
706 	} else {
707 		start_page = NE1SM_START_PG;
708 		stop_page = NE1SM_STOP_PG;
709 	}
710 
711 	/* load the mac-address from the device */
712 	if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
713 		ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
714 			ei_local->mem + E8390_CMD); /* 0x61 */
715 		for (i = 0; i < ETH_ALEN; i++)
716 			dev->dev_addr[i] =
717 				ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
718 	}
719 
720 	if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
721 	    ax->plat->mac_addr)
722 		memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
723 
724 	if (!is_valid_ether_addr(dev->dev_addr)) {
725 		eth_hw_addr_random(dev);
726 		dev_info(&dev->dev, "Using random MAC address: %pM\n",
727 			 dev->dev_addr);
728 	}
729 
730 	ax_reset_8390(dev);
731 
732 	ei_local->name = "AX88796";
733 	ei_local->tx_start_page = start_page;
734 	ei_local->stop_page = stop_page;
735 	ei_local->word16 = (ax->plat->wordlength == 2);
736 	ei_local->rx_start_page = start_page + TX_PAGES;
737 
738 #ifdef PACKETBUF_MEMSIZE
739 	/* Allow the packet buffer size to be overridden by know-it-alls. */
740 	ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
741 #endif
742 
743 	ei_local->reset_8390 = &ax_reset_8390;
744 	ei_local->block_input = &ax_block_input;
745 	ei_local->block_output = &ax_block_output;
746 	ei_local->get_8390_hdr = &ax_get_8390_hdr;
747 	ei_local->priv = 0;
748 
749 	dev->netdev_ops = &ax_netdev_ops;
750 	dev->ethtool_ops = &ax_ethtool_ops;
751 
752 	ret = ax_mii_init(dev);
753 	if (ret)
754 		goto err_out;
755 
756 	ax_NS8390_init(dev, 0);
757 
758 	ret = register_netdev(dev);
759 	if (ret)
760 		goto err_out;
761 
762 	netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
763 		    ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
764 		    dev->dev_addr);
765 
766 	return 0;
767 
768  err_out:
769 	return ret;
770 }
771 
772 static int ax_remove(struct platform_device *pdev)
773 {
774 	struct net_device *dev = platform_get_drvdata(pdev);
775 	struct ei_device *ei_local = netdev_priv(dev);
776 	struct ax_device *ax = to_ax_dev(dev);
777 	struct resource *mem;
778 
779 	unregister_netdev(dev);
780 	free_irq(dev->irq, dev);
781 
782 	iounmap(ei_local->mem);
783 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
784 	release_mem_region(mem->start, resource_size(mem));
785 
786 	if (ax->map2) {
787 		iounmap(ax->map2);
788 		mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
789 		release_mem_region(mem->start, resource_size(mem));
790 	}
791 
792 	free_netdev(dev);
793 
794 	return 0;
795 }
796 
797 /*
798  * ax_probe
799  *
800  * This is the entry point when the platform device system uses to
801  * notify us of a new device to attach to. Allocate memory, find the
802  * resources and information passed, and map the necessary registers.
803  */
804 static int ax_probe(struct platform_device *pdev)
805 {
806 	struct net_device *dev;
807 	struct ei_device *ei_local;
808 	struct ax_device *ax;
809 	struct resource *irq, *mem, *mem2;
810 	unsigned long mem_size, mem2_size = 0;
811 	int ret = 0;
812 
813 	dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
814 	if (dev == NULL)
815 		return -ENOMEM;
816 
817 	/* ok, let's setup our device */
818 	SET_NETDEV_DEV(dev, &pdev->dev);
819 	ei_local = netdev_priv(dev);
820 	ax = to_ax_dev(dev);
821 
822 	ax->plat = dev_get_platdata(&pdev->dev);
823 	platform_set_drvdata(pdev, dev);
824 
825 	ei_local->rxcr_base = ax->plat->rcr_val;
826 
827 	/* find the platform resources */
828 	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
829 	if (!irq) {
830 		dev_err(&pdev->dev, "no IRQ specified\n");
831 		ret = -ENXIO;
832 		goto exit_mem;
833 	}
834 
835 	dev->irq = irq->start;
836 	ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
837 
838 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
839 	if (!mem) {
840 		dev_err(&pdev->dev, "no MEM specified\n");
841 		ret = -ENXIO;
842 		goto exit_mem;
843 	}
844 
845 	mem_size = resource_size(mem);
846 
847 	/*
848 	 * setup the register offsets from either the platform data or
849 	 * by using the size of the resource provided
850 	 */
851 	if (ax->plat->reg_offsets)
852 		ei_local->reg_offset = ax->plat->reg_offsets;
853 	else {
854 		ei_local->reg_offset = ax->reg_offsets;
855 		for (ret = 0; ret < 0x18; ret++)
856 			ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
857 	}
858 
859 	if (!request_mem_region(mem->start, mem_size, pdev->name)) {
860 		dev_err(&pdev->dev, "cannot reserve registers\n");
861 		ret = -ENXIO;
862 		goto exit_mem;
863 	}
864 
865 	ei_local->mem = ioremap(mem->start, mem_size);
866 	dev->base_addr = (unsigned long)ei_local->mem;
867 
868 	if (ei_local->mem == NULL) {
869 		dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
870 
871 		ret = -ENXIO;
872 		goto exit_req;
873 	}
874 
875 	/* look for reset area */
876 	mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
877 	if (!mem2) {
878 		if (!ax->plat->reg_offsets) {
879 			for (ret = 0; ret < 0x20; ret++)
880 				ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
881 		}
882 	} else {
883 		mem2_size = resource_size(mem2);
884 
885 		if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
886 			dev_err(&pdev->dev, "cannot reserve registers\n");
887 			ret = -ENXIO;
888 			goto exit_mem1;
889 		}
890 
891 		ax->map2 = ioremap(mem2->start, mem2_size);
892 		if (!ax->map2) {
893 			dev_err(&pdev->dev, "cannot map reset register\n");
894 			ret = -ENXIO;
895 			goto exit_mem2;
896 		}
897 
898 		ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
899 	}
900 
901 	/* got resources, now initialise and register device */
902 	ret = ax_init_dev(dev);
903 	if (!ret)
904 		return 0;
905 
906 	if (!ax->map2)
907 		goto exit_mem1;
908 
909 	iounmap(ax->map2);
910 
911  exit_mem2:
912 	if (mem2)
913 		release_mem_region(mem2->start, mem2_size);
914 
915  exit_mem1:
916 	iounmap(ei_local->mem);
917 
918  exit_req:
919 	release_mem_region(mem->start, mem_size);
920 
921  exit_mem:
922 	free_netdev(dev);
923 
924 	return ret;
925 }
926 
927 /* suspend and resume */
928 
929 #ifdef CONFIG_PM
930 static int ax_suspend(struct platform_device *dev, pm_message_t state)
931 {
932 	struct net_device *ndev = platform_get_drvdata(dev);
933 	struct ax_device *ax = to_ax_dev(ndev);
934 
935 	ax->resume_open = ax->running;
936 
937 	netif_device_detach(ndev);
938 	ax_close(ndev);
939 
940 	return 0;
941 }
942 
943 static int ax_resume(struct platform_device *pdev)
944 {
945 	struct net_device *ndev = platform_get_drvdata(pdev);
946 	struct ax_device *ax = to_ax_dev(ndev);
947 
948 	ax_initial_setup(ndev, netdev_priv(ndev));
949 	ax_NS8390_init(ndev, ax->resume_open);
950 	netif_device_attach(ndev);
951 
952 	if (ax->resume_open)
953 		ax_open(ndev);
954 
955 	return 0;
956 }
957 
958 #else
959 #define ax_suspend NULL
960 #define ax_resume NULL
961 #endif
962 
963 static struct platform_driver axdrv = {
964 	.driver	= {
965 		.name		= "ax88796",
966 	},
967 	.probe		= ax_probe,
968 	.remove		= ax_remove,
969 	.suspend	= ax_suspend,
970 	.resume		= ax_resume,
971 };
972 
973 module_platform_driver(axdrv);
974 
975 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
976 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
977 MODULE_LICENSE("GPL v2");
978 MODULE_ALIAS("platform:ax88796");
979