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