RTCs: handle NVRAM better
[linux-2.6/openmoko-kernel.git] / drivers / net / ax88796.c
blob7495a9ee8f4bff6dde5b7c7666398d35882059fd
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>
27 #include <linux/eeprom_93cx6.h>
29 #include <net/ax88796.h>
31 #include <asm/system.h>
32 #include <asm/io.h>
34 static int phy_debug = 0;
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_tx_timeout ax_ei_tx_timeout
41 #define __ei_interrupt ax_ei_interrupt
42 #define ____alloc_ei_netdev ax__alloc_ei_netdev
43 #define __NS8390_init ax_NS8390_init
45 /* force unsigned long back to 'void __iomem *' */
46 #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
48 #define ei_inb(_a) readb(ax_convert_addr(_a))
49 #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
51 #define ei_inb_p(_a) ei_inb(_a)
52 #define ei_outb_p(_v, _a) ei_outb(_v, _a)
54 /* define EI_SHIFT() to take into account our register offsets */
55 #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
57 /* Ensure we have our RCR base value */
58 #define AX88796_PLATFORM
60 static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
62 #include "lib8390.c"
64 #define DRV_NAME "ax88796"
65 #define DRV_VERSION "1.00"
67 /* from ne.c */
68 #define NE_CMD EI_SHIFT(0x00)
69 #define NE_RESET EI_SHIFT(0x1f)
70 #define NE_DATAPORT EI_SHIFT(0x10)
72 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
73 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
74 #define NESM_START_PG 0x40 /* First page of TX buffer */
75 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
77 /* device private data */
79 struct ax_device {
80 struct timer_list mii_timer;
81 spinlock_t mii_lock;
82 struct mii_if_info mii;
84 u32 msg_enable;
85 void __iomem *map2;
86 struct platform_device *dev;
87 struct resource *mem;
88 struct resource *mem2;
89 struct ax_plat_data *plat;
91 unsigned char running;
92 unsigned char resume_open;
94 u32 reg_offsets[0x20];
97 static inline struct ax_device *to_ax_dev(struct net_device *dev)
99 struct ei_device *ei_local = netdev_priv(dev);
100 return (struct ax_device *)(ei_local+1);
103 /* ax_initial_check
105 * do an initial probe for the card to check wether it exists
106 * and is functional
109 static int ax_initial_check(struct net_device *dev)
111 struct ei_device *ei_local = netdev_priv(dev);
112 void __iomem *ioaddr = ei_local->mem;
113 int reg0;
114 int regd;
116 reg0 = ei_inb(ioaddr);
117 if (reg0 == 0xFF)
118 return -ENODEV;
120 ei_outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
121 regd = ei_inb(ioaddr + 0x0d);
122 ei_outb(0xff, ioaddr + 0x0d);
123 ei_outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
124 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
125 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
126 ei_outb(reg0, ioaddr);
127 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
128 return -ENODEV;
131 return 0;
134 /* Hard reset the card. This used to pause for the same period that a
135 8390 reset command required, but that shouldn't be necessary. */
137 static void ax_reset_8390(struct net_device *dev)
139 struct ei_device *ei_local = netdev_priv(dev);
140 unsigned long reset_start_time = jiffies;
141 void __iomem *addr = (void __iomem *)dev->base_addr;
143 if (ei_debug > 1)
144 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
146 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
148 ei_status.txing = 0;
149 ei_status.dmaing = 0;
151 /* This check _should_not_ be necessary, omit eventually. */
152 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
153 if (jiffies - reset_start_time > 2*HZ/100) {
154 printk(KERN_WARNING "%s: %s did not complete.\n",
155 __FUNCTION__, dev->name);
156 break;
160 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
164 static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
165 int ring_page)
167 struct ei_device *ei_local = netdev_priv(dev);
168 void __iomem *nic_base = ei_local->mem;
170 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
171 if (ei_status.dmaing) {
172 printk(KERN_EMERG "%s: DMAing conflict in %s [DMAstat:%d][irqlock:%d].\n",
173 dev->name, __FUNCTION__,
174 ei_status.dmaing, ei_status.irqlock);
175 return;
178 ei_status.dmaing |= 0x01;
179 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
180 ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
181 ei_outb(0, nic_base + EN0_RCNTHI);
182 ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
183 ei_outb(ring_page, nic_base + EN0_RSARHI);
184 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
186 if (ei_status.word16)
187 readsw(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
188 else
189 readsb(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
191 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
192 ei_status.dmaing &= ~0x01;
194 le16_to_cpus(&hdr->count);
198 /* Block input and output, similar to the Crynwr packet driver. If you
199 are porting to a new ethercard, look at the packet driver source for hints.
200 The NEx000 doesn't share the on-board packet memory -- you have to put
201 the packet out through the "remote DMA" dataport using ei_outb. */
203 static void ax_block_input(struct net_device *dev, int count,
204 struct sk_buff *skb, int ring_offset)
206 struct ei_device *ei_local = netdev_priv(dev);
207 void __iomem *nic_base = ei_local->mem;
208 char *buf = skb->data;
210 if (ei_status.dmaing) {
211 printk(KERN_EMERG "%s: DMAing conflict in ax_block_input "
212 "[DMAstat:%d][irqlock:%d].\n",
213 dev->name, ei_status.dmaing, ei_status.irqlock);
214 return;
217 ei_status.dmaing |= 0x01;
219 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
220 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
221 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
222 ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
223 ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
224 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
226 if (ei_status.word16) {
227 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
228 if (count & 0x01)
229 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
231 } else {
232 readsb(nic_base + NE_DATAPORT, buf, count);
235 ei_status.dmaing &= ~1;
238 static void ax_block_output(struct net_device *dev, int count,
239 const unsigned char *buf, const int start_page)
241 struct ei_device *ei_local = netdev_priv(dev);
242 void __iomem *nic_base = ei_local->mem;
243 unsigned long dma_start;
245 /* Round the count up for word writes. Do we need to do this?
246 What effect will an odd byte count have on the 8390?
247 I should check someday. */
249 if (ei_status.word16 && (count & 0x01))
250 count++;
252 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
253 if (ei_status.dmaing) {
254 printk(KERN_EMERG "%s: DMAing conflict in %s."
255 "[DMAstat:%d][irqlock:%d]\n",
256 dev->name, __FUNCTION__,
257 ei_status.dmaing, ei_status.irqlock);
258 return;
261 ei_status.dmaing |= 0x01;
262 /* We should already be in page 0, but to be safe... */
263 ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
265 ei_outb(ENISR_RDC, nic_base + EN0_ISR);
267 /* Now the normal output. */
268 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
269 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
270 ei_outb(0x00, nic_base + EN0_RSARLO);
271 ei_outb(start_page, nic_base + EN0_RSARHI);
273 ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
274 if (ei_status.word16) {
275 writesw(nic_base + NE_DATAPORT, buf, count>>1);
276 } else {
277 writesb(nic_base + NE_DATAPORT, buf, count);
280 dma_start = jiffies;
282 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
283 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
284 printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
285 ax_reset_8390(dev);
286 ax_NS8390_init(dev,1);
287 break;
291 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
292 ei_status.dmaing &= ~0x01;
293 return;
296 /* definitions for accessing MII/EEPROM interface */
298 #define AX_MEMR EI_SHIFT(0x14)
299 #define AX_MEMR_MDC (1<<0)
300 #define AX_MEMR_MDIR (1<<1)
301 #define AX_MEMR_MDI (1<<2)
302 #define AX_MEMR_MDO (1<<3)
303 #define AX_MEMR_EECS (1<<4)
304 #define AX_MEMR_EEI (1<<5)
305 #define AX_MEMR_EEO (1<<6)
306 #define AX_MEMR_EECLK (1<<7)
308 /* ax_mii_ei_outbits
310 * write the specified set of bits to the phy
313 static void
314 ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
316 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
317 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
318 unsigned int memr;
320 /* clock low, data to output mode */
321 memr = ei_inb(memr_addr);
322 memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
323 ei_outb(memr, memr_addr);
325 for (len--; len >= 0; len--) {
326 if (bits & (1 << len))
327 memr |= AX_MEMR_MDO;
328 else
329 memr &= ~AX_MEMR_MDO;
331 ei_outb(memr, memr_addr);
333 /* clock high */
335 ei_outb(memr | AX_MEMR_MDC, memr_addr);
336 udelay(1);
338 /* clock low */
339 ei_outb(memr, memr_addr);
342 /* leaves the clock line low, mdir input */
343 memr |= AX_MEMR_MDIR;
344 ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
347 /* ax_phy_ei_inbits
349 * read a specified number of bits from the phy
352 static unsigned int
353 ax_phy_ei_inbits(struct net_device *dev, int no)
355 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
356 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
357 unsigned int memr;
358 unsigned int result = 0;
360 /* clock low, data to input mode */
361 memr = ei_inb(memr_addr);
362 memr &= ~AX_MEMR_MDC;
363 memr |= AX_MEMR_MDIR;
364 ei_outb(memr, memr_addr);
366 for (no--; no >= 0; no--) {
367 ei_outb(memr | AX_MEMR_MDC, memr_addr);
369 udelay(1);
371 if (ei_inb(memr_addr) & AX_MEMR_MDI)
372 result |= (1<<no);
374 ei_outb(memr, memr_addr);
377 return result;
380 /* ax_phy_issueaddr
382 * use the low level bit shifting routines to send the address
383 * and command to the specified phy
386 static void
387 ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
389 if (phy_debug)
390 pr_debug("%s: dev %p, %04x, %04x, %d\n",
391 __FUNCTION__, dev, phy_addr, reg, opc);
393 ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */
394 ax_mii_ei_outbits(dev, 1, 2); /* frame-start */
395 ax_mii_ei_outbits(dev, opc, 2); /* op code */
396 ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */
397 ax_mii_ei_outbits(dev, reg, 5); /* reg address */
400 static int
401 ax_phy_read(struct net_device *dev, int phy_addr, int reg)
403 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
404 unsigned long flags;
405 unsigned int result;
407 spin_lock_irqsave(&ei_local->page_lock, flags);
409 ax_phy_issueaddr(dev, phy_addr, reg, 2);
411 result = ax_phy_ei_inbits(dev, 17);
412 result &= ~(3<<16);
414 spin_unlock_irqrestore(&ei_local->page_lock, flags);
416 if (phy_debug)
417 pr_debug("%s: %04x.%04x => read %04x\n", __FUNCTION__,
418 phy_addr, reg, result);
420 return result;
423 static void
424 ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
426 struct ei_device *ei = (struct ei_device *) netdev_priv(dev);
427 unsigned long flags;
429 printk(KERN_DEBUG "%s: %p, %04x, %04x %04x\n",
430 __FUNCTION__, dev, phy_addr, reg, value);
432 spin_lock_irqsave(&ei->page_lock, flags);
434 ax_phy_issueaddr(dev, phy_addr, reg, 1);
435 ax_mii_ei_outbits(dev, 2, 2); /* send TA */
436 ax_mii_ei_outbits(dev, value, 16);
438 spin_unlock_irqrestore(&ei->page_lock, flags);
441 static void ax_mii_expiry(unsigned long data)
443 struct net_device *dev = (struct net_device *)data;
444 struct ax_device *ax = to_ax_dev(dev);
445 unsigned long flags;
447 spin_lock_irqsave(&ax->mii_lock, flags);
448 mii_check_media(&ax->mii, netif_msg_link(ax), 0);
449 spin_unlock_irqrestore(&ax->mii_lock, flags);
451 if (ax->running) {
452 ax->mii_timer.expires = jiffies + HZ*2;
453 add_timer(&ax->mii_timer);
457 static int ax_open(struct net_device *dev)
459 struct ax_device *ax = to_ax_dev(dev);
460 struct ei_device *ei_local = netdev_priv(dev);
461 int ret;
463 dev_dbg(&ax->dev->dev, "%s: open\n", dev->name);
465 ret = request_irq(dev->irq, ax_ei_interrupt, 0, dev->name, dev);
466 if (ret)
467 return ret;
469 ret = ax_ei_open(dev);
470 if (ret)
471 return ret;
473 /* turn the phy on (if turned off) */
475 ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
476 ax->running = 1;
478 /* start the MII timer */
480 init_timer(&ax->mii_timer);
482 ax->mii_timer.expires = jiffies+1;
483 ax->mii_timer.data = (unsigned long) dev;
484 ax->mii_timer.function = ax_mii_expiry;
486 add_timer(&ax->mii_timer);
488 return 0;
491 static int ax_close(struct net_device *dev)
493 struct ax_device *ax = to_ax_dev(dev);
494 struct ei_device *ei_local = netdev_priv(dev);
496 dev_dbg(&ax->dev->dev, "%s: close\n", dev->name);
498 /* turn the phy off */
500 ei_outb(ax->plat->gpoc_val | (1<<6),
501 ei_local->mem + EI_SHIFT(0x17));
503 ax->running = 0;
504 wmb();
506 del_timer_sync(&ax->mii_timer);
507 ax_ei_close(dev);
509 free_irq(dev->irq, dev);
510 return 0;
513 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
515 struct ax_device *ax = to_ax_dev(dev);
516 unsigned long flags;
517 int rc;
519 if (!netif_running(dev))
520 return -EINVAL;
522 spin_lock_irqsave(&ax->mii_lock, flags);
523 rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
524 spin_unlock_irqrestore(&ax->mii_lock, flags);
526 return rc;
529 /* ethtool ops */
531 static void ax_get_drvinfo(struct net_device *dev,
532 struct ethtool_drvinfo *info)
534 struct ax_device *ax = to_ax_dev(dev);
536 strcpy(info->driver, DRV_NAME);
537 strcpy(info->version, DRV_VERSION);
538 strcpy(info->bus_info, ax->dev->name);
541 static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
543 struct ax_device *ax = to_ax_dev(dev);
544 unsigned long flags;
546 spin_lock_irqsave(&ax->mii_lock, flags);
547 mii_ethtool_gset(&ax->mii, cmd);
548 spin_lock_irqsave(&ax->mii_lock, flags);
550 return 0;
553 static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
555 struct ax_device *ax = to_ax_dev(dev);
556 unsigned long flags;
557 int rc;
559 spin_lock_irqsave(&ax->mii_lock, flags);
560 rc = mii_ethtool_sset(&ax->mii, cmd);
561 spin_lock_irqsave(&ax->mii_lock, flags);
563 return rc;
566 static int ax_nway_reset(struct net_device *dev)
568 struct ax_device *ax = to_ax_dev(dev);
569 return mii_nway_restart(&ax->mii);
572 static u32 ax_get_link(struct net_device *dev)
574 struct ax_device *ax = to_ax_dev(dev);
575 return mii_link_ok(&ax->mii);
578 static const struct ethtool_ops ax_ethtool_ops = {
579 .get_drvinfo = ax_get_drvinfo,
580 .get_settings = ax_get_settings,
581 .set_settings = ax_set_settings,
582 .nway_reset = ax_nway_reset,
583 .get_link = ax_get_link,
586 #ifdef CONFIG_AX88796_93CX6
587 static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
589 struct ei_device *ei_local = eeprom->data;
590 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
592 eeprom->reg_data_in = reg & AX_MEMR_EEI;
593 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
594 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
595 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
598 static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
600 struct ei_device *ei_local = eeprom->data;
601 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
603 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
605 if (eeprom->reg_data_in)
606 reg |= AX_MEMR_EEI;
607 if (eeprom->reg_data_clock)
608 reg |= AX_MEMR_EECLK;
609 if (eeprom->reg_chip_select)
610 reg |= AX_MEMR_EECS;
612 ei_outb(reg, ei_local->mem + AX_MEMR);
613 udelay(10);
615 #endif
617 /* setup code */
619 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
621 void __iomem *ioaddr = ei_local->mem;
622 struct ax_device *ax = to_ax_dev(dev);
624 /* Select page 0*/
625 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD);
627 /* set to byte access */
628 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
629 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
632 /* ax_init_dev
634 * initialise the specified device, taking care to note the MAC
635 * address it may already have (if configured), ensure
636 * the device is ready to be used by lib8390.c and registerd with
637 * the network layer.
640 static int ax_init_dev(struct net_device *dev, int first_init)
642 struct ei_device *ei_local = netdev_priv(dev);
643 struct ax_device *ax = to_ax_dev(dev);
644 void __iomem *ioaddr = ei_local->mem;
645 unsigned int start_page;
646 unsigned int stop_page;
647 int ret;
648 int i;
650 ret = ax_initial_check(dev);
651 if (ret)
652 goto err_out;
654 /* setup goes here */
656 ax_initial_setup(dev, ei_local);
658 /* read the mac from the card prom if we need it */
660 if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) {
661 unsigned char SA_prom[32];
663 for(i = 0; i < sizeof(SA_prom); i+=2) {
664 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
665 SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT);
668 if (ax->plat->wordlength == 2)
669 for (i = 0; i < 16; i++)
670 SA_prom[i] = SA_prom[i+i];
672 memcpy(dev->dev_addr, SA_prom, 6);
675 #ifdef CONFIG_AX88796_93CX6
676 if (first_init && ax->plat->flags & AXFLG_HAS_93CX6) {
677 unsigned char mac_addr[6];
678 struct eeprom_93cx6 eeprom;
680 eeprom.data = ei_local;
681 eeprom.register_read = ax_eeprom_register_read;
682 eeprom.register_write = ax_eeprom_register_write;
683 eeprom.width = PCI_EEPROM_WIDTH_93C56;
685 eeprom_93cx6_multiread(&eeprom, 0,
686 (__le16 __force *)mac_addr,
687 sizeof(mac_addr) >> 1);
689 memcpy(dev->dev_addr, mac_addr, 6);
691 #endif
692 if (ax->plat->wordlength == 2) {
693 /* We must set the 8390 for word mode. */
694 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
695 start_page = NESM_START_PG;
696 stop_page = NESM_STOP_PG;
697 } else {
698 start_page = NE1SM_START_PG;
699 stop_page = NE1SM_STOP_PG;
702 /* load the mac-address from the device if this is the
703 * first time we've initialised */
705 if (first_init && ax->plat->flags & AXFLG_MAC_FROMDEV) {
706 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
707 ei_local->mem + E8390_CMD); /* 0x61 */
709 for (i = 0 ; i < ETHER_ADDR_LEN ; i++)
710 dev->dev_addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
713 ax_reset_8390(dev);
715 ei_status.name = "AX88796";
716 ei_status.tx_start_page = start_page;
717 ei_status.stop_page = stop_page;
718 ei_status.word16 = (ax->plat->wordlength == 2);
719 ei_status.rx_start_page = start_page + TX_PAGES;
721 #ifdef PACKETBUF_MEMSIZE
722 /* Allow the packet buffer size to be overridden by know-it-alls. */
723 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
724 #endif
726 ei_status.reset_8390 = &ax_reset_8390;
727 ei_status.block_input = &ax_block_input;
728 ei_status.block_output = &ax_block_output;
729 ei_status.get_8390_hdr = &ax_get_8390_hdr;
730 ei_status.priv = 0;
732 dev->open = ax_open;
733 dev->stop = ax_close;
734 dev->do_ioctl = ax_ioctl;
735 dev->ethtool_ops = &ax_ethtool_ops;
737 ax->msg_enable = NETIF_MSG_LINK;
738 ax->mii.phy_id_mask = 0x1f;
739 ax->mii.reg_num_mask = 0x1f;
740 ax->mii.phy_id = 0x10; /* onboard phy */
741 ax->mii.force_media = 0;
742 ax->mii.full_duplex = 0;
743 ax->mii.mdio_read = ax_phy_read;
744 ax->mii.mdio_write = ax_phy_write;
745 ax->mii.dev = dev;
747 #ifdef CONFIG_NET_POLL_CONTROLLER
748 dev->poll_controller = ax_ei_poll;
749 #endif
750 ax_NS8390_init(dev, 0);
752 if (first_init) {
753 printk("AX88796: %dbit, irq %d, %lx, MAC: ",
754 ei_status.word16 ? 16:8, dev->irq, dev->base_addr);
756 for (i = 0; i < ETHER_ADDR_LEN; i++)
757 printk("%2.2x%c", dev->dev_addr[i],
758 (i < (ETHER_ADDR_LEN-1) ? ':' : ' '));
760 printk("\n");
763 ret = register_netdev(dev);
764 if (ret)
765 goto out_irq;
767 return 0;
769 out_irq:
770 /* cleanup irq */
771 free_irq(dev->irq, dev);
772 err_out:
773 return ret;
776 static int ax_remove(struct platform_device *_dev)
778 struct net_device *dev = platform_get_drvdata(_dev);
779 struct ax_device *ax;
781 ax = to_ax_dev(dev);
783 unregister_netdev(dev);
784 free_irq(dev->irq, dev);
786 iounmap(ei_status.mem);
787 release_resource(ax->mem);
788 kfree(ax->mem);
790 if (ax->map2) {
791 iounmap(ax->map2);
792 release_resource(ax->mem2);
793 kfree(ax->mem2);
796 free_netdev(dev);
798 return 0;
801 /* ax_probe
803 * This is the entry point when the platform device system uses to
804 * notify us of a new device to attach to. Allocate memory, find
805 * the resources and information passed, and map the necessary registers.
808 static int ax_probe(struct platform_device *pdev)
810 struct net_device *dev;
811 struct ax_device *ax;
812 struct resource *res;
813 size_t size;
814 int ret;
816 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
817 if (dev == NULL)
818 return -ENOMEM;
820 /* ok, let's setup our device */
821 ax = to_ax_dev(dev);
823 memset(ax, 0, sizeof(struct ax_device));
825 spin_lock_init(&ax->mii_lock);
827 ax->dev = pdev;
828 ax->plat = pdev->dev.platform_data;
829 platform_set_drvdata(pdev, dev);
831 ei_status.rxcr_base = ax->plat->rcr_val;
833 /* find the platform resources */
835 dev->irq = platform_get_irq(pdev, 0);
836 if (dev->irq < 0) {
837 dev_err(&pdev->dev, "no IRQ specified\n");
838 ret = -ENXIO;
839 goto exit_mem;
842 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
843 if (res == NULL) {
844 dev_err(&pdev->dev, "no MEM specified\n");
845 ret = -ENXIO;
846 goto exit_mem;
849 size = (res->end - res->start) + 1;
851 /* setup the register offsets from either the platform data
852 * or by using the size of the resource provided */
854 if (ax->plat->reg_offsets)
855 ei_status.reg_offset = ax->plat->reg_offsets;
856 else {
857 ei_status.reg_offset = ax->reg_offsets;
858 for (ret = 0; ret < 0x18; ret++)
859 ax->reg_offsets[ret] = (size / 0x18) * ret;
862 ax->mem = request_mem_region(res->start, size, pdev->name);
863 if (ax->mem == NULL) {
864 dev_err(&pdev->dev, "cannot reserve registers\n");
865 ret = -ENXIO;
866 goto exit_mem;
869 ei_status.mem = ioremap(res->start, size);
870 dev->base_addr = (unsigned long)ei_status.mem;
872 if (ei_status.mem == NULL) {
873 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
874 (unsigned long long)res->start,
875 (unsigned long long)res->end);
877 ret = -ENXIO;
878 goto exit_req;
881 /* look for reset area */
883 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
884 if (res == NULL) {
885 if (!ax->plat->reg_offsets) {
886 for (ret = 0; ret < 0x20; ret++)
887 ax->reg_offsets[ret] = (size / 0x20) * ret;
890 ax->map2 = NULL;
891 } else {
892 size = (res->end - res->start) + 1;
894 ax->mem2 = request_mem_region(res->start, size, pdev->name);
895 if (ax->mem == NULL) {
896 dev_err(&pdev->dev, "cannot reserve registers\n");
897 ret = -ENXIO;
898 goto exit_mem1;
901 ax->map2 = ioremap(res->start, size);
902 if (ax->map2 == NULL) {
903 dev_err(&pdev->dev, "cannot map reset register\n");
904 ret = -ENXIO;
905 goto exit_mem2;
908 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
911 /* got resources, now initialise and register device */
913 ret = ax_init_dev(dev, 1);
914 if (!ret)
915 return 0;
917 if (ax->map2 == NULL)
918 goto exit_mem1;
920 iounmap(ax->map2);
922 exit_mem2:
923 release_resource(ax->mem2);
924 kfree(ax->mem2);
926 exit_mem1:
927 iounmap(ei_status.mem);
929 exit_req:
930 release_resource(ax->mem);
931 kfree(ax->mem);
933 exit_mem:
934 free_netdev(dev);
936 return ret;
939 /* suspend and resume */
941 #ifdef CONFIG_PM
942 static int ax_suspend(struct platform_device *dev, pm_message_t state)
944 struct net_device *ndev = platform_get_drvdata(dev);
945 struct ax_device *ax = to_ax_dev(ndev);
947 ax->resume_open = ax->running;
949 netif_device_detach(ndev);
950 ax_close(ndev);
952 return 0;
955 static int ax_resume(struct platform_device *pdev)
957 struct net_device *ndev = platform_get_drvdata(pdev);
958 struct ax_device *ax = to_ax_dev(ndev);
960 ax_initial_setup(ndev, netdev_priv(ndev));
961 ax_NS8390_init(ndev, ax->resume_open);
962 netif_device_attach(ndev);
964 if (ax->resume_open)
965 ax_open(ndev);
967 return 0;
970 #else
971 #define ax_suspend NULL
972 #define ax_resume NULL
973 #endif
975 static struct platform_driver axdrv = {
976 .driver = {
977 .name = "ax88796",
978 .owner = THIS_MODULE,
980 .probe = ax_probe,
981 .remove = ax_remove,
982 .suspend = ax_suspend,
983 .resume = ax_resume,
986 static int __init axdrv_init(void)
988 return platform_driver_register(&axdrv);
991 static void __exit axdrv_exit(void)
993 platform_driver_unregister(&axdrv);
996 module_init(axdrv_init);
997 module_exit(axdrv_exit);
999 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1000 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1001 MODULE_LICENSE("GPL v2");