signalfd: fix interaction with posix-timers
[linux-2.6/libata-dev.git] / drivers / net / ax88796.c
blob90e0734e60375abe0325cc5cfd0c7a49e0b9c43f
1 /* drivers/net/ax88796.c
3 * Copyright 2005,2007 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * Asix AX88796 10/100 Ethernet controller support
7 * Based on ne.c, by Donald Becker, et-al.
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.
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/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/mii.h>
28 #include <net/ax88796.h>
30 #include <asm/system.h>
31 #include <asm/io.h>
33 static int phy_debug = 0;
35 /* Rename the lib8390.c functions to show that they are in this driver */
36 #define __ei_open ax_ei_open
37 #define __ei_close ax_ei_close
38 #define __ei_poll ax_ei_poll
39 #define __ei_tx_timeout ax_ei_tx_timeout
40 #define __ei_interrupt ax_ei_interrupt
41 #define ____alloc_ei_netdev ax__alloc_ei_netdev
42 #define __NS8390_init ax_NS8390_init
44 /* force unsigned long back to 'void __iomem *' */
45 #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
47 #define ei_inb(_a) readb(ax_convert_addr(_a))
48 #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
50 #define ei_inb_p(_a) ei_inb(_a)
51 #define ei_outb_p(_v, _a) ei_outb(_v, _a)
53 /* define EI_SHIFT() to take into account our register offsets */
54 #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
56 /* Ensure we have our RCR base value */
57 #define AX88796_PLATFORM
59 static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
61 #include "lib8390.c"
63 #define DRV_NAME "ax88796"
64 #define DRV_VERSION "1.00"
66 /* from ne.c */
67 #define NE_CMD EI_SHIFT(0x00)
68 #define NE_RESET EI_SHIFT(0x1f)
69 #define NE_DATAPORT EI_SHIFT(0x10)
71 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
72 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
73 #define NESM_START_PG 0x40 /* First page of TX buffer */
74 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
76 /* device private data */
78 struct ax_device {
79 struct timer_list mii_timer;
80 spinlock_t mii_lock;
81 struct mii_if_info mii;
83 u32 msg_enable;
84 void __iomem *map2;
85 struct platform_device *dev;
86 struct resource *mem;
87 struct resource *mem2;
88 struct ax_plat_data *plat;
90 unsigned char running;
91 unsigned char resume_open;
93 u32 reg_offsets[0x20];
96 static inline struct ax_device *to_ax_dev(struct net_device *dev)
98 struct ei_device *ei_local = netdev_priv(dev);
99 return (struct ax_device *)(ei_local+1);
102 /* ax_initial_check
104 * do an initial probe for the card to check wether it exists
105 * and is functional
108 static int ax_initial_check(struct net_device *dev)
110 struct ei_device *ei_local = netdev_priv(dev);
111 void __iomem *ioaddr = ei_local->mem;
112 int reg0;
113 int regd;
115 reg0 = ei_inb(ioaddr);
116 if (reg0 == 0xFF)
117 return -ENODEV;
119 ei_outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
120 regd = ei_inb(ioaddr + 0x0d);
121 ei_outb(0xff, ioaddr + 0x0d);
122 ei_outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
123 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
124 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
125 ei_outb(reg0, ioaddr);
126 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
127 return -ENODEV;
130 return 0;
133 /* Hard reset the card. This used to pause for the same period that a
134 8390 reset command required, but that shouldn't be necessary. */
136 static void ax_reset_8390(struct net_device *dev)
138 struct ei_device *ei_local = netdev_priv(dev);
139 unsigned long reset_start_time = jiffies;
140 void __iomem *addr = (void __iomem *)dev->base_addr;
142 if (ei_debug > 1)
143 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
145 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
147 ei_status.txing = 0;
148 ei_status.dmaing = 0;
150 /* This check _should_not_ be necessary, omit eventually. */
151 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
152 if (jiffies - reset_start_time > 2*HZ/100) {
153 printk(KERN_WARNING "%s: %s did not complete.\n",
154 __FUNCTION__, dev->name);
155 break;
159 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
163 static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
164 int ring_page)
166 struct ei_device *ei_local = netdev_priv(dev);
167 void __iomem *nic_base = ei_local->mem;
169 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
170 if (ei_status.dmaing) {
171 printk(KERN_EMERG "%s: DMAing conflict in %s [DMAstat:%d][irqlock:%d].\n",
172 dev->name, __FUNCTION__,
173 ei_status.dmaing, ei_status.irqlock);
174 return;
177 ei_status.dmaing |= 0x01;
178 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
179 ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
180 ei_outb(0, nic_base + EN0_RCNTHI);
181 ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
182 ei_outb(ring_page, nic_base + EN0_RSARHI);
183 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
185 if (ei_status.word16)
186 readsw(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
187 else
188 readsb(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
190 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
191 ei_status.dmaing &= ~0x01;
193 le16_to_cpus(&hdr->count);
197 /* Block input and output, similar to the Crynwr packet driver. If you
198 are porting to a new ethercard, look at the packet driver source for hints.
199 The NEx000 doesn't share the on-board packet memory -- you have to put
200 the packet out through the "remote DMA" dataport using ei_outb. */
202 static void ax_block_input(struct net_device *dev, int count,
203 struct sk_buff *skb, int ring_offset)
205 struct ei_device *ei_local = netdev_priv(dev);
206 void __iomem *nic_base = ei_local->mem;
207 char *buf = skb->data;
209 if (ei_status.dmaing) {
210 printk(KERN_EMERG "%s: DMAing conflict in ax_block_input "
211 "[DMAstat:%d][irqlock:%d].\n",
212 dev->name, ei_status.dmaing, ei_status.irqlock);
213 return;
216 ei_status.dmaing |= 0x01;
218 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
219 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
220 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
221 ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
222 ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
223 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
225 if (ei_status.word16) {
226 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
227 if (count & 0x01)
228 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
230 } else {
231 readsb(nic_base + NE_DATAPORT, buf, count);
234 ei_status.dmaing &= ~1;
237 static void ax_block_output(struct net_device *dev, int count,
238 const unsigned char *buf, const int start_page)
240 struct ei_device *ei_local = netdev_priv(dev);
241 void __iomem *nic_base = ei_local->mem;
242 unsigned long dma_start;
244 /* Round the count up for word writes. Do we need to do this?
245 What effect will an odd byte count have on the 8390?
246 I should check someday. */
248 if (ei_status.word16 && (count & 0x01))
249 count++;
251 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
252 if (ei_status.dmaing) {
253 printk(KERN_EMERG "%s: DMAing conflict in %s."
254 "[DMAstat:%d][irqlock:%d]\n",
255 dev->name, __FUNCTION__,
256 ei_status.dmaing, ei_status.irqlock);
257 return;
260 ei_status.dmaing |= 0x01;
261 /* We should already be in page 0, but to be safe... */
262 ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
264 ei_outb(ENISR_RDC, nic_base + EN0_ISR);
266 /* Now the normal output. */
267 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
268 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
269 ei_outb(0x00, nic_base + EN0_RSARLO);
270 ei_outb(start_page, nic_base + EN0_RSARHI);
272 ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
273 if (ei_status.word16) {
274 writesw(nic_base + NE_DATAPORT, buf, count>>1);
275 } else {
276 writesb(nic_base + NE_DATAPORT, buf, count);
279 dma_start = jiffies;
281 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
282 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
283 printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
284 ax_reset_8390(dev);
285 ax_NS8390_init(dev,1);
286 break;
290 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
291 ei_status.dmaing &= ~0x01;
292 return;
295 /* definitions for accessing MII/EEPROM interface */
297 #define AX_MEMR EI_SHIFT(0x14)
298 #define AX_MEMR_MDC (1<<0)
299 #define AX_MEMR_MDIR (1<<1)
300 #define AX_MEMR_MDI (1<<2)
301 #define AX_MEMR_MDO (1<<3)
302 #define AX_MEMR_EECS (1<<4)
303 #define AX_MEMR_EEI (1<<5)
304 #define AX_MEMR_EEO (1<<6)
305 #define AX_MEMR_EECLK (1<<7)
307 /* ax_mii_ei_outbits
309 * write the specified set of bits to the phy
312 static void
313 ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
315 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
316 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
317 unsigned int memr;
319 /* clock low, data to output mode */
320 memr = ei_inb(memr_addr);
321 memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
322 ei_outb(memr, memr_addr);
324 for (len--; len >= 0; len--) {
325 if (bits & (1 << len))
326 memr |= AX_MEMR_MDO;
327 else
328 memr &= ~AX_MEMR_MDO;
330 ei_outb(memr, memr_addr);
332 /* clock high */
334 ei_outb(memr | AX_MEMR_MDC, memr_addr);
335 udelay(1);
337 /* clock low */
338 ei_outb(memr, memr_addr);
341 /* leaves the clock line low, mdir input */
342 memr |= AX_MEMR_MDIR;
343 ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
346 /* ax_phy_ei_inbits
348 * read a specified number of bits from the phy
351 static unsigned int
352 ax_phy_ei_inbits(struct net_device *dev, int no)
354 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
355 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
356 unsigned int memr;
357 unsigned int result = 0;
359 /* clock low, data to input mode */
360 memr = ei_inb(memr_addr);
361 memr &= ~AX_MEMR_MDC;
362 memr |= AX_MEMR_MDIR;
363 ei_outb(memr, memr_addr);
365 for (no--; no >= 0; no--) {
366 ei_outb(memr | AX_MEMR_MDC, memr_addr);
368 udelay(1);
370 if (ei_inb(memr_addr) & AX_MEMR_MDI)
371 result |= (1<<no);
373 ei_outb(memr, memr_addr);
376 return result;
379 /* ax_phy_issueaddr
381 * use the low level bit shifting routines to send the address
382 * and command to the specified phy
385 static void
386 ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
388 if (phy_debug)
389 pr_debug("%s: dev %p, %04x, %04x, %d\n",
390 __FUNCTION__, dev, phy_addr, reg, opc);
392 ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */
393 ax_mii_ei_outbits(dev, 1, 2); /* frame-start */
394 ax_mii_ei_outbits(dev, opc, 2); /* op code */
395 ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */
396 ax_mii_ei_outbits(dev, reg, 5); /* reg address */
399 static int
400 ax_phy_read(struct net_device *dev, int phy_addr, int reg)
402 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
403 unsigned long flags;
404 unsigned int result;
406 spin_lock_irqsave(&ei_local->page_lock, flags);
408 ax_phy_issueaddr(dev, phy_addr, reg, 2);
410 result = ax_phy_ei_inbits(dev, 17);
411 result &= ~(3<<16);
413 spin_unlock_irqrestore(&ei_local->page_lock, flags);
415 if (phy_debug)
416 pr_debug("%s: %04x.%04x => read %04x\n", __FUNCTION__,
417 phy_addr, reg, result);
419 return result;
422 static void
423 ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
425 struct ei_device *ei = (struct ei_device *) netdev_priv(dev);
426 unsigned long flags;
428 printk(KERN_DEBUG "%s: %p, %04x, %04x %04x\n",
429 __FUNCTION__, dev, phy_addr, reg, value);
431 spin_lock_irqsave(&ei->page_lock, flags);
433 ax_phy_issueaddr(dev, phy_addr, reg, 1);
434 ax_mii_ei_outbits(dev, 2, 2); /* send TA */
435 ax_mii_ei_outbits(dev, value, 16);
437 spin_unlock_irqrestore(&ei->page_lock, flags);
440 static void ax_mii_expiry(unsigned long data)
442 struct net_device *dev = (struct net_device *)data;
443 struct ax_device *ax = to_ax_dev(dev);
444 unsigned long flags;
446 spin_lock_irqsave(&ax->mii_lock, flags);
447 mii_check_media(&ax->mii, netif_msg_link(ax), 0);
448 spin_unlock_irqrestore(&ax->mii_lock, flags);
450 if (ax->running) {
451 ax->mii_timer.expires = jiffies + HZ*2;
452 add_timer(&ax->mii_timer);
456 static int ax_open(struct net_device *dev)
458 struct ax_device *ax = to_ax_dev(dev);
459 struct ei_device *ei_local = netdev_priv(dev);
460 int ret;
462 dev_dbg(&ax->dev->dev, "%s: open\n", dev->name);
464 ret = request_irq(dev->irq, ax_ei_interrupt, 0, dev->name, dev);
465 if (ret)
466 return ret;
468 ret = ax_ei_open(dev);
469 if (ret)
470 return ret;
472 /* turn the phy on (if turned off) */
474 ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
475 ax->running = 1;
477 /* start the MII timer */
479 init_timer(&ax->mii_timer);
481 ax->mii_timer.expires = jiffies+1;
482 ax->mii_timer.data = (unsigned long) dev;
483 ax->mii_timer.function = ax_mii_expiry;
485 add_timer(&ax->mii_timer);
487 return 0;
490 static int ax_close(struct net_device *dev)
492 struct ax_device *ax = to_ax_dev(dev);
493 struct ei_device *ei_local = netdev_priv(dev);
495 dev_dbg(&ax->dev->dev, "%s: close\n", dev->name);
497 /* turn the phy off */
499 ei_outb(ax->plat->gpoc_val | (1<<6),
500 ei_local->mem + EI_SHIFT(0x17));
502 ax->running = 0;
503 wmb();
505 del_timer_sync(&ax->mii_timer);
506 ax_ei_close(dev);
508 free_irq(dev->irq, dev);
509 return 0;
512 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
514 struct ax_device *ax = to_ax_dev(dev);
515 unsigned long flags;
516 int rc;
518 if (!netif_running(dev))
519 return -EINVAL;
521 spin_lock_irqsave(&ax->mii_lock, flags);
522 rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
523 spin_unlock_irqrestore(&ax->mii_lock, flags);
525 return rc;
528 /* ethtool ops */
530 static void ax_get_drvinfo(struct net_device *dev,
531 struct ethtool_drvinfo *info)
533 struct ax_device *ax = to_ax_dev(dev);
535 strcpy(info->driver, DRV_NAME);
536 strcpy(info->version, DRV_VERSION);
537 strcpy(info->bus_info, ax->dev->name);
540 static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
542 struct ax_device *ax = to_ax_dev(dev);
543 unsigned long flags;
545 spin_lock_irqsave(&ax->mii_lock, flags);
546 mii_ethtool_gset(&ax->mii, cmd);
547 spin_lock_irqsave(&ax->mii_lock, flags);
549 return 0;
552 static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
554 struct ax_device *ax = to_ax_dev(dev);
555 unsigned long flags;
556 int rc;
558 spin_lock_irqsave(&ax->mii_lock, flags);
559 rc = mii_ethtool_sset(&ax->mii, cmd);
560 spin_lock_irqsave(&ax->mii_lock, flags);
562 return rc;
565 static int ax_nway_reset(struct net_device *dev)
567 struct ax_device *ax = to_ax_dev(dev);
568 return mii_nway_restart(&ax->mii);
571 static u32 ax_get_link(struct net_device *dev)
573 struct ax_device *ax = to_ax_dev(dev);
574 return mii_link_ok(&ax->mii);
577 static const struct ethtool_ops ax_ethtool_ops = {
578 .get_drvinfo = ax_get_drvinfo,
579 .get_settings = ax_get_settings,
580 .set_settings = ax_set_settings,
581 .nway_reset = ax_nway_reset,
582 .get_link = ax_get_link,
585 /* setup code */
587 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
589 void __iomem *ioaddr = ei_local->mem;
590 struct ax_device *ax = to_ax_dev(dev);
592 /* Select page 0*/
593 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD);
595 /* set to byte access */
596 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
597 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
600 /* ax_init_dev
602 * initialise the specified device, taking care to note the MAC
603 * address it may already have (if configured), ensure
604 * the device is ready to be used by lib8390.c and registerd with
605 * the network layer.
608 static int ax_init_dev(struct net_device *dev, int first_init)
610 struct ei_device *ei_local = netdev_priv(dev);
611 struct ax_device *ax = to_ax_dev(dev);
612 void __iomem *ioaddr = ei_local->mem;
613 unsigned int start_page;
614 unsigned int stop_page;
615 int ret;
616 int i;
618 ret = ax_initial_check(dev);
619 if (ret)
620 goto err_out;
622 /* setup goes here */
624 ax_initial_setup(dev, ei_local);
626 /* read the mac from the card prom if we need it */
628 if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) {
629 unsigned char SA_prom[32];
631 for(i = 0; i < sizeof(SA_prom); i+=2) {
632 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
633 SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT);
636 if (ax->plat->wordlength == 2)
637 for (i = 0; i < 16; i++)
638 SA_prom[i] = SA_prom[i+i];
640 memcpy(dev->dev_addr, SA_prom, 6);
643 if (ax->plat->wordlength == 2) {
644 /* We must set the 8390 for word mode. */
645 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
646 start_page = NESM_START_PG;
647 stop_page = NESM_STOP_PG;
648 } else {
649 start_page = NE1SM_START_PG;
650 stop_page = NE1SM_STOP_PG;
653 /* load the mac-address from the device if this is the
654 * first time we've initialised */
656 if (first_init && ax->plat->flags & AXFLG_MAC_FROMDEV) {
657 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
658 ei_local->mem + E8390_CMD); /* 0x61 */
660 for (i = 0 ; i < ETHER_ADDR_LEN ; i++)
661 dev->dev_addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
664 ax_reset_8390(dev);
666 ei_status.name = "AX88796";
667 ei_status.tx_start_page = start_page;
668 ei_status.stop_page = stop_page;
669 ei_status.word16 = (ax->plat->wordlength == 2);
670 ei_status.rx_start_page = start_page + TX_PAGES;
672 #ifdef PACKETBUF_MEMSIZE
673 /* Allow the packet buffer size to be overridden by know-it-alls. */
674 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
675 #endif
677 ei_status.reset_8390 = &ax_reset_8390;
678 ei_status.block_input = &ax_block_input;
679 ei_status.block_output = &ax_block_output;
680 ei_status.get_8390_hdr = &ax_get_8390_hdr;
681 ei_status.priv = 0;
683 dev->open = ax_open;
684 dev->stop = ax_close;
685 dev->do_ioctl = ax_ioctl;
686 dev->ethtool_ops = &ax_ethtool_ops;
688 ax->msg_enable = NETIF_MSG_LINK;
689 ax->mii.phy_id_mask = 0x1f;
690 ax->mii.reg_num_mask = 0x1f;
691 ax->mii.phy_id = 0x10; /* onboard phy */
692 ax->mii.force_media = 0;
693 ax->mii.full_duplex = 0;
694 ax->mii.mdio_read = ax_phy_read;
695 ax->mii.mdio_write = ax_phy_write;
696 ax->mii.dev = dev;
698 #ifdef CONFIG_NET_POLL_CONTROLLER
699 dev->poll_controller = ax_ei_poll;
700 #endif
701 ax_NS8390_init(dev, 0);
703 if (first_init) {
704 printk("AX88796: %dbit, irq %d, %lx, MAC: ",
705 ei_status.word16 ? 16:8, dev->irq, dev->base_addr);
707 for (i = 0; i < ETHER_ADDR_LEN; i++)
708 printk("%2.2x%c", dev->dev_addr[i],
709 (i < (ETHER_ADDR_LEN-1) ? ':' : ' '));
711 printk("\n");
714 ret = register_netdev(dev);
715 if (ret)
716 goto out_irq;
718 return 0;
720 out_irq:
721 /* cleanup irq */
722 free_irq(dev->irq, dev);
723 err_out:
724 return ret;
727 static int ax_remove(struct platform_device *_dev)
729 struct net_device *dev = platform_get_drvdata(_dev);
730 struct ax_device *ax;
732 ax = to_ax_dev(dev);
734 unregister_netdev(dev);
735 free_irq(dev->irq, dev);
737 iounmap(ei_status.mem);
738 release_resource(ax->mem);
739 kfree(ax->mem);
741 if (ax->map2) {
742 iounmap(ax->map2);
743 release_resource(ax->mem2);
744 kfree(ax->mem2);
747 free_netdev(dev);
749 return 0;
752 /* ax_probe
754 * This is the entry point when the platform device system uses to
755 * notify us of a new device to attach to. Allocate memory, find
756 * the resources and information passed, and map the necessary registers.
759 static int ax_probe(struct platform_device *pdev)
761 struct net_device *dev;
762 struct ax_device *ax;
763 struct resource *res;
764 size_t size;
765 int ret;
767 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
768 if (dev == NULL)
769 return -ENOMEM;
771 /* ok, let's setup our device */
772 ax = to_ax_dev(dev);
774 memset(ax, 0, sizeof(struct ax_device));
776 spin_lock_init(&ax->mii_lock);
778 ax->dev = pdev;
779 ax->plat = pdev->dev.platform_data;
780 platform_set_drvdata(pdev, dev);
782 ei_status.rxcr_base = ax->plat->rcr_val;
784 /* find the platform resources */
786 dev->irq = platform_get_irq(pdev, 0);
787 if (dev->irq < 0) {
788 dev_err(&pdev->dev, "no IRQ specified\n");
789 ret = -ENXIO;
790 goto exit_mem;
793 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
794 if (res == NULL) {
795 dev_err(&pdev->dev, "no MEM specified\n");
796 ret = -ENXIO;
797 goto exit_mem;
800 size = (res->end - res->start) + 1;
802 /* setup the register offsets from either the platform data
803 * or by using the size of the resource provided */
805 if (ax->plat->reg_offsets)
806 ei_status.reg_offset = ax->plat->reg_offsets;
807 else {
808 ei_status.reg_offset = ax->reg_offsets;
809 for (ret = 0; ret < 0x18; ret++)
810 ax->reg_offsets[ret] = (size / 0x18) * ret;
813 ax->mem = request_mem_region(res->start, size, pdev->name);
814 if (ax->mem == NULL) {
815 dev_err(&pdev->dev, "cannot reserve registers\n");
816 ret = -ENXIO;
817 goto exit_mem;
820 ei_status.mem = ioremap(res->start, size);
821 dev->base_addr = (unsigned long)ei_status.mem;
823 if (ei_status.mem == NULL) {
824 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
825 (unsigned long long)res->start,
826 (unsigned long long)res->end);
828 ret = -ENXIO;
829 goto exit_req;
832 /* look for reset area */
834 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
835 if (res == NULL) {
836 if (!ax->plat->reg_offsets) {
837 for (ret = 0; ret < 0x20; ret++)
838 ax->reg_offsets[ret] = (size / 0x20) * ret;
841 ax->map2 = NULL;
842 } else {
843 size = (res->end - res->start) + 1;
845 ax->mem2 = request_mem_region(res->start, size, pdev->name);
846 if (ax->mem == NULL) {
847 dev_err(&pdev->dev, "cannot reserve registers\n");
848 ret = -ENXIO;
849 goto exit_mem1;
852 ax->map2 = ioremap(res->start, size);
853 if (ax->map2 == NULL) {
854 dev_err(&pdev->dev, "cannot map reset register");
855 ret = -ENXIO;
856 goto exit_mem2;
859 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
862 /* got resources, now initialise and register device */
864 ret = ax_init_dev(dev, 1);
865 if (!ret)
866 return 0;
868 if (ax->map2 == NULL)
869 goto exit_mem1;
871 iounmap(ax->map2);
873 exit_mem2:
874 release_resource(ax->mem2);
875 kfree(ax->mem2);
877 exit_mem1:
878 iounmap(ei_status.mem);
880 exit_req:
881 release_resource(ax->mem);
882 kfree(ax->mem);
884 exit_mem:
885 free_netdev(dev);
887 return ret;
890 /* suspend and resume */
892 #ifdef CONFIG_PM
893 static int ax_suspend(struct platform_device *dev, pm_message_t state)
895 struct net_device *ndev = platform_get_drvdata(dev);
896 struct ax_device *ax = to_ax_dev(ndev);
898 ax->resume_open = ax->running;
900 netif_device_detach(ndev);
901 ax_close(ndev);
903 return 0;
906 static int ax_resume(struct platform_device *pdev)
908 struct net_device *ndev = platform_get_drvdata(pdev);
909 struct ax_device *ax = to_ax_dev(ndev);
911 ax_initial_setup(ndev, netdev_priv(ndev));
912 ax_NS8390_init(ndev, ax->resume_open);
913 netif_device_attach(ndev);
915 if (ax->resume_open)
916 ax_open(ndev);
918 return 0;
921 #else
922 #define ax_suspend NULL
923 #define ax_resume NULL
924 #endif
926 static struct platform_driver axdrv = {
927 .driver = {
928 .name = "ax88796",
929 .owner = THIS_MODULE,
931 .probe = ax_probe,
932 .remove = ax_remove,
933 .suspend = ax_suspend,
934 .resume = ax_resume,
937 static int __init axdrv_init(void)
939 return platform_driver_register(&axdrv);
942 static void __exit axdrv_exit(void)
944 platform_driver_unregister(&axdrv);
947 module_init(axdrv_init);
948 module_exit(axdrv_exit);
950 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
951 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
952 MODULE_LICENSE("GPL v2");